• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Strings are immutable

 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

How strings are immutable when compared with integers?

But we are changing the string values like
String s = "abc"

s= s.concat("de");

Thnaks
 
Ranch Hand
Posts: 218
VI Editor Ruby Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because you are not changing the string that is referenced by 's', but you are replacing the 's' with the reference of a new object that is resulted by s.concat('de').

Does that make sense?

In ASCII art:

Orig:

s -----> "abc"

Then:

[ August 03, 2008: Message edited by: Wirianto Djunaidi ]
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here value of String object (abc)is not changing . Only the reference s indicating seperate String object.We just lost the reference to object abc which was earlier indicating by s.
For this reason if you try with just
s.concat("de");
System.out.println(s);
it will only print abc .
 
sumaraghavi ragha
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thaks for your reply!!

But can you please tell me why it happens to integers??
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[I assume you are referring to int (the primitive type) and not Integer (the wrapper object. In that case...]

An int variable, holds the actual int value, not a reference to an object on the heap. In the examples above, the int is equivalent to the object reference, not the object itself. So just as the bit pattern representing the object reference changed when you assigned a reference to a new string, the bit pattern representing the int value changes when you assign a different int value to the variable.
 
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there are two types of integers, one is the primitive int and the other is the Wrapper class called Integer.

The Wrapper class behaves just like the other class in Java and it is stored in heap. The explanation mentioned above also stands true for wrapper class. When you try to change a value, internally a new object is created.
 
sumaraghavi ragha
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then can i say all wrapper classes are immutable??
 
Master Rancher
Posts: 5060
81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not really. The term "wrapper" isn't formally defined anywhere I know of, and it's entirely possible that someone might use the term in a sense that includes mutable objects. For example, a java.util.Date can be thought of as a wrapper around a long. But for some unknown reason (most likely, someone just screwed up) a Date is mutable.

On the other hand, as far as I can see, all the wrapper classes in the java.lang package are immutable. Those are the ones involved in autoboxing and unboxing. If that's what you meant, then yes. But it's not really true in general.
 
sumaraghavi ragha
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thans Mike and all others it's helped
 
She still doesn't approve of my superhero lifestyle. Or this shameless plug:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic