Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Wrapper Classes and Autoboxing and Unboxing in Java  RSS feed

 
mack mackdavid
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am beginner in java language.I want to know that about Wrapper Classes and Autoboxing and Unboxing in Java.

Thanks
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Java has two kinds of types: primitive types and reference types. There's a limited set of primitive types: byte, short, int, long, boolean, float, double, char

Primitive types are not objects. Things like collection classes (for example ArrayList) can only store objects. To make it possible to store for example simple numbers in an ArrayList, these numbers (of type int, for example) must be wrapped into objects. That's what the wrapper classes are for. For each primtive type, there's a corresponding wrapper class: byte -> java.lang.Byte, short -> java.lang.Short, int -> java.lang.Integer, etc.

Java can automatically convert primitive values to and from instances of the wrapper classes, which makes the syntax more convenient (you don't have to do these conversions yourself all the time).

Oracle has a tutorial about this subject: Autoboxing and Unboxing
 
Adewole Adebayo
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay, major importance of Wrapper class is when developing a GUI application. imagine you need an int from a user via a JTextField? Note: java doesn't have a way to get int from a JTextField like you would get an int from a console directly by using the nextInt() method so they came up with the Class Wrapper for primitive types.. e.g Integer, Float, Double, Boolean e.t.c are all of Wrapper class and to get an int from a JTextField you will write the code like this; int= Integer.parseInt(String);

Software Engineering...
Aptech Education

mack mackdavid wrote:Hi

I am beginner in java language.I want to know that about Wrapper Classes and Autoboxing and Unboxing in Java.

Thanks
 
Campbell Ritchie
Marshal
Posts: 55694
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is not a real use of a wrapper object; it would be just as easy to create a method in the String classThe real need for wrapper objects is that you can create an object representing the value of a primitive.
 
George Macin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When it required ?

When a method is expecting a wrapper class object but the value that is passed as parameter is a primitive type. When primitive value assigned to wrapper class variable or reverse.

Advantages

No need to make object explicitly and wrap the value of primitive type.

Disadvantage

“Autoboxing” is too unexpected in its behavior and can easily result in difficult to recognize errors.

Performance

Autoboxing does create objects which is not clearly visible in the code. So when autoboxing occurs performance suffers.

More about....Autoboxing and Unboxing

George
 
Campbell Ritchie
Marshal
Posts: 55694
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
I hope the previous posters on this thread weren't still waiting for an answer.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!