This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
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


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: 32481
    
214

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


[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, 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: 20029
    
  44

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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: uninitialized local variables.
 
It's not a secret anymore!