Jump to content

General Java help


obasan

Recommended Posts

public class MyClass {

                  public static void main (String[] args) {

                                    int x = 5;

                                    addOneTo(x);

                                    System.out.println(x);

                  }

 

                  static void addOneTo (int num) {

                                    num++;

                  }

}

// Outputs “5”

 

Why is the output 5 and not 6?

Link to comment
Share on other sites

9 minutes ago, obasan said:

public class MyClass {

                  public static void main (String[] args) {

                                    int x = 5;

                                    addOneTo(x);

                                    System.out.println(x);

                  }

 

                  static void addOneTo (int num) {

                                    num++;

                  }

}

// Outputs “5”

 

Why is the output 5 and not 6?

Not 100% sure if Java is like C++ in this aspect but in C++ the problem would be the num in the addOneTo function isn't being passed by reference and never updates the x. Not sure here though, am still learning Java, can someone clarify?

Link to comment
Share on other sites

27 minutes ago, obasan said:

public class MyClass {

                  public static void main (String[] args) {

                                    int x = 5;

                                    addOneTo(x);

                                    System.out.println(x);

                  }

 

                  static void addOneTo (int num) {

                                    num++;

                  }

}

// Outputs “5”

 

Why is the output 5 and not 6?

Java is pass by value, not pass by reference.

Consider reading this:

http://jonskeet.uk/java/passing.html

Edited by Explv
  • Like 3
Link to comment
Share on other sites

Java passes into methods by value, not by reference -- although objects are manipulated by reference. In order for your example to work, you would need to have the class you're working in as an instance.

 

AddByOne add = new AddByOne(5);

add.addOne(); //outputs 5

 

That would work because in the constructor you would set a private int that is accessible only to the instance and manipulate it from inside the object. However you can also add getter/setter to change or grab the variable externally.

 

Here is a good link to read on

http://www.javaworld.com/article/2077424/learn-java/does-java-pass-by-reference-or-pass-by-value.html

EDIT: Hadn't noticed other people replied, sorry

  • Like 1
Link to comment
Share on other sites

53 minutes ago, Explv said:

Java is pass by value, not pass by reference.

Consider reading this:

http://jonskeet.uk/java/passing.html

 

 

19 minutes ago, Polymorphism said:

 

Java passes into methods by value, not by reference -- although objects are manipulated by reference. In order for your example to work, you would need to have the class you're working in as an instance.

 

AddByOne add = new AddByOne(5);

add.addOne(); //outputs 5

 

That would work because in the constructor you would set a private int that is accessible only to the instance and manipulate it from inside the object. However you can also add getter/setter to change or grab the variable externally.

 

Here is a good link to read on

http://www.javaworld.com/article/2077424/learn-java/does-java-pass-by-reference-or-pass-by-value.html

 

 

Thank you for these responses. I didn't realize that objects actual references weren't passed into the method. Both articles were great to read :)

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...
On 3/26/2017 at 8:15 PM, TheWind said:

 

Thank you for these responses. I didn't realize that objects actual references weren't passed into the method. Both articles were great to read :)

 

Both articles were an eye opener for me. I had no idea that Java passes by VALUE, NOT REFERENCE. Thanks to everyone who helped!

  • Like 1
Link to comment
Share on other sites

Java may be by value, but this value, if it is an object and not a primitive type contains a reference. The issue that you are having is because you are using a primitive type, which do not contain an object reference and therefore cannot be manipulated by using the reference (it uses the value).

If you used the same code but replaced the data type with an object such as a list, the code would behave as you originally expected.

Just to repeat, a primitive passes a VALUE, an object passes as a VALUE but contains a reference.

By using the same logic as what others have replied with, how the hell would a singleton work other than creating a new reference each time?

On 3/27/2017 at 0:15 AM, TheWind said:

 

Thank you for these responses. I didn't realize that objects actual references weren't passed into the method. Both articles were great to read :)

They are.

Edited by Final
Link to comment
Share on other sites

1 hour ago, Final said:

Java may be by value, but this value, if it is an object and not a primitive type contains a reference. The issue that you are having is because you are using a primitive type, which do not contain an object reference and therefore cannot be manipulated by using the reference (it uses the value).

If you used the same code but replaced the data type with an object such as a list, the code would behave as you originally expected.

Just to repeat, a primitive passes a VALUE, an object passes as a VALUE but contains a reference.

By using the same logic as what others have replied with, how the hell would a singleton work other than creating a new reference each time?

They are.

By using getters and setters

Link to comment
Share on other sites

1 hour ago, obasan said:

By using getters and setters

Because references are passed, that's how getters are possible. Otherwise it'd be the same object 'value' but a different reference and therefore different memory locations/instances. Getters function off of the same principle which is being disputed here. In this same example, a getter would have the same issue because the variable is storing a primitive value. Try the code yourself with a getter.

Edited by Final
Link to comment
Share on other sites

14 minutes ago, Final said:

Because references are passed, that's how getters are possible. Otherwise it'd be the same object 'value' but a different reference and therefore different memory locations/instances. Getters function off of the same principle which is being disputed here. In this same example, a getter would have the same issue because the variable is storing a primitive value. Try the code yourself with a getter.

i will indeed.

 

im learning java from scratch with no prior programming experience. this shit is hard lol.

Link to comment
Share on other sites

public class MyClass {

                  public static void main (String[] args) {

                                    int x = 5;

                                    addOneTo(x);

                                    System.out.println(x);

                  }

 

                  static void addOneTo (int num) {

                               //You have to pre increment to see the result after referencing x

                                    ++num;

                  }

}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...