aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Pass By Values / Pass By reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Pass By Values / Pass By reference" Watch "Pass By Values / Pass By reference" New topic
Author

Pass By Values / Pass By reference

Sarath Koiloth Ramath
Ranch Hand

Joined: May 07, 2008
Posts: 52
Hi frnds,


I read the k&B passing the variables and object.But land up with lots of doubts.Can any one please help me to understand the idea behing the pass by reference and pass by values.Is java using pass by values Or java using both.please give a good description

Thank


J 4 Java
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14435
    
  23

Java is strictly pass-by-value. What confuses people is that Java passes references by value too, which is not the same as pass-by-reference.

See this JavaRanch Campfire Story: Pass-by-Value Please
[ May 22, 2008: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Tusshar Fasate
Ranch Hand

Joined: May 21, 2008
Posts: 81
Hi Sarath,
Java use only pass by value.
and it doesnt use pass by reference.

here example

static void ranch(int a)
{
int b=b+a
}
in...main

{
int x=9;
ranch(x);

}

here value of x is copied to variable a
and whatever operaions made on a will not be reflected in x.


But its little confusing about array or any object refernce.

static void ranch(Test t)//Test is any class
{

}
in main...
{
Test t1=new Test();

ranch(t1);
....
...}
here also only one object is created and reference is sent to method ranch
so whatever operation made in ranch method will reflect to original object.its also call by value but bit of confusing.


Tushar...
Pune
India
Sarath Koiloth Ramath
Ranch Hand

Joined: May 07, 2008
Posts: 52
Hi Tusar,

I think i got it.i will try to explain.

While passing a primitive variable ,Java is passing the copy of the variable.when we pass an object, the reference(that may be an address reference,but pointing to the same object) is passing.hence we say pass by value.So any change made will be reflected in the original object.I am correct...
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

Originally posted by Sarath Koiloth Ramath:
So any change made will be reflected in the original object.
it depends on what you mean by 'any change'. if i do this:



There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Sarath Koiloth Ramath
Ranch Hand

Joined: May 07, 2008
Posts: 52
Ok Thanks..I got it...

Thanks once again tushar,Jesper ,and fred...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pass By Values / Pass By reference