• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

compare a String created at runtime? pls help

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not understand why line 2 compare a String created at runtime?
1. String java = "java", va = "va";
2. System.out.println(java == "ja"+va);
please help me to understand it,
thanks for help,
tg
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by tom gong:
I do not understand why line 2 compare a String created at runtime?
1. String java = "java", va = "va";
2. System.out.println(java == "ja"+va);
please help me to understand it,
thanks for help,
tg


In line 1 you create a string in the String pool area, but in line 2, when you use "+", you actually create a new object and that object is not in the string pool area.Although the contents of two objects are same, their memory address are different. Therefore line 2 should print false
Kai

[This message has been edited by Kai Li (edited July 23, 2000).]
 
tom gong
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

my question is regarding compile time vs runtime.
the orginal question was from Duncan Anderson's book published from syngress as following :
why is output?
1. class test{
2. public static main (String []ags)
3. {
4. String java ="java", ja="ja";
5. System.out.println(java == "java");//true
6. System.out.println(java == "ja"+"va");//true
7. System.out.println(java == "ja"+ va);//false
8. }
9. }
for line 5 and 6 output is true, I understand.
but I do not understand line 7, the output is false, because based on book, line 6 compare a String created at compile time, line 7 compare a String created at runtime.
can anybody tell me why line 7 compare a String created at runtime?
sorry about this question,
tg
 
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
let me try to help u.
In some previous discussion jim has provided a link to JLS (here <http://java.sun.com/docs/books/jls/html/3.doc.html>;). So if u go there and read u will find following statement.

Strings computed by constant expression (�15.27) are computed at compile time and then treated as if they were literal.

If we go through the following example
String a = "Bill"; // String constant
String b = "Gates"; // String constant
String b = "Bill" + "Gates"; // String constant
String d = a + b; // not a String constant
See last statement is not a String literal because it is not a String constant expression.Result is not known at compile time because it is based on variables.
However if we change the example little bit and make both the variables as final variable
final String a = "Bill"; // String constant
final String b = "Gates"; // String constant
String d = a + b; // this *is* a String constant
See Now last statement is a String literal because now it is a String constant expression. Result is known at compile time because it is based on final variables.
Hope this will help u. feel free to correct me any time.
Regards
Vivek
 
tom gong
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but......., sorry I am kind of slow.
look at your code, first example.
1. String a = "Bill";
2. String b = "Gates";
3. String b = "Bill" + "Gates";
4. String d = a + b;
when compiler compile to line 4, compiler already know that a = "Bill" and b = "Gates", so Result is known at compile
time. d can be asigned at compile time.
thank again for your help,
tg
 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Compiler does not keep track of where variable is being used. so value of variable might be the same or some other expression has changed it. but if u declare a variable as final now it is for sure that u can't change the vale of same variable somewhere else.
Hope i am able to clear my point. i would love to hear from someone who can explain it in better ways.
vivek
[This message has been edited by Vivek Shrivastava (edited July 24, 2000).]
reply
    Bookmark Topic Watch Topic
  • New Topic