• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

More Code Concepts...

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the output of the following code?
1: String str = "Welcome";
2:
3: str.concat(" to Java!");
4:
5: System.out.println(str);
A) Strings are immutable, compilation error at line 3.
B) Strings are immutable, runtime exception at line 3.
C) Prints "Welcome".
D) Prints "Welcome to Java!".
I had read that concat method is for strings so whats the output of the above code??
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not to be tough but
What's the answer given?? What do you think the answer is??? Or what u get when u try to compile and run this piece??

Please participate and contribute your thoughts first
Prabhu.
[This message has been edited by Prabhu (edited May 17, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer given is C but i thought that one can concatenate with strings, however considering that strings are immutable than we cannot use the concat...Is this the logic?
One more thing, i am not compiling each code...rather doing it more theoretically, Is it the right approach? or should i also compile and see the outputs? and then deduce etc.
ooops! i just compiled and found the answer...that was quite a simple problem..and i guess one should compile!!to have a real understanding.
one last thing: line 3 cannot be used alone and has to be assigned to str to make it work for the compiler! right?
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because Strings are immutable, all string methods are non-destructive
Any seemingly mutable operation on String ( concat, +=, trim, toUpperCase, toLowerCase. etc ) actually returns a reference to the new string object. Obviously, they don't affect the object on which the method is called.
In your example, the .concat method is returning a new string, but you are not storing it anywhere. The original String str continues to preserve its original value.
Hope this clarifies,
Ajith
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
in simpler words if u would have said
str=str.concat("to java");
the u would have got D as u r answer

lets say
& means address of the reference
* means object to which it is pointing to

String str = "Welcome";
// str &200 "welcome" address is *300
str.concat(" to Java!");
// a new object is created but without assignment to any reference
System.out.println(str);
//str still contains reference to object stored at *300
hope this helps
 
I do some of my very best work in water. Like this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic