• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

o/p needed

 
Suhita Reddy
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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. }
7.
8. public static void main (String args [ ] ) {
9. Integer i = new Integer (0);
10. add3 (i);
11. system.out.printIn (i.intValue ( ) );
12. }
regards
suhita
 
Jack Lee
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mohamed Mahrous
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prints: 0
Wrapper objects are immutable
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Aum Tao
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wht is the o/p this?


Why is more important than What.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic