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

A SIMPLE DOUBT REG WRAPPER CLASSES!  RSS feed

 
mara thamizhan
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HAI ALL,
WHAT IS THE USE OF MAKING A PRIMITIVE DATATYPE VALUE TO BE STORED IN A OBJECT AS WE STORE USING WRAPPER CLASSES!

I READ THAT IT WILL ADD FUNCTIONALITY!

CAN U EXPLAIN HOW IT WILL ADD FUNCTIONALITY & IS THERE ANY DRAWBACKS DUE TO THIS?

BYE FOR NOW!

CHEERS,
SAYEE
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,

WELCOME TO JAVARANCH!

HERE, LET ME HELP YOU WITH THAT "CAPS LOCK" KEY. SOMETIMES IF YOU BANG IT WITH YOUR FIST, LIKE THis, it comes unstuck. There! That's better!

I'm not quite sure what you're asking. In Java, the primitive types like "int", "char", "double", "boolean" aren't kinds of objects; as a result, you can't treat them like objects. This is unfortunate, because all of the nice collection classes (the Lists, Sets, Maps, etc) only work with objects.

That's where those "wrapper" classes come in. If you wrap up an "int" inside an "Integer" wrapper object, then you can treat the result as any other object. You can store it in a Collection, or do any of the many other things that only work with Objects. When you need to get the primitive back, there are methods like intValue() to get it out again.

Java 5 -- the newest version of Java -- will often do this kind of wrapping and unwrapping for you automatically. It's called "autoboxing".
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Discussion continued at http://www.coderanch.com/t/404102/java/java/WRAPPER-CLASSES-ADD-FUNCTIONALITY
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!