File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes method.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "method.." Watch "method.." New topic


xie li
Ranch Hand

Joined: Nov 30, 2005
Posts: 54
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 }
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.
Devi Sri
Ranch Hand

Joined: Dec 20, 2005
Posts: 114

Answer: A. 0

Devisri, SCJP 5.0, SCWCD 5.0
"Dream is Not what you see in sleep. Dream is that which never lets you sleep" - Abdul Kalam
Cheenu Subramanian
Ranch Hand

Joined: Aug 15, 2005
Posts: 40
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
vipul patel
Ranch Hand

Joined: Oct 16, 2005
Posts: 146

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

Joined: Nov 30, 2005
Posts: 54
thank you
I agree. Here's the link:
subject: method..
It's not a secret anymore!