• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Immutable classes in java

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in Java the following classes are immutable:-
String,
Exception,
URL,
Character,
Byte,
Integer,
Short,
Long,
Float
and Double
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
She'll be back. I'm just gonna wait here. With this tiny ad:
Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders
https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing
reply
    Bookmark Topic Watch Topic
  • New Topic