• 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

final String

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all
i came accross the following question in one of the mock tests of Dan Chisholm:

class test{
public static void main(String args[]) {
String a = "A";
String b = "B";
final String c = a+b;
final String d = a+b;
System.out.print((c==c) + ",");
System.out.print(((a+b)==(a+b)) + ",");
System.out.print(c==d);
}
}
Prints: true,false,false
Now if both the variables a & b are declared as final, then why the output changes to
true,true,true

Thanx and Regards
Indraneel
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Indraneel Das:
Hi all
class test{
public static void main(String args[]) {
String a = "A";
String b = "B";
final String c = a+b;
final String d = a+b;
System.out.print((c==c) + ",");
System.out.print(((a+b)==(a+b)) + ",");
System.out.print(c==d);
}
}
Indraneel


a+b evaluates at runtime.so, everytime u execute a+b it returns new string object. that is why (a+b)=(a+b) prints false.
But, in the below given code a and b are declared final.So, a+b evaluates at compile time and hence (a+b)=(a+b) prints true.

hope it helps.
regds
Arpana
 
Indraneel Das
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thx Arpana for clarifying my doubt. Now its clear to me.
Regards
Indraneel
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given that a+b evaluates at runtime, does the JVM check the String pool to see if the result of the runtime calculation already exists or does it always return a new String?
 
Arpana Rai
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by John Pritchard:
Given that a+b evaluates at runtime, does the JVM check the String pool to see if the result of the runtime calculation already exists or does it always return a new String?


At runtime a+b (if a and b are not final ) returns new String object and the String constructor does not check the String pool when it creates a new String object.
regds
arpana
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic