obasan Posted March 26, 2017 Share Posted March 26, 2017 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? Quote Link to comment Share on other sites More sharing options...
Magarac Posted March 26, 2017 Share Posted March 26, 2017 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? Quote Link to comment Share on other sites More sharing options...
Explv Posted March 26, 2017 Share Posted March 26, 2017 (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 March 26, 2017 by Explv 3 Quote Link to comment Share on other sites More sharing options...
Mumble Posted March 26, 2017 Share Posted March 26, 2017 it's easy enough to do x++; instead of writing a whole method for it... Quote Link to comment Share on other sites More sharing options...
Explv Posted March 26, 2017 Share Posted March 26, 2017 3 minutes ago, Mumble said: it's easy enough to do x++; instead of writing a whole method for it... I don't think that is the point of his question... 1 Quote Link to comment Share on other sites More sharing options...
Magarac Posted March 26, 2017 Share Posted March 26, 2017 16 minutes ago, Explv said: Java is pass by value, not pass by reference. Consider reading this: http://jonskeet.uk/java/passing.html Oh wow interesting read.. was really curious if it was the same as in C++ or not and that makes a ton of sense. Quote Link to comment Share on other sites More sharing options...
Polymorphism Posted March 26, 2017 Share Posted March 26, 2017 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 1 Quote Link to comment Share on other sites More sharing options...
TheWind Posted March 27, 2017 Share Posted March 27, 2017 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 1 Quote Link to comment Share on other sites More sharing options...
obasan Posted April 8, 2017 Author Share Posted April 8, 2017 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! 1 Quote Link to comment Share on other sites More sharing options...
Alek Posted April 8, 2017 Share Posted April 8, 2017 In C++ you would have to define the function as taking a reference, otherwise this code would behave the same (pass by value). The function would look like: static void addOneTo (int& num) 1 Quote Link to comment Share on other sites More sharing options...
Final Posted April 8, 2017 Share Posted April 8, 2017 (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 April 8, 2017 by Final Quote Link to comment Share on other sites More sharing options...
obasan Posted April 8, 2017 Author Share Posted April 8, 2017 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 Quote Link to comment Share on other sites More sharing options...
Final Posted April 8, 2017 Share Posted April 8, 2017 (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 April 8, 2017 by Final Quote Link to comment Share on other sites More sharing options...
obasan Posted April 8, 2017 Author Share Posted April 8, 2017 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. Quote Link to comment Share on other sites More sharing options...
toomaz109 Posted April 8, 2017 Share Posted April 8, 2017 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; } } Quote Link to comment Share on other sites More sharing options...