Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Immutable classes in java

 
gaurav singhal
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in Java the following classes are immutable:-
String,
Exception,
URL,
Character,
Byte,
Integer,
Short,
Long,
Float
and Double
 
Girish Nagaraj
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Girish Nagaraj
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Herb Tybur
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic