• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

pass by value (Object,Primitive, Array)

 
Ranch Hand
Posts: 220
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider such a piece of code;

I want to ask some questions to find the right asnwers about passing parameters to methods



result is;


1
0
0
0


Can I conclude from that code (that I code(source is me))

Primitives are passed by value and you cannot change the value of the primitive in a method!

For objects you can change the value of the object inside the method(instance variables can change but you cannot refer to null it)

Primitives arrays can be changed in methods

[ November 05, 2008: Message edited by: Anut Walidera ]
 
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
primitives are passed by a copy of the value of its reference variable (the primitive's value).
objects are pass by copy of the object reference value (its location on the heap)
arrays are objects and like all objects you can change the state of an object without changing it's reference value. you can do the same thing by changing the instance variables within an object you pass to your method.
[ November 05, 2008: Message edited by: Paul Campbell ]
 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi anut...
you are right but in case of arrays there are two different scenerios.

1)
if you are assigning one array variable to other variable instead of assigning values in array through index then it will not effect the original array

class GFC304 {
static void m1(int[] i1, int[] i2) {
int[] i3 = i1; i1 = i2; i2 = i3;
}
public static void main (String[] args) {
int[] i1 = {1}, i2 = {3}; m1(i1, i2);
System.out.print(i1[0] + "," + i2[0]);
}}

2)if you are assigning values through index then the original array get effected.

class GFC305 {
static void m1(int[] i1, int[] i2) {
int i = i1[0]; i1[0] = i2[0]; i2[0] = i;
}
public static void main (String[] args) {
int[] i1 = {1}, i2 = {3}; m1(i1, i2);
System.out.print(i1[0] + "," + i2[0]);
}}


[ November 05, 2008: Message edited by: Ganeshkumar cheekati ]
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gansesh

if you are assigning one array variable to other variable instead of assigning values in array through index then it will not effect the original array



When you assign one array variable to another(or an object to another object), the refernce will be changed. ie, the actual parameters-in the calling method and the formal parameter-in the called method, will point to different objects. That is why change in one won't affect the other.
 
Tuna Töre
Ranch Hand
Posts: 220
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your answers I got the idea!
So I learned it cleary

Thanks...


I am going to take SCJP in 15 days... I can't wait anymore friends...
I will share my experiences sure.... Exam is easy I know...


[ November 06, 2008: Message edited by: Anut Walidera ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic