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 o/p needed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "o/p needed" Watch "o/p needed" New topic

o/p needed

Suhita Reddy
Ranch Hand

Joined: Jun 09, 2006
Posts: 60
hi all,

wht is the o/p this?

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.printIn (i.intValue ( ) );
12. }
Jack Lee
Ranch Hand

Joined: Jun 06, 2006
Posts: 38
output is 0

when the main method call add3(), it copy the reference of local variable i in main method and pass it to the local variable i in add3 method.

those two i point to the same object at the begin of the method add3()

Inside add3() method, it changes the reference of the local i variable to a new Integer, which does not change anything of the local variable i in main method, so the output of the println in main method still print out the value of the original Integer object.

SCJP 5.0<br />SCWCD 1.4
Mohamed Mahrous

Joined: Jul 06, 2006
Posts: 3
Prints: 0
Wrapper objects are immutable
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Have you not compiled and run the code yourself? Getting practice with using an editor and compiler is an important part of becoming a Java programmer. Do you want to be just a "theoretical" programmer? If you cannot understand a compiler message or why a particular expression evaluates to a particular result then just ask.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Aum Tao
Ranch Hand

Joined: Feb 14, 2006
Posts: 210
wht is the o/p this?

Why is more important than What.

SCJP 1.4 85%
I agree. Here's the link:
subject: o/p needed
It's not a secret anymore!