primitive acting like a Object
This makes no logical sense to me how can I ask java if a int is a object it wont compile so basically no but when I say here use it as a object its like oh ya for sure no problem.
1. Can anyone explain to my why this happens Its harder to remember for the SCJP when it makes not logical sense to me?
2. Does anyone else feel like this logic is at least kinda screwed up?
Code:
public class Test {
public static void main(String[] arg){
int myInt = 5;
objectTest(myInt); //this is OK
//Systemout.println(myInt instanceof Object ); <----- but this doesn't compile????????? Wat
}
public static void objectTest(Object o){
System.out.println("int counted as a object");
}
}
Re: primitive acting like a Object
It is all well defined in the JLS or Java Language Specification:
15.20.2. Type Comparison Operator instanceof: The type of the RelationalExpression operand of the instanceof operator must be a reference type or the null type; otherwise, a compile-time error occurs.
5.3. Method Invocation Conversion: Method invocation contexts allow the use of one of the following: ... a boxing conversion (§5.1.7) optionally followed by widening reference conversion...
Re: primitive acting like a Object
ok I think i can accept that sounds like int is not a object but this boxing conversion you speak of converts it to Integer which is one and instanceof is referenced based so no conversion happens or at least something along that line. Well thank you for your help.
although I still think its kinda dumb but at least there is logic behind it.
Re: primitive acting like a Object
thanks for taking the time by the way to help me out.
Re: primitive acting like a Object
Quote:
Originally Posted by
killutch
ok I think i can accept that sounds like int is not a object but this boxing conversion you speak of converts it to Integer which is one and instanceof is referenced based so no conversion happens or at least something along that line. Well thank you for your help.
You're welcome!
Quote:
although I still think its kinda dumb but at least there is logic behind it.
Myself, having not yet written a programming language, I don't feel wise enough to question the logic of these decisions. Their decisions may have something to do with the fact that boxing and un-boxing haven't always been a part of Java, and implementing this well without breaking pre-existing code is not an easy thing to do.
I also commend your efforts to try to understand this well.
Re: primitive acting like a Object
Quote:
Originally Posted by
Fubarable
Myself, having not yet written a programming language, I don't feel wise enough to question the logic of these decisions.
ultimately your right I don't know what challenges they face for all I know box conversion could be as useful as polymorphism therefor way over justifying something that seems "inconsistent" in the language. I should probably calm down a little bit when finding something like this again.
It kinda reminded me of this Destroy All Software: WAT - YouTube