Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java is pass by value????

 
suavedeep kaur
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am confused about the java's concept of pass by value ............as some books say java is all pass by value


and some says Primitive types are passed by value and
Objects are passed by reference


please can someone explain in detail whats the reson n how it happens???
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
simple demo, if it was pass-by-ref, 'list' would change

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

Yes, Java is pass by value only. But the confusing thing is that references are also passed by value.

For more information, see this JavaRanch Campfire Story: Pass-by-Value Please
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi suavedeep kaur welcome to Javaranch,

i am confused about the java's concept of pass by value ............as some books say java is all pass by value


and some says Primitive types are passed by value and
Objects are passed by reference

In which book did you find that Objects are passed by reference
It's actually object references are passed by value and not objects.

Hope this helps
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And yes
as some books say java is all pass by value

Why only some books all books say so.

Hope this helps
 
suavedeep kaur
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sir f have complete reference its on page 133

i am new to java so exploring it

its what all is written
objects are passed by value
 
suavedeep kaur
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


[edit]Add code tags. CR[/edit]
[ July 21, 2008: Message edited by: Campbell Ritchie ]
 
suavedeep kaur
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks sir i got my doubt cleared
 
Campbell Ritchie
Sheriff
Pie
Posts: 49432
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by suavedeep kaur:
Thanks sir i got my doubt cleared
Well done. This is a common problem, and causes quite a lot of confusion, particularly when some books are not precise in how they describe it.
Click the search button above, and put "pass by value" in the box. You get lots of links; try this one, and look at the "wonderful link" quoted in the 3rd message. It is a bit complicated, but explains very clearly what pass-by-reference would be and how in works in Pascal. You can then try it out in Java, as in Michael Dunn's example, and you can see how the results are different.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic