This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

o/p needed  RSS feed

 
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
 
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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prints: 0
Wrapper objects are immutable
 
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.
 
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!