• 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

method..

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1 public class Test {
2 public static void add3 (Integer i) {
3 int val = i.intValue();
4 val += 3;
5 i = new Integer(val);
6 }
7
8 public static void main(String args[]) {
9 Integer i = new Integer(0);
10 add3(i);
11 System.out.println(i.intValue()); 12 } 13 } What is the result?

A. 0

B. 3

C. Compilation fails.

D. An exception is thrown at runtime.
 
Ranch Hand
Posts: 115
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer: A. 0
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When u pass arguments to a method, only a copy of the reference /value is passed. so changes to the reference/value will not reflect in the calling method. But changes to the values referred by the reference will be reflected. Hope thats not too confusing
 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Xie,

Just remember that everything is passed by value. There is nothing like pointers.

As K&B says, and I love it. "Java is not C++". saying such thing by yourself will really(!) convience yourself to think java code seperately than C++ code.

By the way, output is 0. so option A is right.
 
xie li
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you
 
reply
    Bookmark Topic Watch Topic
  • New Topic