J2EE Enterprise Design Patterns | how to configure web.xml (Or Deployment descriptor) | Java Application using MyEclipse Debugging | Scala Language

Archive for the ‘Scala Language’ Category

Since last couple of days I was chasing Scala Language Implicit Conversions and implicit Parameters. When I was started to learn Play Framework, I have found there are many places in Controllers Action methods taking implicit request as input parameter and returning response based on executing some logic on implicit request.  During my leaning I was found it was not easy to get an idea about implicit Conversions and Parameters, so I thought to share my knowledge which explains you about some of it concept with examples and different cases.

If you have past experience on C, C++, Java or some programing language you guys have already used this feature as part of respective compiler implementation but you were not aware of it because Scala is the language which provide implicit as language feature. In case of implicit conversion compiler “Do the right thing for you” and convert lower level type to upper level whenever it’s required. Let’s understand this with example.

In salesforce we are using,

System.debug(‘Hello Salesforce  ‘ + 1);                  and                        Println(‘Hello Salesforce  ‘ + 1.0);

Same as in Java we are using somewhere in codes like,

System.out.println(“……  Hello Java” + 1.8);

If you see System.out.println taking two different types of parameters, one having String type and another having value of Integer type.

Have you ever thought of it, why it has happened and why java compiler has not given compilation error? Here comes implicit comes into picture. Behind the hood compiler will insert such convention for you, if compiler will not provide you such facility it would have given you compiler error while you trying to print using two different types for expression together.

Now let’s try to understand how these languages sometime accept different types of expressions as method argument and not showing compilation error. So when java compiler will see System.out.printl() with different expression, compiler will look for respective conversion method for the same scope and if it will find respective method, it will convert to appropriate type. So java has already done the job for you by implementing below method, and we can call these types of methods to run-time conversion methods.

public String int2String(int i) {
return String.valueOf(i);
}

So far so good, I guess now you have got your answer. When compiler saw System.out.println(“Hello Java” + 1) it will insert below convention for you,

System.out.println(“Hello Java” + int2String(1));

The same way it has happened for float2String, boolean2String etc……. The example you have seen above is called implicit conversion and understand how different languages using implicit conversion as part of their implementation.

Same way java 1.5 having concept of AutoBoxing and UnBoxing. These are also an example of implicit conversion. Java collection is collection of objects. When you declare list of integer

List list = new List();
list.add(1); //woking fine with JDK 1.5 or later

Above statement will work fine if you are using JDK 1.5 or later, prior JDK1.5 you have to write like,

list.add(new Integer(1));

Next part I will show you how Scala providing implicit as language feature and provide developer to freedom to use it in project implementation. I feel that Scala implicit must have to understand all Scala developer who all wants to write expressive code, which is easy to readable and execute complex logic behind the lid.

Hope this will help you. Please feel free to provide your feedback…. 🙂

Advertisements

I am writing this blog to give you brief on Scala technology. Since past couple of days I was reading Scala and Scala based web framework Lift so just thought to share something what I did. So before starts with it, give you some brief idea what scala is?

Scala is a multi-paradigm language which runs on the JVM also gives the power of Functional Programming as well as Object Oriented programming and you can also say this language is better java with lot of utility classes, functions and all. The name Scala derived for “Scalable” and “Language” and it has been designed by Martin Odersky very well known for “Java Generics”.

Why Scala ? we have already good technology in the market like Java, C++, Ruby on Rails, and why we are talking about Scala technology? The popularity of Java and C++ has been decreasing against modern dynamic programming languages like Scala. These technologies provide more powerful feathers, and by providing effective syntax reducing lots of line of code.

As a Java, J2EE and Salesforce developer when I was started to learn Scala language first I started to look into comparisons between Java and Scala. Now below you will see some features provided by scala with respect to Java language. The best way to compare Scala with Java is to start with writing a simple Class. Writing a class in scala is simple and if you know how to write class in Java and C++ then you will not find any difficulty and I must say you will enjoy the syntactical freedom provided by Scala.

Now I am writing a Class called Car having two variables with one is final. Show below class for more details,


package com.interview.tests;

public class Car {

private String color;

private final int version = 1;

public Car(String color) {
this.color = color;
}

public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}

public int getVersion() {
return version;
}

}

In Scala if you want to write above class you just have to write two line of code,

class Car(color: String, val version: Int) {

}

Scala compiler generates the same thing which we have written in java by removing Public as class access modifier and Private as variable access modifier. In short, you get the same functionality by writing the less code, easier to read and less error prone than Java Class.

Yes I know as a developer you think if scala define constructor like this how can we define multiple constructor. Constructors in Scala are a bit different than Java. Scala has two type of Constructor.

  1. Primary Constructor
  2. Auxiliary Constructor
  • Primary Constructor As we know in Java we have a no argument constructor which is provided by every class and we don’t have to write explicitly. In scala primary constructor also comes in same way and we can define in scala as below,

class Car {
var color: string;
}

Above scala constructor doesn’t accept any parameters, but you can also define primary constructor with parameters and this

is how scala constructor differs from Java one. We can define parameter base constructer in scala as below,

class Car(val version: Int , var color: String) {

}

  • Auxiliary Constructor If we want to declare more than one constructor in Java we used to overloading mechanism accepting different parameters. On similar note Scala classes declare Auxiliary constructor which are overloaded forms of the Primary one. In scala the Auxiliary constructor are named as this like,


class Car(val version: Int , var color: String) {
//Auxiliary Constructor
def this(val version: Int , var color: String, , var name: String){
this(version , color)
this.name = name
}
}

Yes as you see you must call primary constructor before you go ahead with your Auxiliary based constructor.

You learn how to declare class now you will see how to declare variable. Like class remove access modifier same as in variable declaration you don’t need to write access modifier and variable declaration comes in two ways,

  • Var: using var you can declare your normal variable which comes with getter and setter method and you can change your variable value using setter and get value using getter.
  • Val: using val you can declare your Final type of variable which comes only with getter method you cannot set the value.

You already aware of Scala Class and variable it’s time to learn about scala member function. In scala function starts with def keyword with function name and arguments. Below you can find how to write function in scala,


def mileage(runningKm : Double, petrolPrice: Double) : Double = {
runningKm/ petrolPrice
}

Now as you see mileage function starts with def keyword, having two parameters with Double type running kilometres and petrol price which returns calculated car mileage in Double. But wait we have not written any return keyword in method, yes scala provide you the freedom where you don’t need to write return and in function last statement be your return value. Here you will find return type written after function “:  Double = “. Scala also identified return type without mentioning return type at function, you just need to write “=” equals after function like,


def mileage(runningKm : Double, petrolPrice: Double) = {
runningKm/ petrolPrice
}

Both the method behaving same. Now what if you want to write method which return void or nothing.  Scala gives you Unit keyword as void in Java like,


def mileage(runningKm: Double , petrolPrice: Double): Unit = {
runningKm/petrolPrice
}

def mileage(runningKm: Double , petrolPrice: Double) {
runningKm/petrolPrice
}

Both the methods are same and returning nothing(Void).

For more topics follow below URLs,

Thanks for reading you will see some more stuff related to Scala some more time……