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.

Let me explain this with an example (I have used the interactive mode of scala command):

scala> val tple=("Hello World",56, 56.66, true)
tple: (java.lang.String, Int, Double, Boolean)= (Hello, Int, Double, Boolean)

The above code creates a tuple of 4 elements of totally unrelated types.  It can be clearly seen in the result printed soon after the statement.

scala>println(tple._1)
Hello World

scala>println(tple._4)
true

It has to be noted that the parameter are referenced from 1 and not from 0. So its pretty straight forward- use <Tuple Name>._<Parameter Position> to access the element.

There are different ways of creating Tuples. Some of them are:

scala>Tuple4(5,6,"Hello",6.5)
res0:(Int,Int,java.lang.String, Double)=(5,6,"Hello",6.5)

scala>val tple1=1->2->"Hi"
tple1: ((Int,Int),java.lang.String)=((1,2),"Hi")

Observe that the tple1 contains another tuple inside it- (Int,Int). Use the same idea to access it.

scala>println(tple1._1._1)1

This was in brief about Tuples. This concept caught my attention immediately as i had faced the problem in Java. So thought of describing it here.

Advertisements
Categories: Scala Tags: , , ,

“val” versus “var” Declarations in Scala

Scala allows one to decide whether a variable is immutable or mutable. Immutable is Read only where as mutable is read-write. Immutable variables are declared with the keyword “val“.

scala> val age:Int=22

Here age has been initialized to a value during its declaration. As it has been declared as a val, age cannot be reassigned to a new value. Any attempt to do will result in al reassignment to val error. Let us consider an Array being declared as val.

scala> val names:Array[String]=new Array(6)

Here names reference cannot be changed to point to a different Array, but the array itself can be modified. In other words the contents/elements of the array can be modified.

scala> names= new Array(5) //This will give a reassignment to val error. 

scala> names(0)="Scala" //This does not give an error.

Also it is to be noted that val variables must be initialized when they are declared.

Mutable variables are delcared with the keyword “var“. Unlike val, “var” can be reassigned to different values or point to different objects. But they have to be initialised at the time of declaration.

scala> var age:Int=22
age: Int=22

scala> age=35
age: Int=35

There’s an exception to the rule that one must initialize the val’s and var’s. When they are used as constructor parameters, the val’s and var’s will be initialised when the object is instantiated. Also, derived classes can override val’s declared inside the parent classes.

Categories: Scala Tags: , , , ,

Monitoring and Profiling using VisualVM-1

I executed a simple GUI application which would load the CSV file and parse it and show the contents in a JTable. When the applications started- There was a JFrame, 2 JPanels, a JLabel and a JButton with an Icon. I wanted to monitor the Heap size variations, the number of Classes, Threads details and also wanted to profile the application. So i thought of using VisualVM. The following are the results and snapshots of profiling using VisualVM. Note that the application had only one public class MainFrame in gui package. Also note that i was using the Nimbus Look and Feel.

Read more…

My JavaFX Application

Was going thru the JavaFX 1.2 API and found a lot of new features some of them are: New JavaFX Controls, New Layouts, Charting API, Local Data Storage among others. I was impressed by few samples which i saw here and here. So i thought of exploring the New JavaFX Controls and created a sample application.

Screenshot of the application:

Run the JNLP file here.

Download the source code here.

Will try out the Chart API as well!!!!

Categories: JavaFX Tags: , ,

Selection Sort: Brute Force approach to sorting with example in Groovy

In Selection sort technique the list is scanned to find the smallest element and it is then exchanged with the first element, putting the smallest element in its final position i.e at the beginning. Then the list is scanned starting from the second element to find the smallest among the remaining n-1 elements and exchange it with the second element to put the second smallest element in its final position. In general in the ith pass the algorithm searches for the smallest number from the n-i elements and swaps it with the element in the ith position i.e Ai. The list is sorted after n-1 passes. Read more…

Core Java Refcardz by Cay S. Horstmann

November 12, 2008 Leave a comment

This refcard gives you an overview of key aspects of the Java language and cheat sheets on the core library (formatted output, collections, regular expressions, logging, properties) as well as the most commonly used tools (javac, java, jar).Core Java

Features include Java Keywords, Standard Java Packages, Character Escape Sequences, Collections and Common Algorithms, Regular Expressions, JAR Files and more.

To download the Refcardz click here.

Categories: Articles Tags: , , ,

Vote for your IDE: Best IDE for Java Application Development

October 20, 2008 3 comments

Hi readers,

I have created this poll to get an idea of the popularity of various IDEs for Java Application developments all over the world. Do vote for your favorite IDE and also if possible leave a comment on why you voted for the IDE. Looking forward for a huge response.

Categories: General Java, IDE Tags: , ,