aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Immutable classes in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Immutable classes in java" Watch "Immutable classes in java" New topic
Author

Immutable classes in java

gaurav singhal
Ranch Hand

Joined: Nov 18, 2005
Posts: 135
I read a book which says that there are three immutable classes in java.

I know only one that is String can anybody name the other two Immutable classes in java.

Gaurav
Anil Kumar Saha
Ranch Hand

Joined: Apr 07, 2004
Posts: 111
in Java the following classes are immutable:-
String,
Exception,
URL,
Character,
Byte,
Integer,
Short,
Long,
Float
and Double

Regards,

Anil Kumar Saha
SCJP 1.4
http://www.agilej.blogspot.com/
Girish Nagaraj
Ranch Hand

Joined: Apr 19, 2006
Posts: 153
class A {

public static void main(String[] args) {

Integer i1 = 3;

System.out.println("i1 = " + i1);

i1++;

System.out.println("i1 = " + i1);
}
}

Compiles fine.O/p:-
3
4

Wrapper classes are muttable in sdk 1.5.

Am I right.
Filippo Vitale
Greenhorn

Joined: May 06, 2006
Posts: 12
I don't think so, please try step by step with a debugger, you will find that the line



create a new immutable Integer Object


Filippo


_ _ _____________________________ _ _ <br /> <br /> * SCJP 5.0
Girish Nagaraj
Ranch Hand

Joined: Apr 19, 2006
Posts: 153
class A {

public static void main(String[] args) {

Integer i1 = 3;

System.out.println("i1 = " + i1);

i1++; // line (1)

System.out.println("i1 = " + i1); // line (2)
}
}

1)If i1++ returns new immutable object then I am not catching it.
2)In line (2) I am printing i1 which shows its value is changed.

How???
gaurav singhal
Ranch Hand

Joined: Nov 18, 2005
Posts: 135
When you do i1++ it is equal to the i1=i1+1 so it start pointing to a new object but there are two object created in the heap memory.



Thanks Anil for naming the immutable class.
Filippo Vitale
Greenhorn

Joined: May 06, 2006
Posts: 12
Ok, we agree that i1 is a ref to an instance of Integer.

I think the line (1) can be rewritten as:


or

or


"new Integer" means a ref to a brand new Integer instance.

Am I wrong
Filippo
Girish Nagaraj
Ranch Hand

Joined: Apr 19, 2006
Posts: 153
class A {

A() {

System.out.println("A");
}

public static void main(String[] args) {

Integer i1 = 3, i2;

i2 = i1;

System.out.println("i1 = " + i1);

i1++;

System.out.println("i1 = " + i1);
System.out.println("(i1 == i2) :" + (i1 == i2)); // line 1
}
}

Yes you ppl are right.
I got false in line (1).
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
A class is immutable if you don't have the source at your disposal (or are not allowed to change that source.

A class instance is immutable is it has no way to change its datamembers once they're initialised.


42
Herb Tybur
Greenhorn

Joined: May 12, 2006
Posts: 19
Originally posted by Jeroen T Wenting:
A class is immutable if you don't have the source at your disposal (or are not allowed to change that source.



I believe that is wrong. Availability of the source code has got nothing to do with immutability of the class.

A class is immutable if all of its public methods are final, which means you cannot change the functionality of that class anyway. The class itself may be non-final.
 
 
subject: Immutable classes in java