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

Passing Primitive Data Values

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

please who can explain to me the result of system.out.println(i) is 1
 
Rancher
Posts: 4271
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that Java is pass-by-value.
That is, the value of i in main is passed into the addTwo method, not i itself.

The i in addTwo is therefore not the same primitive as the one in main, so anything done to it has no effect at all outside that method.
 
Ranch Hand
Posts: 43
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you tell me what is the difference between ++i and i++?
 
emma roberts
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but what I can not understand why the increment in the signature method change the value of  the i  but normally  all change in the signature method and body is not considered outside the addTwo
 
Marshal
Posts: 14045
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shubham Rai wrote:Can you tell me what is the difference between ++i and i++?



As an expression, ++i evaluates to (i + 1) whereas i++ evaluates to (i).  So, if you have:

Whereas,
 
Junilu Lacar
Marshal
Posts: 14045
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

emma roberts wrote:but what I can not understand why the increment in the signature method change the value of  the i  but normally  all change in the signature method and body is not considered outside the addTwo


What are you referring to as the "signature method"? It's not clear what you're confused about. Was there something in Dave's explanation that you did not understand?
 
Shubham Rai
Ranch Hand
Posts: 43
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general way consider a very basic example :


Now coming to your code:
When you pass the value of i++ to your method it will remain as i=0 and after the execution of addTwo() method  it will increase to +1 and hence your line no 7 will print i=1
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!