aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes java data types initializing 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 "java data types initializing" Watch "java data types initializing" New topic
Author

java data types initializing

thejaka samarakoon
Ranch Hand

Joined: Jul 20, 2010
Posts: 56
I have a problem in when primitive data types initialized.




this will give the output or error as the variable y is getting read before being properly initialized.but in this below code the variables got initialized to 0,what is the difference here.can anyone explain?


Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Local variables (variables declared inside methods) are not automatically initialized. Fields declared at class level are automatically initialized to the default value of their datatype...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Sridhar Gudipalli
Ranch Hand

Joined: Nov 02, 2005
Posts: 120
This may help!!


Sridhar Gudipalli|SCJP 6.0
SCWCD objectives
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066



The local variable should be initialized before they are used. The compiler will evaluate this value in all possible ways, that it thinks. And since we know that, in the above code, you initialized the variable y in all logically possible way with a poor if conditions, that's way it flags Compilation Error, saying that the variable y may not be initialized, if you change the as follows, you won't get Compilation Error. because you initialized the variable y in all the possible ways, that it thinks!








|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Mahmoud Hossam
Ranch Hand

Joined: Feb 20, 2010
Posts: 74

the compiler can't know for sure if the If-statements are going to execute,so it thinks that y is uninitialized
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java data types initializing
 
Similar Threads
properly initialized
Comparision
small swapping problem please please reply!!!!!
initialization and if
Method Variable