• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

differences between a primitive type and a class type

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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



 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much. it helped me a lot.
 
Yeah. What he said. Totally. Wait. What? Sorry, I was looking at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic