• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

primitive types not castable as lang.Object

 
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there is no Class called boolean only one called Boolean
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know this is of no importance here but... In C#, primitives can be treated as objects transparently.
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
There's a way to do it better - find it. -Edison. A better tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic