• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

uninitialized local variables.

 
srikanth bhadragiri
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trying to retrieve values from uninitialized local variables throw compiler error.

The below code snippet throw compiler error for usage of 'price' in SOP. This is fine.

-------------------- Code Snippet ------------------------------

public static void main(String[] args){

int price;

if(Boolean.TRUE){
price = 100;
}

System.out.println(price);
}

---------------------------------------------------------------------------------
Replace
if(Boolean.TRUE){ .... }
with
if(true){ .... }
the code compiles and SOP displays 100 as output.

Q1) How is literal 'true' different from 'Boolean.TRUE' ?
Q2) How does the compiler treat 'true' and 'Boolean.TRUE' ?


Srikanth
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34237
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Boolean.TRUE is an object and true is a primitive. Java takes care of autoboxing between the two for you.
 
srikanth bhadragiri
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Boolean.TRUE is an object and true is a primitive. Java takes care of autoboxing between the two for you.


Thanks for reply. I know about autoboxing in Java, but in this scenario replace if(Boolean.TRUE) with if(true). The result is different.

My question is why does compiler throw error in below first code snippet "if(Boolean.TRUE){}" and not in second code snippet "if(true){}"?
Is it something like autoboxing works only during runtime and not during compile time?

------------
// compiler throws error : price is not initialized
public static void main(String[] args){
int price;
if(Boolean.TRUE){ price = 100; }
System.out.println(price);
}
------------
// No compiler error
public static void main(String[] args){
int price;
if(true){ price = 100; }
System.out.println(price);
}
 
Henry Wong
author
Marshal
Pie
Posts: 21024
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
srikanth bhadragiri wrote:
Jeanne Boyarsky wrote:Boolean.TRUE is an object and true is a primitive. Java takes care of autoboxing between the two for you.


Thanks for reply. I know about autoboxing in Java, but in this scenario replace if(Boolean.TRUE) with if(true). The result is different.

My question is why does compiler throw error in below first code snippet "if(Boolean.TRUE){}" and not in second code snippet "if(true){}"?
Is it something like autoboxing works only during runtime and not during compile time?

------------

------------



A boolean true literal is a compile time constant. A Boolean.TRUE, actually any Boolean instance, is *not* a compile time constant. In the second case, the compiler is able to determine that the condition will always run. And in the second case, the compiler is not able to make the same determination, hence, can't determined that it is guaranteed to be executed.

Henry
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic