• 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

a question

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question ID :957967165446
Consider the following class...

What will be the output of the following program?
Why does it print 2 and null when run?
Thanks.
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sarah Haideri:
Question ID :957967165446
Why does it print 2 and null when run?
Thanks.


Sarah,
Trace the code carefully

Here t1 is created with i = 0
and when you print it

it checks the toString() method

and prints null.
Similarly t2 prints 2 because value of i is 2 and toString() returns i.
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, what about the code below:

It compiles well but throws NullPointerException at ////A at
runtime, anyone has better idea? If I change it into:

It will compile and run well. Why? In my opinion, the two
should be the same.
Thanks
Guoqiao

Originally posted by Jyotsna Clarkin:
[B]
Here t1 is created with i = 0
and when you print it

it checks the toString() method

and prints null.
Similarly t2 prints 2 because value of i is 2 and toString() returns i. [/B]


reply
    Bookmark Topic Watch Topic
  • New Topic