Thursday, May 22, 2008

Manifest Constants

Do you know!

null , true and false are called reserved constants or manifest constants in Java. These are constants which is assigned a specific value in the programming language itself.

Each program will have different repesentation of the constants for example. null value in java is represented as 'null' and the same in C is represented as 'NULL'.

Tuesday, May 6, 2008

intern() option in String

Why are we not using the so called String.intern() in our coding practice.

Is it because we don't know the functionality. If so.. let me say something on this.. The actual functionality of this function is to give an unique string object from an internal table.

This literally means the function is meant to return a basic representation of the string.

Internally a pool of string is maintained by the class.. when the method is actually invoked on the String object.. if the pool already contains the string is checked using String.equals() and the string from the pool is returned. if it is not already present the string is actually added to the pool and returned back.
All the string with the same content will refer to the same pointer which holds the representation of the string.

The intern() option is to save memory as the headache of comparing objects using equals is reduced and allows users to compare strings using '=='


for example..

String a = "Hello";
char[] b = {'H','e','l','l','o'};
String c = new String(b);
System.out.println(a==c);

this will return a false. as the actual representation is not matched.

we can overcome this using String.intern()

System.out.println(a==c.intern());

returns true.. as the function is more concerned about the represtation of the string.