This week's book giveaway is in the Python forum.
We're giving away four copies of Python Continuous Integration and Delivery and have Moritz Lenz on-line!
See this thread for details.
Win a copy of Python Continuous Integration and Delivery this week in the Python 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
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Passing Primitive Data Values  RSS feed

 
Ranch Hand
Posts: 45
  • 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: 3915
45
  • 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: 37
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: 45
  • 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
 
Sheriff
Posts: 13147
218
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • 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
Sheriff
Posts: 13147
218
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • 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: 37
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
 
See where your hand is? Not there. It's next to this tiny ad:
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!