• 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

pass by value

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Im confuse by output of this.


Because primitive types pass by value so you pass a copy of variable . With reference type like array you pass copy of refernce so i expect output to be 0 0 1, but answer here is 1 0 1. Can pls explain.

 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Little scope problem here
a is an instance member which means that if you modify it within a method its value will be changed. You also pass b by value but you also name the argument "b" so inside the method b will be the argument and not the instance member and that's why the value of b (the instance member) does not change.
If you refer to b inside the method then the actual b you are working on is the argument and not the instance member.
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Pervez Amin
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ok, I see we really do this.a = 1!! which mean we change actual object. b is a copy which mean we never change the actual variable, we modify copy of the variable. With array which is reference type - we assign a new value to the first elemnt in the array.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic