Name the mutable and immutable Classes
posted 18 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Can any body write in detail about the Mutable and Immutable classes that are commonly used. in Java and important wrt SCJP1.5
posted 18 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
As I know String and all the wrapper classes are immutable.
Rashid Mian
Ranch Hand
Posts: 31
posted 18 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Wrapper class are immutable? Can you explain with examples
posted 18 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
What does it say in whatever book you are using to study for SCJP?
Have you looked at the java.lang package classes in the Java API?
Have you looked at the java.lang package classes in the Java API?
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
posted 18 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
String(immutable) : Once we have assigned a value can never be change.It is immutable.
For example, we have String s = �abcdef�; //Create a new String
object,with value �abcdef� refers to it.
String s2 = s;//Create a 2nd reference variable
referring to the same thing.
s = s.contact(�morestuff�);//Create a new String
object,with value �abcdef morestuff� , refers
to it.
(Change s�s reference from the old string to
the new String.(Remember s2 is still referring to the original �abcdef� String.
StringBuffer(mutable): StringBuffer objects are changeable.It is mutable.
For example, StringBuffer sb = new StringBuffer(�abc�);
sb.append(�def�);
System.out.println(sb);
In this case the output will be �abcdef�.
regards
SADASIVAKUMAR UTTI
For example, we have String s = �abcdef�; //Create a new String
object,with value �abcdef� refers to it.
String s2 = s;//Create a 2nd reference variable
referring to the same thing.
s = s.contact(�morestuff�);//Create a new String
object,with value �abcdef morestuff� , refers
to it.
(Change s�s reference from the old string to
the new String.(Remember s2 is still referring to the original �abcdef� String.
StringBuffer(mutable): StringBuffer objects are changeable.It is mutable.
For example, StringBuffer sb = new StringBuffer(�abc�);
sb.append(�def�);
System.out.println(sb);
In this case the output will be �abcdef�.
regards
SADASIVAKUMAR UTTI
SADASIVAKUMAR UTTI, SCJP1.4
A bend in the road is not the end of the road ... unless you fail to make the turn.
posted 18 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
The wrapper classes for the primitives are immutable (Integer, Long, Character, etc.).
Wrapper classes per se are a concept that is orthogonal to mutability (i.e., they can be mutable or immutable).
Wrapper classes per se are a concept that is orthogonal to mutability (i.e., they can be mutable or immutable).
posted 17 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi all,
how i will know whether class is mutable or not by seeing java api .
and how to create a mutable class of my own.
Thanks,
prakash
how i will know whether class is mutable or not by seeing java api .
and how to create a mutable class of my own.
Thanks,
prakash
posted 17 years ago
By using your memory? Vishal's first answer, as modified by Ulf, is probably all you need to know. You need to know what the primitive wrapper classes are anyway, because they come up in other parts of the test objectives (like autoboxing)
[ March 06, 2008: Message edited by: Mike Simmons ]
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Originally posted by siva prakash:
how i will know whether class is mutable or not by seeing java api .
By using your memory? Vishal's first answer, as modified by Ulf, is probably all you need to know. You need to know what the primitive wrapper classes are anyway, because they come up in other parts of the test objectives (like autoboxing)
[ March 06, 2008: Message edited by: Mike Simmons ]
Mike Simmons
Rancher
Posts: 5252
85
posted 17 years ago
Do you know how to create any class on your own?
Do you know what a mutable class is?
As Barry said above, what does it say in whatever book you are using to study for the SCJP?
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Originally posted by siva prakash:
and how to create a mutable class of my own.
Do you know how to create any class on your own?
Do you know what a mutable class is?
As Barry said above, what does it say in whatever book you are using to study for the SCJP?
posted 17 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Why would you want to write a mutable class? 

posted 13 years ago
if want to perform any modification on existing object with those changes a new Object will be created this concept is nothing but immutability......
sample code is
final public class MyImmutable{
private int var1=0;
public MyImmutable(int var1){
this.var1=var1;
}
public MyImmutable getModify(int var1){
if(this.var1==var1){return this;}
else
return new MyImmutable(var1);
}
}
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Tim Weide wrote:Why would you want to write a mutable class?
if want to perform any modification on existing object with those changes a new Object will be created this concept is nothing but immutability......
sample code is
final public class MyImmutable{
private int var1=0;
public MyImmutable(int var1){
this.var1=var1;
}
public MyImmutable getModify(int var1){
if(this.var1==var1){return this;}
else
return new MyImmutable(var1);
}
}
| What's that smell? I think this tiny ad may have stepped in something. The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |






