aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes uninitialized local variables. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "uninitialized local variables." Watch "uninitialized local variables." New topic
Author

uninitialized local variables.

srikanth bhadragiri
Greenhorn

Joined: May 09, 2012
Posts: 2
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

Joined: May 26, 2003
Posts: 30758
    
156

Boolean.TRUE is an object and true is a primitive. Java takes care of autoboxing between the two for you.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
srikanth bhadragiri
Greenhorn

Joined: May 09, 2012
Posts: 2
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: uninitialized local variables.