• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

are wrapper classes intialized to zero?

 
archu sweet
Ranch Hand
Posts: 66
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if we declare Integer i;

does i get intialized to zero???
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm... If only there were some way to test this.
 
kumarjit banerjee
Ranch Hand
Posts: 32
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15205
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why classes like "Integer" called wrapper?What is the reason?
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because they wrap around a primitive. That way you can store primitives in a Collection.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic