| Author |
are wrapper classes intialized to zero?
|
archu sweet
Ranch Hand
Joined: Mar 07, 2011
Posts: 66
|
|
if we declare Integer i;
does i get intialized to zero???
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3791
|
|
|
No. Because it's a reference type, it gets initialised to null. Assuming you're talking about a member variable, that is - variables declared in methods aren't initialised to anything.
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
Hmmm... If only there were some way to test this.
|
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
|
 |
kumarjit banerjee
Ranch Hand
Joined: Mar 27, 2011
Posts: 32
|
|
archu sweet wrote:if we declare Integer i;
does i get intialized to zero???
Consider the following example
The output of the program is null. The reason is that when Integer i; is declared the reference variable i is initialized to null since it is an instance variable. Now the obvious question is that the program should throw a NullPointerException but this not the case.
System.out.println(i); invokes the below overloaded version of println of the class java.io.PrintStream.
Now consider the below method of java.lang.String
This provides the solution to the query.
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12911
|
|
Kumarjit: Thanks for your answer. Please UseCodeTags when you post source code - as you can see, the code will be formatted in a much more readable way when you use them.
Anchu: As you can see, you could have checked this out yourself by writing a small program. Try writing lots of little programs to test out features of Java - trying things out yourself is one of the best ways to learn.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
abalfazl hossein
Ranch Hand
Joined: Sep 06, 2007
Posts: 602
|
|
|
Why classes like "Integer" called wrapper?What is the reason?
|
 |
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
|
|
|
Because they wrap around a primitive. That way you can store primitives in a Collection.
|
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
|
 |
 |
|
|
subject: are wrapper classes intialized to zero?
|
|
|