aspose file tools*
The moose likes Java in General and the fly likes why sometime we need to initialize integer variable ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "why sometime we need to initialize integer variable ?" Watch "why sometime we need to initialize integer variable ?" New topic
Author

why sometime we need to initialize integer variable ?

Alvin chew
Ranch Hand

Joined: Jan 08, 2004
Posts: 834
hi, wondering why sometime we need to initialize the integer variable but sometime not ?

example :


can somebody pls explain why is it so i must initialize num2 ? thank you very much for your time
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
Class and instance variables are automatically given a starting value when the class is loaded or the instance is created. Variables declared inside a method are not.
Think about what the += operator does - it takes the existing value in a variable and adds to it. Therefore the compiler watches out for the possibility that num2 does not have a starting value assigned.
Automatically zeroing memory that a new object is going to occupy is part of the built-in security design of Java. In languages that don't do this, it is possible to grab a large hunk of memory that might have been used for data by a previous application and fish around in it for things like passwords.
Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why sometime we need to initialize integer variable ?