• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Wrapper Classes and Autoboxing and Unboxing in Java

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
I hope the previous posters on this thread weren't still waiting for an answer.
 
reply
    Bookmark Topic Watch Topic
  • New Topic