• Post Reply Bookmark Topic Watch Topic
  • New Topic

differences between a primitive type and a class type  RSS feed

 
Serhat Duygun
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, everyone,

i am trying to understand that differences between a primitive type and a class type have an impact, i know if google it i can get plenty of documentation about that, but please take look at my exemple and explain to me why above code result is why "i" is not "2" i really confuse

Thank You



 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason that main's i is not incremented is because Java passes all parameters by value. A result of this is that when you change the value of a method parameter inside that method, the change only affects the method's copy of that variable, and is not seen by the caller.

"Wait," you might say. "Why then can main see the change to the iArr variable?" The answer is: It can't. That variable's value isn't changing. What's chaning is the contents of the object (an array) that both main's iArr and incr's n point to. The iArr reference variable's value was copied, so now we have to independent reference variables pointing to the same object. We're not changing the value of either of those variables, just the inside of the object they point to.



So it's the difference between, on one hand X = something and, on the other hand, X.field = something or X[index] = something.
 
Serhat Duygun
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much. it helped me a lot.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!