• 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

Marcus Green Exam 3 Qtn3 and Qtn56 confusing me.

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Somebody help me with the addition of char (variable or literal) to integrals. On exam 3/Qtn 3 of M/Green option 2 is correct, ie int i=2+'2', but on qtn 56 option 2 is wrong, ie System.out.println(i+'0').Confused???
 
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q56 wants to know which eval to zero... 0+'0' is 48... you have to know that the int value of character 0 is 48, not zero. so the answer to 0+'0' is 0+48=48
Q3 just wants to know if 2+'2' is legal syntax, which it is.
Does that help?
[ July 31, 2002: Message edited by: Nate Johnson ]
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"dlamini"-
Welcome to the JavaRanch! Please adjust your displayed name to match the JavaRanch Naming Policy.
You can change it here.
Thanks! and again welcome to the JavaRanch!
 
Nhlanhla Dlamini
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Follow-up on my question.
Thanks Nate Johnson for responding to my question - I was answered. I would like to find out: does this mean for every int value there's a corresponding and different char value? ie is int i=3 different from char c='3'in terms of value? And what about char c=3. What's the differnce?
Thank you in advance.
N.Dlamini.
 
Nate Johnson
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The easiest way to find out is to write a little program... try this...

You will see that the first one prints out a 3 and the second a heart (the ascii character with the value of 3). If you wanted to get 3 as the output of a char you would have to either put the 3 in single quotes like you did or say...

So to answer your question, no there is not a char value for each int value... check out a java book to see the ranges of types. int values are 32 bit and char is only 16 bit... but every char does have an int value.
[ August 05, 2002: Message edited by: Nate Johnson ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic