• 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

Character Representation Doubt

 
Ranch Hand
Posts: 579
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given the following class

public class ZeroPrint{
public static void main(String argv[]){
int i =0;
//Here
}
}

Which of the following lines if placed after the comment //Here will print out 0.

1) System.out.println(i++);
2) System.out.println(i+'0');
3) System.out.println(i);
4) System.out.println(i--);

Plz Let Me Know Why 2nd Option Is Not Correct?
Thanks

##########################################################
Agrah Upadhyay
3rd Year
B.tech
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The second option will concatenate the character value '0' with the toString() representation of the numeric value 0. The result will be "00" instead of "0".
 
agrah upadhyay
Ranch Hand
Posts: 579
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank U Very Much Sir,
But why Operation [1+'0'] And Then toString() is happening/
Plz Tell me Logic Behind that
Thanks
##################3
Agrah Upadhyay[/LIST]
 
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The second option will print 48 ie. ascii value of
'0'+i => 48+0 => 48

hope this will help you

Sandy
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think java uses Unicode although 0 in unicode also symbolises 48 so there is nothing wrong with the awnser given by mr Sandeep.

Please Correct Me If i am Wrong.
 
anand phulwani
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am very eager to know what is the problem with the other 3 options they seem to be the awnser as well which will print 0.
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello anand,
Surely option 3 and 4 will also print 0 as output.

Sandy
 
Michael Ernest
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Michael Ernest:
The second option will concatenate the character value '0' with the toString() representation of the numeric value 0. The result will be "00" instead of "0".


Yow. I was in the wrong part of my brain on this answer -- my apologies.

The actual output of option 2 is 48. Sandeep has given the correct explanation. It's a 'Unicode' 48 instead of an ASCII '48' but obviously it comes to the same thing.
[ September 15, 2005: Message edited by: Michael Ernest ]
 
That is a really big piece of pie for such a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic