• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

On String.

 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String s1 = "abc";
String s2 = "def";
s2 = s1 + s2;

what is the value of s2?
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would "abcdef".
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simple questions like this one can very quicly be answered by writing a small test program and compiling and running it. You'll most likely have an answer quicker if you do that instead of asking it in the forum, and you also don't have to waste other people's time.

Experimenting with Java is a great way of learning it.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey any ways,
Just to make the question lil bit interesting...

String s1 = "abc";
String s2 = "def";
String s3 = "abcdef";
String s4 = s1+s2;
String s5 = new String("abcdef");

What is s3 == s4
and s3 == s5 True or False?
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gaurav Joshi:
Hey any ways,
Just to make the question lil bit interesting...

String s1 = "abc";
String s2 = "def";
String s3 = "abcdef";
String s4 = s1+s2;
String s5 = new String("abcdef");

What is s3 == s4
and s3 == s5 True or False?




Hi Gaurav,
s3==s4 and s4 == s5 both will return false, because s4 brings the concatinated String of s1 and s2.
Similarly, you create a new String object s5 with the same value of s1+s2; which is again "abcdef".

A more important thing to understand is when you write s3 == s4, then two different String references are compared, even though their values are same.

Regards,
Vaibhav Goel
 
Gaurav Joshi
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String s1 = "abc";
String s2 = "def";
String s3 = "abcdef";
String s4 = s1+s2;
String s5 = new String("abcdef");

String s6 = "abc"+"def"

What is s3 == s4
and s3 == s5
Now what is s3==s6 True or False?
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String s6 = "abc"+"def"

Can somebody tell me in the above code if a new String object is created.
[ March 27, 2008: Message edited by: sridhar row ]
 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String s6 = "abc"+"def"

The above concatenation is done at compile time and hence JVM sees it just like our s3 variable and they both are got from the Literal pool (Flyweight design pattern) and hence s3 == s6 will return True.
 
Sandeep Bhandari
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
StringBuffer s8 = new StringBuffer("abcdef");

what will be the result of

s3.equals(s8);
True or False?

Note: equals() method in String class checks for the equality of characters
 
Oh. Hi guys! Look at this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic