/    Sign up×
Community /Pin to ProfileBookmark

Java : Object Wrapper

Hi Guys..

i’ve got Home work from my college..
Is anybody know How to convert an Object into primitive Data Type.?!
or from primitive data Type to an Object.?!

e.g (maybe absolutely wrong ? ):

[CODE]
Char x;
x = “Converting Obejct”;

Object convert = new Object();
convert = x;
[/CODE]

Is that possible.?!
Please.. Help me out..!

to post a comment
Java

1 Comments(s)

Copy linkTweet thisAlerts:
@LaurelLynnJul 23.2006 — In Java, for every primative there is a corresponding Object Wrapper Class:

int -> Integer()

long -> Long()

float -> Float()

double -> Double()

char -> Character()

String is already an object to begin with and needs no wrapper.

Note: x = "Converting Obejct";

will not fit in a char as that primative only holds one character.

In your example you would use String instead, and no wrapper would be needed. Or you could use an array 'char[]', which like all arrays is also an object.

If your line had been:

char x = 'c';

Then you could create an object like so:

Character c = new Character( x );

And back:

x = c.charValue();

The wrapper classes have methods to convert back to the various primative types as well as to a String.

One of the real pains in Java is the need to keep converting back and forth when using the various Collection classes.

Hope that helps, Laurel ?
×

Success!

Help @pocky spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...