• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Doubt about final....

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got this question in Dan's mock exam....
class Blue {
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);
}
}
The result of this program is true, false, false.
But if we change a and b to final, then the result is true, true, true. (This is true even if final modifier is removed from c and d).
Could anybody please explain the reason behind this behaviour?
Thanks for the help in advance.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

But if we change a and b to final, then the result is true, true, true. (This is true even if final modifier is removed from c and d).
Could anybody please explain the reason behind this behaviour?


The final strings expressions are determined at compile time, not runtime.
Because strings a and b are final the compiler knows they cannot be re-assigned, so they get a fixed reference to a string object.
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Vidya Ram:
But if we change a and b to final, then the result is true, true, true. (This is true even if final modifier is removed from c and d).
Could anybody please explain the reason behind this behaviour?
Thanks for the help in advance.


This is a trick question and you have pointed out the trick. The String variables c and d are declared final, but their values must be evaluated at runtime because a and b are not final. Since c and d are evaluated at runtime a new instance must be created for each so the reference values for c and d are not the same and the equality operator, ==, returns false.
If a and b were both final then the values of c and d could have been evaluated at compile time and both could be represented by one instance of a String constant. The reference values of c and d would then be equal and the equality operator would return true.
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the expression ((a+b)==(a+b)) is evaluated, does that mean that each (a+b) is a sort of unamed temporary reference variable pointing at the value of a+b ?
That would make sense to me because then, if a and b are final, ((a+b)==(a+b)) evaluates to true because both are represented by the same string const.
Am I way off track ?
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ravi Varman:
When the expression ((a+b)==(a+b)) is evaluated, does that mean that each (a+b) is a sort of unamed temporary reference variable pointing at the value of a+b ?


Yes, since a and b are not final then each (a+b) is evaluated at runtime and each results in a new instance of a String.
 
Vidya Ram
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all. The things are very clear to me now.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!