Results 1 to 6 of 6
  1. #1
    killutch is offline Member
    Join Date
    Sep 2012
    Posts
    70
    Rep Power
    0

    Default 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");
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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...

  3. #3
    killutch is offline Member
    Join Date
    Sep 2012
    Posts
    70
    Rep Power
    0

    Default 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.

  4. #4
    killutch is offline Member
    Join Date
    Sep 2012
    Posts
    70
    Rep Power
    0

    Default Re: primitive acting like a Object

    thanks for taking the time by the way to help me out.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: primitive acting like a Object

    Quote Originally Posted by killutch View Post
    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!

    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.

  6. #6
    killutch is offline Member
    Join Date
    Sep 2012
    Posts
    70
    Rep Power
    0

    Default Re: primitive acting like a Object

    Quote Originally Posted by Fubarable View Post
    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

Similar Threads

  1. Text Fields Acting Up
    By Tohron in forum Java Applets
    Replies: 5
    Last Post: 09-05-2012, 06:14 AM
  2. Sphinx 4 Voice Recognition acting WEIRD!
    By treeface99 in forum Advanced Java
    Replies: 3
    Last Post: 04-16-2012, 05:39 PM
  3. Converting primitive int to Intger object?
    By NewbieDan in forum New To Java
    Replies: 7
    Last Post: 02-15-2012, 12:33 PM
  4. actionPerformed acting strangely
    By Fortu in forum New To Java
    Replies: 18
    Last Post: 04-10-2011, 04:07 AM
  5. Acting Java as if it's C problem
    By reis3k in forum New To Java
    Replies: 13
    Last Post: 10-18-2010, 09:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •