• 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:

Why strange output of this String question

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi folks !
please help me out to understand this question

Code First :
class StringDemo
{public static void main(String ss[])
{String s= "HELLO";
s.toLowerCase();
System.out.println(s);
}
}
its output is : HELLO

Code Second :
class StringDemo
{public static void main(String ss[])
{String s= "HELLO";
System.out.println(s.toLowerCase();

}
}
its output is : hello

why o/p is different in both cases, as we are not changing the reference to newly created string object
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String Object is immutable. The "s.toLowerCase()" will return a new String object but the string referenced by s will not change.
[ May 11, 2006: Message edited by: wise owen ]
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read this thread for more detail.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

System.out.println will print the returning value of the method.
in this case s.toLowerCase() is returning a new String and it is being printed in the console....

plz correct if i am wrong

regards
krishna bulusu
 
jerry sharma
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi wise !

where the returned object by the method s.toLowerCase() in code A: goes

as in both cases references is not being cjanged
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The created String Object will be lost because not variable to reference it.
This is portion of code in my previous link. It show the process step by step.


Example:

String s = "XYZ"; //1. a object created

s.concat("ABC"); //now a fresh copy of XYZ created and "ABC" to it and the
//reference to XYZABC returned, but it get lost
//as you are not assiging reference to it.
System.out.println(s); // its "XYZ"

now lets see if you write:

System.out.println(s.concat("ABC")); //it will print XYZABC but the s=XYZ

if you write:
s = s.concat("ABC"); //then s=XYZABC

 
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup,what u need to do is this:
s=s.toLowerCase();
 
my overalls have superpowers - they repel people who think fashion is important. 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