Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Question on String Immutability

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ,

I have a question on immutability from chapter 2 from  

OCA: Oracle Certified Associate Java SE 8 Programmer I Study Guide: Exam 1Z0-808

. The example :

line 1    String s1 = "1";
line 2    String s2 = s1.concat("2");
line 3    s2.concat("3");
line 4    System.out.println(s2);

prints  12

but if instead if line 3 is s2 = s2.concat("3") . the result would be 123 . how does it make difference ? A bit confused on this .


Thanks ,
Liza

 
Ranch Hand
Posts: 312
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Liza,
In making the assignment, you are "recording" the (result of the) concatenation [s2 = s2.concat("3")], rather than immediately discarding it [s2.concat("3")] since its reference is "not recorded".
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is trying to see if you know that String objects are immutable.The reference varies when it is adopted.
String a="A";
a.concat("B");
System.out.print(a); A
String b=a.concat("C");
System.out.println(b); AC

---------------------------------------------------------------

StringBuilder objects are mutable.

StringBuilder sb=new StringBuilder("A");
sb.append("B");

System.out.println(sb);  //AB
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
concat method returned the concated string.
Example :
    String a = "abc";
    a.concat("D");  //after executing , still 'a' is "abc" but this statement return "abcD".
    String b = a.concat("E"); //after excuting , still 'a' is "abc" . value of b is "abcE" .
 
 
Marshal
Posts: 69894
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yuresh Karunanayake wrote:concat method returned the concated string. . . .

Yes, that is correct, and welcome to the Ranch
 
Liza Sachdev
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for reply !!
 
Ranch Hand
Posts: 84
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liza Sachdev wrote:hi ,

I have a question on immutability from chapter 2 from  

OCA: Oracle Certified Associate Java SE 8 Programmer I Study Guide: Exam 1Z0-808

. The example :

line 1    String s1 = "1";
line 2    String s2 = s1.concat("2");
line 3    s2.concat("3");
line 4    System.out.println(s2);

prints  12

but if instead if line 3 is s2 = s2.concat("3") . the result would be 123 . how does it make difference ? A bit confused on this .


Thanks ,
Liza

in strings if you concat it will create a new string object that  ref value has to be assigned to the new reference that is string s2  if not assign it will discarded and original string will printed
 
Do not meddle in the affairs of dragons - for you are crunchy and good with ketchup. Crunchy tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic