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

String Literal

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far my understanding String literals are created in a heap.

public class MAIN {

String s1 = "I am a string";
String s2 = s1;
String s3 = "I am a String";

String x1="abc";
String x2=x1;
String x3="abc";
public static void main(String[] args) {

MAIN m = new MAIN();
System.out.println("s1 == s2 : " + m.s1==m.s2);//1
System.out.println("s1 == s3 : " + m.s1==m.s3);//2
System.out.println("s2 == s3 : " + m.s2==m.s3);//3

System.out.println(m.x1==m.x2);//4
System.out.println(m.x1==m.x3);//5
System.out.println(m.x2==m.x3);//6


}
}
I think answer should be true for line 1 2 3 as it will point to same String value "I am String" .
But when i tried to compile it ,it gives false for line 1 2 3.

And if I make my String literal as final will they still point to same "I am String".

And I think that line 1 2 3 are exactly doing same as line 4 5 6 ,what is the difference ???

ANS:

line 1 2 3 --flase
line 4 5 6 --true

Please explain where I am going wrong?
[ January 22, 2007: Message edited by: Kasak Tahilramani ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first statement should print "true", as the two variables as assigned to the same object -- this has nothing to do with Strings and would be the same for any kind of object.

But the second two should print "false" because your two String literals aren't the same -- they differ in capitalization. If the two Strings were exactly the same, then the compiler would notice that, and use a single String object to represent them both, so all three lines would print "true".
 
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kasak Tahilramani:
As far my understanding String literals are created in a heap.

public class MAIN {

String s1 = "I am a string";
String s2 = s1;
String s3 = "I am a String";

String x1="abc";
String x2=x1;
String x3="abc";
public static void main(String[] args) {

MAIN m = new MAIN();
System.out.println("s1 == s2 : " + m.s1==m.s2);//1
System.out.println("s1 == s3 : " + m.s1==m.s3);//2
System.out.println("s2 == s3 : " + m.s2==m.s3);//3

System.out.println(m.x1==m.x2);//4
System.out.println(m.x1==m.x3);//5
System.out.println(m.x2==m.x3);//6


}
}
I think answer should be true for line 1 2 3 as it will point to same String value "I am String" .
But when i tried to compile it ,it gives false for line 1 2 3.

And if I make my String literal as final will they still point to same "I am String".

And I think that line 1 2 3 are exactly doing same as line 4 5 6 ,what is the difference ???

ANS:

line 1 2 3 --flase
line 4 5 6 --true

Please explain where I am going wrong?

[ January 22, 2007: Message edited by: Kasak Tahilramani ]



I was going to mention the capitals but that has been addressed in the previous post. I have compilled and ran the code above and the results are exactly as you have stated. I have no idea why the first line does not evaluate to true because it definately should as far as I can tell. Any ideas anyone?

Mark.
[ January 22, 2007: Message edited by: Mark Smyth ]
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The + operator takes precedence over the ==.
If you write the print statements as below you will get the correct answers.

System.out.println("s1 == s2 : " + (m.s1==m.s2));
 
victor kamat
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In general the arithmetic +, -, *, /, % etc take precedence over the boolean operators
 
Mark Smyth
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by victor kamat:
The + operator takes precedence over the ==.
If you write the print statements as below you will get the correct answers.

System.out.println("s1 == s2 : " + (m.s1==m.s2));



That is one tricky question, I had a feeling it was something simple like this.
 
Kasak Tahilramani
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot victor .That worked !!!

If I have :

String s4="s1 == s2 : " + m.s1;
System.out.println(s4==m.s2);

Will the output will change???
Does marking making String literal as final add any effect to the output???
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The output would not change even if you introduce a new variable s4.

s4 would be equal to "s1 == s2 : I am a string" when it is initialised. It stored as separate object in heap and hence it is not equal to s4

Marking a variable final ensures that the value doesnt get modified. I guess it would n't affect the outcome.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic