• 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

convert char literal to int (Mughal p.52)

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Mughal's book re: Numeric Promotion in Arithmetic Expressions ... the following code:
public class {
public static void main(String args[]){
byte b = 32;
char c = 'z';
short s = 256;
int i = 10000;
float f = 3.5F;
double d = 0.5;
double v = (d * i) + (f * -b) - (c / s);
System.out.println("Value of v: " + v);
}
}
My ques. is how does one convert char c = 'z' to a numeric int value and whether the exam will cover knowing how to do this conversion?
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Linda Pan:
My ques. is how does one convert char c = 'z' to a numeric int value and whether the exam will cover knowing how to do this conversion?


In order to get the numeric value of a char variable, you can simply assign it to a variable of type int - in such a case, an implicit conversion will be performed because there is no risk of data loss when going from a char to an int. You can get that value like this:

Implicit coversions are most definitely fair game for the exam - understand what can be implicitly converted and what requires explicit casting.
I hope that helps,
Corey
 
Linda Pan
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh I really meant ... the compiler ouputs 122 but how would I derive the same answer in order to come to the conclusion that the final answer when you compile the complete code is 4888.0? I understand the rules for going through each line ... but I don't understand the '(c / s)' in double v = (d*i) + (f* -b) - (c/s);
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In these type of questiosn generally you are given the value of char. You don't have to remember the value of each char. But to be on safe side take a look at the ascii codes
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sarma Lolla:
In these type of questiosn generally you are given the value of char. You don't have to remember the value of each char. But to be on safe side take a look at the ascii codes


Probably the best thing to do is remember what 'A' and 'a' are. As long as you can remember those, you can derive the rest, but you're not going to be asked a question like, "What is the ASCII value of 'q'?" on the exam. If it is important that you know what a character's value is, the exam will give you that information.
Corey
 
Linda Pan
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice to know. Thank you both very much for replying.
 
Sarma Lolla
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Probably the best thing to do is remember what 'A' and 'a' are. As long as you can remember those, you can derive the rest, but you're not going to be asked a question like, "What is the ASCII value of 'q'?" on the exam. If it is important that you know what a character's value is, the exam will give you that information


I would like to include '0'-'9' numbers also for the above 'A' and 'a'. Because
char c=\u0030; works fine but not
char c=\u0040;
Please observe the missing single quotes around the unicode chars above.
 
If you look closely at this tiny ad, you will see five bicycles and a naked woman:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic