This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming 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
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

pass by value

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

Im confuse by output of this.


Because primitive types pass by value so you pass a copy of variable . With reference type like array you pass copy of refernce so i expect output to be 0 0 1, but answer here is 1 0 1. Can pls explain.

 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Little scope problem here
a is an instance member which means that if you modify it within a method its value will be changed. You also pass b by value but you also name the argument "b" so inside the method b will be the argument and not the instance member and that's why the value of b (the instance member) does not change.
If you refer to b inside the method then the actual b you are working on is the argument and not the instance member.
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Pervez Amin
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ok, I see we really do this.a = 1!! which mean we change actual object. b is a copy which mean we never change the actual variable, we modify copy of the variable. With array which is reference type - we assign a new value to the first elemnt in the array.
 
Yeast devil! Back to the oven that baked you! And take this tiny ad too:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!