Immutable classes in java
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
I know only one that is String can anybody name the other two Immutable classes in java.
Gaurav
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
in Java the following classes are immutable:-
String,
Exception,
URL,
Character,
Byte,
Integer,
Short,
Long,
Float
and Double
String,
Exception,
URL,
Character,
Byte,
Integer,
Short,
Long,
Float
and Double
Regards,
Anil Kumar Saha
SCJP 1.4
http://www.agilej.blogspot.com/
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
create a new immutable Integer Object
Filippo
_ _ _____________________________ _ _ <br /> <br /> * SCJP 5.0
Girish Nagaraj
Ranch Hand
Posts: 153
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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???
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
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
Thanks Anil for naming the immutable class.
Filippo Vitale
Greenhorn
Posts: 12
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
_ _ _____________________________ _ _ <br /> <br /> * SCJP 5.0
Girish Nagaraj
Ranch Hand
Posts: 153
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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).
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).
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
A class instance is immutable is it has no way to change its datamembers once they're initialised.
42
posted 19 years ago
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 |









