aspose file tools*
The moose likes Beginning Java and the fly likes primitive types not castable as lang.Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "primitive types not castable as lang.Object" Watch "primitive types not castable as lang.Object" New topic
Author

primitive types not castable as lang.Object

Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 761

This statement is dead on obvious but my problem is:
I need to call this method:
someMethod (java.lang.Class type, java.lang.Object value)

Scenario 1: (Works)
boolean bP = true
Boolean bW = new Boolean(bP);
someMethod( Boolean.class, bW ); //ok
Scenario 2: (Compile error)
someMethod( boolean.class, bP ); //error
Is there any way to coerce scenario #2 to work?
Pho

Regards,

Pho
Grant Crofton
Ranch Hand

Joined: Nov 08, 2000
Posts: 154
No, if the method wants an Object, the method'll have an Object.
You could shorten it slightly with someMethod( Boolean.class, new Boolean( bP ) );
What's wrong with doing it the first way?
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Solution one is the way you coerce it to work. The problem you are describing is the reason for the existence of the wrapper classes that provide object representations of primitive types.
Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 761

Well if you think about it,
boolean.class is a subclass of "Class".
and "Class" is a subclass of "Object".
So does it mean that the relationships are not transitive ?
Pho
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

there is no Class called boolean only one called Boolean


SCJP
Visit my download page
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
That's why they are called "primitives". Because they are NOT objects. when you wrapped the boolean primitive with a Boolean object wrapper it worked.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
I know this is of no importance here but... In C#, primitives can be treated as objects transparently.


Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Tony. . . naughty, naughty - reading up on microsoft stuff when no one is looking .
So are they really primitives and the language disguises that (I think not) or did they make everything a true object??? It is really just the primitives, the special handling of strings, and arrays that prevent java from being a pure object oriented language. How many concessions did C# make???
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: primitive types not castable as lang.Object