This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Pass by Value confusion for arrays question

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I encountered this question on Enthuware test today. The correct answer is B.  I understand why i is 1 but I do not understand why the method changes the arrays value. Is it because it's actually getting the array element of 0 and not just the name?

What will be the result of attempting to compile and run the following class?




The code will print i = 1 iArr[0] = 1
The code will print i = 1 iArr[0] = 2
The code will print i = 2 iArr[0] = 1
The code will print i = 2 iArr[0] = 2
The code will not compile.
There is no problem with the code.
 
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is passed by value.  The value is the reference to the array.
 
Mike Roundtree
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A value from the array isn't being passed in.  The reference to the array is being passed in.  Answer B is correct.  I would download NetBeans to check this for yourself.  Download Netbeans and install.  Then testing these things is just a matter of pasting into a new class and right clicking and choosing "Run from file".  Took me about 5 seconds to do the testing.
 
Heather Keating
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you and yes I did try it. The question at the top was part of the original mock question. I always thought pass by value did not affect the original value and here it seems like it does.
 
Mike Roundtree
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the value of iArr is unaffected.  It is still referencing the same array.  You changed a value inside the array using a passed in value that references the array.  The value that is passed is the reference to the array which has not been changed. That's why you can still access the same array when you get out.  The other values in the array are still the same.  Here is an example of how setting n to null doesn't change the original iArr passed in.  


/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package July2017;

 
Mike Roundtree
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This might help... especially the very end

http://javadude.com/articles/passbyvalue.htm
 
Heather Keating
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay it is clear to me now. N is simply pointing to null object but the array reference is unchanged. Thank you!
 
Mike Roundtree
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, this link may help further

https://docstore.mik.ua/orelly/java-ent/jnut/ch02_10.htm

Especially section 2.10.4
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an excellent topic with a very nice and simple explanation about pass by value.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And another excellent article about pass by value
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!