Page 1 of 2 12 LastLast
Results 1 to 20 of 34
Like Tree3Likes

Thread: Passing values and objects

  1. #1
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Passing values and objects

    Whether I pass primitives or objects the original value does not change. Is this expected result?

    Java Code:
    public class Class {
    	public static void main(String args[])
    	{
    		int x=3;
    		doItPrim(x);
    		System.out.println(x);//3
    		
    		Integer i=new Integer(3);
    		doItObj(i);
    		System.out.println(i);//3
    	}
    	public static void doItPrim(int x)
    	{
    		x=5;
    	}
    	
    	public static void doItObj(int i)
    	{
    		i=new Integer(5);
    	}
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Passing values and objects

    the original value does not change
    yes, Java passes args by value.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: Passing values and objects

    Then how come in the below code the value changes?

    Java Code:
    public class Class {
    	public static void main(String args[])
    	{
    		
    		A a = new A();
    		System.out.println(a.i);
    		
    		Class class1 = new Class();
    		class1.doIt(a);
    		
    		System.out.println(a.i);
    		
    	}
    	public  void doIt(A x)
    	{
    		x.i = 20;
    	}
    }
    Java Code:
    public class A{
    	public int i;
    	
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Passing values and objects

    Please post the output and add some comments that shows what you are talking about.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    ababa is offline Member
    Join Date
    Oct 2014
    Location
    Bhagalpur, India
    Posts
    3
    Rep Power
    0

    Default Re: Passing values and objects

    read about lifetime of variables.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    Default Re: Passing values and objects

    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Passing values and objects

    Quote Originally Posted by KevinWorkman View Post
    Aw man, that one is way better drawn than my own :(

    http://www.java-forums.org/new-java/...ing-wrong.html
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    Default Re: Passing values and objects

    Haha! I'm in an OOP class right now (it's a pre-req for some other required classes), so those diagrams look all too familiar!
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  9. #9
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: Passing values and objects

    Hi KevinWorkman,

    I already read the link before you posted it. Ok. Let me ask a simple question.

    Java Code:
    Integer i=new Integer(3);
    Here the variable i contains the address of 3 or the value 3? When I print the variable i it prints 3 but shouldn't the variable i supposed to contain the address of 3?

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Passing values and objects

    The Integer class overrrides the toString() method which returns the String representation of its wrapped value (here: 3); Integers are also subject to autoboxing; if you want to test stuff, better use the following small class:

    Java Code:
    public class MyInt {
        private int i;
        public MyInt(int i) { this.i= i; }
        // test it with and without this method:
        public String toString() { return ""+i; }
    }
    kind regards,

    Jos

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Passing values and objects

    Quote Originally Posted by jmohandos304 View Post
    Hi KevinWorkman,

    I already read the link before you posted it. Ok. Let me ask a simple question.

    Java Code:
    Integer i=new Integer(3);
    Here the variable i contains the address of 3 or the value 3?
    It contains the value 3.
    The value of a primitive is its value.
    The value of a reference is its address on the heap.

    So when you pass a primitive into a method (say your above '3') the value of the primitive is passed in.
    The same happens with a reference.
    In both cases it is not the same reference or primitive, it is a copy of it with the same value. Consequently any changes to the value of the variable in that method has no effect on the original variable.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Passing values and objects

    Here the variable i contains the address of 3 or the value 3
    Neither. i has the address of an instance of an Integer object that contains a 3.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Passing values and objects

    I see I might have misinterpreted the question..:)
    I thought by "contains" they meant "there's an attribute inside 'i' that has the value of 3".

    :)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #14
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Passing values and objects

    Well, one more time. Hi I'm Gimby and I suck at paint!

    Passing values and objects-really_really_really_lay_it_on_thick.png
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  15. #15
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    Default Re: Passing values and objects

    One thing that might be confusing OP: you don't have access to the address in memory. The pictures and examples use a theoretical memory address to demonstrate their point, and you're passing around a reference to that location in memory, but as a Java developer you can't actually print that value out.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  16. #16
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Passing values and objects

    Quote Originally Posted by KevinWorkman View Post
    passing around a reference to that location in memory, but as a Java developer you can't actually print that value out.
    Well, actually it depends on the implementation:

    From Object.hashCode()

    "As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)"

    Regards,
    Jim
    KevinWorkman likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  17. #17
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    Default Re: Passing values and objects

    I guess it depends on whether that conversion preserves any information that's actually usable. My only point was that the OP seems to be expecting to interact directly with the memory address, which ain't Java. All of our examples use the memory address as an example, not as a real piece of data that you need to worry about.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  18. #18
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Passing values and objects

    I -really- wish we were in a place where you could just safely say "you pass around objects" and that everyone would then instantly correctly understand what that means without having to resort to discussion about references, pointers and memory addresses - things Java is designed to worry about for you.

    Congrats on managing to reach only 4k posts after almost exactly 4 years. You're an on-topic trooper.
    KevinWorkman likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  19. #19
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    Default Re: Passing values and objects

    Quote Originally Posted by gimbal2 View Post
    Congrats on managing to reach only 4k posts after almost exactly 4 years. You're an on-topic trooper.
    Oh! Well first off I'd like to thank all of the spammers for keeping me busy deleting their posts, next I'd like to thank everybody who swore at me for not doing their homework for them...
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  20. #20
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: Passing values and objects

    The below link states that in Java primitives are passed by value and objects are passed by reference. Is it correct or wrong?

    http://www.cse.yorku.ca/~mack/1011/PassByReference.PDF

Page 1 of 2 12 LastLast

Similar Threads

  1. passing objects w/RMI
    By nicoeschpiko in forum Advanced Java
    Replies: 5
    Last Post: 02-06-2011, 09:59 AM
  2. Passing Objects
    By Java_Developer in forum New To Java
    Replies: 6
    Last Post: 10-29-2009, 02:32 PM
  3. MVC passing objects
    By simo_mon in forum New To Java
    Replies: 1
    Last Post: 07-17-2009, 07:57 AM
  4. Passing the Values
    By Lagarto in forum JDBC
    Replies: 1
    Last Post: 07-16-2007, 04:03 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
  •