Jump to content

General Java help


Recommended Posts

Posted

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?

Posted
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?

Posted (edited)
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
Posted

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
Posted
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
  • 2 weeks later...
Posted
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
Posted (edited)

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
Posted
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

Posted (edited)
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
Posted
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.

Posted

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;

                  }

}

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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