Results 1 to 6 of 6
Thread: primitive acting like a Object
- 09-15-2012, 05:24 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 70
- Rep Power
- 0
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?
Java 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...
- 09-15-2012, 05:55 AM #3
Member
- Join Date
- Sep 2012
- Posts
- 70
- Rep Power
- 0
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.
- 09-15-2012, 05:59 AM #4
Member
- Join Date
- Sep 2012
- Posts
- 70
- Rep Power
- 0
Re: primitive acting like a Object
thanks for taking the time by the way to help me out.
-
Re: primitive acting like a Object
You're welcome!
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.although I still think its kinda dumb but at least there is logic behind it.
I also commend your efforts to try to understand this well.
- 09-15-2012, 07:59 PM #6
Member
- Join Date
- Sep 2012
- Posts
- 70
- Rep Power
- 0
Re: primitive acting like a Object
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
Similar Threads
-
Text Fields Acting Up
By Tohron in forum Java AppletsReplies: 5Last Post: 09-05-2012, 06:14 AM -
Sphinx 4 Voice Recognition acting WEIRD!
By treeface99 in forum Advanced JavaReplies: 3Last Post: 04-16-2012, 05:39 PM -
Converting primitive int to Intger object?
By NewbieDan in forum New To JavaReplies: 7Last Post: 02-15-2012, 12:33 PM -
actionPerformed acting strangely
By Fortu in forum New To JavaReplies: 18Last Post: 04-10-2011, 04:07 AM -
Acting Java as if it's C problem
By reis3k in forum New To JavaReplies: 13Last Post: 10-18-2010, 09:10 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks