This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Variable convertion

 
Marky Mark
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, i was wondering if it is possilbe to convert an Integer to a charachter?
ie - so '1' = 'a'
and '2' = 'b' and so on
i know this much : number=integer.parseInt(number); < this , i think,
converts a srting to an int, i think its something like this
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marky
Because an int to a char is considered a narrowing conversion you need to cast the int to a char.
int i = 65;
char itoc = (char)i;
System.out.println(itoc);
// prints A
Going the other way is easier because you dont need to cast an a char to an int, as it is conseidered a widening conversion.
char a = 'A';
int atoi = a;
System.out.println(atoi);
// prints 65
hope that helps
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic