Overview of the Java sessions at the Great Indian Dev Summit-2012

There was a lot to learn at the Java sessions of the GIDS 2012 with topics covering Java 7, Java 8, Concurrency in Java using STM model, Java EE 7 and its various JSRs, Scala, JavaScript among other topics. And not to forget a beautiful key note by Venkat Subramaniam about the need for developers … Read more

Partially applied functions in Scala

Before going into Partially applied Functions, let me introduces 2 terms-
Function Literal- This exists in the source code- Something similar to Class definition. So we have

(x:Int,y:Int) => x+y

Function value- When this function literal is assigned to a reference they become function values. The are similar to the Objects created from the class. These are created at runtime

var sum = (x:Int,y:Int) => x+y

So in the above case we can use sum to invoke the method.

println(sum(5,7))

Read more

Brief insight into Constructors in Scala

In the previous post here, I had introduced few concepts related to constructors in Scala. In this post I would go a bit deeper into the constructors and how we can provide different constructors. Summarizing about the introduction in the previous post here

Read more

Object Oriented Programming in Scala- 1

Scala uses both Object oriented and functional programming concepts. For folks coming from the Java, we would always explore how to do OOP in Scala. Here I thought of writing few posts related to OOP in Scala (may be a Series of posts ;)). In this post lets talk about-

  • Classes
  • Objects
  • Constructors
  • Overloaded Constructors

Read more

BOJUG Meet- 20th March 2010

BOJUG met for the month on March at Misys Software Solutions (India) Private Limited. The meet was completely focused on Android and learning Android framework is on the top of my wish list- So a perfect match it was :). Its since 2nd Year that I wanted to learn Android and also had 2 failed … Read more

Null, null, Nil, Nothing, None, and Unit in Scala

Null– Its a Trait. null– Its an instance of Null- Similar to Java null. Nil– Represents an emptry List of anything of zero length. Its not that it refers to nothing but it refers to List which has no contents. Nothing is a Trait. Its a subtype of everything. But not superclass of anything. There … Read more

Traits in Scala- Deep Dive

Traits are units of code reuse in Scala. Traits encapsulates methods and field definitions. Their role is similar to that of interfaces in Java- A workaround for Multiple Inheritance. But unlike Interfaces they can have method and field definitions. More features will be explained in the article.

Defining Traits:

A trait definition looks like a class definition but uses the keyword “trait.

trait Bounceable{

     def bounce():String="Bounce Method in Trait"

}

The above declared trait has a concrete method. The ability to declare concrete methods in an trait gives default implementation option. In interfaces one has to write/copy-paste the default declaration in each class which implements the interface or declare a class that implements that interface and let other classes extend the concrete class. The first method has code repition and the second method has the restriction that a class can extend only one class. So this feature of “trait” stands out best.

As in Java a class implements interface, in Scala classes traits are mixed into a class. This is done using either extends or “with” keywords.

class Ball(s: Int) extends Bouncable{

      var size:Int

      override def toString():String= Integer.toString(size)

}

In the above example we are using extends to mix in the trait- Here Ball is implicitly inherits trait’s superclass which is “AnyRef”. Lets try to use the class Ball to test the mixed in trait.

object Test{

           def main(args:Array[String])={

                    var ball = new Ball(33)

                    var ball2:Bounceable= new Ball(400)

                    println(ball.bounce()) //This prints "Bounce Method in Trait"

                    println(ball) //This prints "33"

                    println(ball2.bounce())

                    println(ball2)

         }

}

Note: Output written as comment.

Regarding the ball2 instance being declared as type “Bounceable”. Its a valid declaration cause supertype references can refer to the instances of its subtypes. But they cannot access the methods/fields declared by the subtypes (different with the overriding case).

Read more

How’s Scala different from Java?

Scala is statically type like Java but with Type Inferencing support. Which means that the scala compiler analyzes the code deeply to determine what type a particular value is. In Scala its not required to use semicolons to terminate a statement if the termination is obvious by line ending. If there are more than one … Read more

Control Structures in Scala- A brief overview

If Statements: Lets consider a first entry example of If statments, without using much of Scala’s features. if(5+6==10){ println("You are wrong in calculations") }else{ println("Keep it up") } This is a pretty simple and straight forward examples. Now lets add some Scala flavor in the If-Statement. In Scala If-Statements are expressions, which means that the … Read more

Tuples- Returning multiple values in Scala

When I was coding in Java I used to build Classes just to return multpile values and also sometimes used pass by reference (by means of using Objects). I really missed a permanent solution 🙁 Scala has a solution for this- It supports something called “Tuples” which is created with the literal syntax of a comma-separated list of the items inside parentheses like (x1,x2,x3 …). The items in the parantheses may not be related to each other in terms of the type, which means that we can have String’s, Int’s and so on. These literal “groupings” are instantiated as scala.TupleN instances, where the N is the number of items in the tuple. The Scala API defines separate TupleN classes for N between 1 and 22, inclusive. Tuples can be assigned to variables, passed as values or return them from the methods.