• 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

 
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i getting the out put as 200. as we knoe java is passby value but im getting 200 as out please help me
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This theory doesn't prevent you from modifying an object's state. Objects you pass to a method are not immutable. The reference to the object will not change, but it's content can be changed.
Check what happens with the following :

 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The reference to the object will not change, but it's content can be changed.


as you told reference will not change means inside the method. method reference and actual reference are pointing to the same object wright and after the method scope the method reference will be no more valid is it correct.l
 
Ranch Hand
Posts: 608
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

santhosh.R gowda wrote:after the method scope the method reference will be no more valid is it correct.l



Yes this is correct. However the original reference will still be valid - you can still use it.

If there still confusion I'll recommend you get your hands on Head First Java. The got a really nice 'remote control' which should makes things crystal clear & a whole lot of other concepts too (Of course!)
 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
points to note:

1.class test
2.only one instance of class test is created.

test p = new test(100);


3.p is the reference variable which holds the address of object test, with i = 100.
4.in getRef(p) - you are passing the address of object test, as p holds the address.
5. public static void getRef(test obj)
{
obj.i=200;
}

inside this method ,

no new object is created.


obj has the same address as p.so obj and p refers the same object of class test, where the i's value is 100.

6. when you assign the value for i using this statement,

obj.i=200;


you are manipulating the one and only object, by changing its i value to 200.

7.that's how you get 200 as output,using this print statement.
System.out.println(p.i);



test.jpg
[Thumbnail for test.jpg]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See the following JavaRanch Campfire Story to understand what pass by value means exactly in Java:
Pass-by-Value Please
reply
    Bookmark Topic Watch Topic
  • New Topic