Java


3
Mar 08

The ‘final’ keyword in Java

Time to explore the ‘final‘ keyword.

When you use the keyword ‘final’ with a variable declaration, the value stored inside that variable cannot be changed come what may!!

So suppose you do this :

final int i =10;

After this, nowhere in your code can you change the value of ‘i’ i.e. ‘i’ will always have a value ’10′ .

So i = 11; //will give you a compile time error.

Now, suppose you did not initialize the final integer ‘i’ while declaring it.

final int i;

Can you assign any value to this variable now?

Yes, you definitely can. But only once!

So,

final int i;

i = 10; //This is fine

i = 11; //Compiler error

Now that was the core stuff about ‘final’ . Let’s learn a bit more in detail about it.

The ‘final’ keyword can be used for a class declaration or method declaration.

What happens when you use ‘final’ for a class declaration?

A: You cannot have a sub class of a class declared final i.e. No other class can extend this class.

What happens when you declare a method as final?

A: Simply put, the method cannot be overridden.

Can you override a method(Not declared as ‘final’) present inside a final class?

A: Obviously Not. Since there’s no way to extend a class declared final there’s no way of overriding its methods!

What happens when I declare a method inside a class as both ‘private’ and ‘final’ ?

A: Making a class both ‘private’ and ‘final’ makes the ‘final’ keyword redundant as a private method cannot be accessed in its sub class. But hey, you’ll be able to declare a method of the same name as in the base class if the method has been made private in the base class. But then that doesn’t mean you’re overriding the method. You’re simply declaring a new method in the sub class.

Note: You cannot make an ‘abstract’ class or method as ‘final’ because an ‘abstract’ class needs to be extended which will not be possible if you mark it as ‘final’!

I found this article on ‘final’ worth reading:

http://www.ibm.com/developerworks/java/library/j-jtp1029.html


29
Feb 08

New jdk 1.5 ‘for’ loop ( Enhanced for loop-’for each’)

int a[] = {1,2,3,4,5};

for(int i =0;i<a.length;i++)

{

System.out.println(“Value: “+a[i]);

}

This is so 1.4!!!

Here’s how you do it with the new and enhanced for loop :

for(int b: a)

{

System.out.println(“Value :”+b);

}

To make it easy for you to understand how to interpret the above condition, read it as follows:

” for each int b in a…..iterate”

Basically what you have to do is – To the left of the colon in the for loop declare the datatype that is present inside the array and to the right of the colon write the name of the array.

The above ‘for’ loop will iterate as many times as there are elements in the array ‘a’. At every iteration int b will have the value of the next element.

In simple terms its like this:

for(int b: a) will internally expand like this : for(int i=0;i<a.length;i++){int b=a[i];}

You can use this for loop for iterating over type collections too.

Example :

List<MyObject> al = new ArrayList();

//Create an Object of some class you want to put into the arraylist.

MyObject myObject = new MyObject();

MyObject myObject1 = new MyObject();

al.add(myObject);

al.add(myObject1);

for(MyObject o : al)

{

System.out.println(“Object hashcode is “+o.hashCode());

}


19
Feb 08

Simple example to demonstrate that String object is immutable

In my post ‘Difference between String and StringBuffer/StringBuilder’ I told you stuff like String object is immutable( meaning the value stored in the object cannot be changed) and that when you perform operations such as concat or replace, internally a new object is created to hold the result.

Below is a simple example that will make you believe that what I said about String object is indeed true!

String s = “Let’s test”;

s.concat(” if the String object is IMMUTABLE”);

System.out.println(s);

s = s.concat(” if the String object is IMMUTABLE”);

System.out.println(s);

The output of the above code will be:

Let’s test

Let’s test if the String object is IMMUTABLE

That’s all people! The above piece of code proves that String is immutable and hence the results of operations like concat etc. should be stored into a new object.