Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

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: 4577
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: 15631
264
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: 15631
264
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
 
a wee bit from the empire
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic