• 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

Java Literals

 
Ranch Hand
Posts: 30
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Test1{
public static void main(String jlc[]){
int a='7';
System.out.println(a);
}}


Please explain why the output is 55

 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It prints 55 because this is the number assigned to character '7' (DIGIT SEVEN).
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that you are assigning a character to your variable a, not an integer value. If you want the value of a to be 7, you should write:

int a = 7;

(without the singel quotes)
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To add to what poeple are telling you; the char literal '7' is not a character, but a number, as you will see from the Java® Language Specification (=JLS); the JLS includes it as an unsigned integer type. There are many places where you can find out the number equivalents of a character; this Unicode page has '7' on. You will notice it is shown as 37 which is hexadecimal, which equates to 55 in decimal.
 
Ranch Hand
Posts: 393
9
Open BSD BSD Debian
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the computing world characters are mapped/coded to numbers trough various encoding schemes depend on the language on hand. Java utilize the UTF-16  encoding scheme
what happens in

is called implicit type conversion let's examine it
Java is a strong typed language . Every entity in Java have to have a type.
A type is a set of permissible values and a set of permissible operations that operate on that values. That  is enforced by the compiler if you use a non valid value or operation
the compiler complains and not compile your code
You have declared the variable a as type integer that means the variable a can take only integer values like ...-1,0,1... but you give it a value of type char instead.
NOTE
   Normally if the type on the right hand side of an assignment  is not equal(more correctly compatible) to the type on the left hand side the compiler raises an error and not compile
   because you're out of the type compatibility rules. That's a characteristic of all strong typed languages and even if can be seemed hard to you the prime times  help much to avoid
   errors that have to be raised at run time. imagine a critical situation such as an such error in an nuclear plant would be catastrofic
Now turn in us because char and int are compatible types the compiler doesn't complain instead help you in hiding your error(correctly you had to declare a of type char if you intend
to assign it a character such as '7') in transform your character '7' in an integer that corresponds in an int i.e. 55 on the UTF-16 conversion table  


 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic