• 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

Clarification on a fundamentals question

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class example {
int i = 0;
public static void main(String args[]) {
int i = 1;
change_i(i);
System.out.println(i);
}
public static void change_i(int i) {
i = 2;
i *= 2;
}
}


A. The program does not compile.
B. The program prints 0.
C. The program prints 1.
D. The program prints 2.
E. The program prints 4.

Can somebody explain why the value is 1 for this question
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java is pass-by-value.
take a look at this for further explanation :

http://www.javaranch.com/campfire/StoryPassBy.jsp

Regards
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To refer to the class member variable i, you'd have to use
this.i = 2;

- Anu
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The method change_i() changes the value of the i locally (i.e. inside the method). The value is lost once we are outside the method and so the System.out.println(i) only sees the local value for i in the main() method which has a value of 1.
To print the changed value from change_i() you need to return the new value :

public class example {
int i = 0;
public static void main(String args[]) {
int i = 1;
i = change_i(i);
System.out.println(i);
}
public static int change_i(int i) {
i = 2;
i *= 2;
return i;
}
}


Hope that makes sense ?
Finner
[ June 15, 2006: Message edited by: Finner Jones ]
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Originally posted by Anupama Ponnapalli:

To refer to the class member variable i, you'd have to use
this.i = 2;




this can't be used in a static context. Both methods are static.


Naseem
[ June 15, 2006: Message edited by: Naseem Khan ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following "companion" example...

Do you see why the output is different?
reply
    Bookmark Topic Watch Topic
  • New Topic