• 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

uninitialized local variables.

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic