• 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
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Passing Primitive Data Values  RSS feed

 
Ranch Hand
Posts: 33
  • 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: 3797
40
  • 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.
 
Greenhorn
Posts: 23
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: 33
  • 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: 12815
211
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: 12815
211
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
Greenhorn
Posts: 23
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
 
There's a way to do it better - find it. -Edison. A better tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!