File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes are wrapper classes intialized to zero? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "are wrapper classes intialized to zero?" Watch "are wrapper classes intialized to zero?" New topic

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

Joined: Apr 06, 2010
Posts: 4544

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

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
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

Now consider the below method of java.lang.String

This provides the solution to the query.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15093

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 8 API documentation
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
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.
I agree. Here's the link:
subject: are wrapper classes intialized to zero?
It's not a secret anymore!