Integer literals decimal, octal, hexadecimal, binary – Computer Sir Ki Class
  

Login
[lwa]



Code Learning #JAVA#3801    siteicon   siteicon  

Integer literals decimal, octal, hexadecimal, binary

Use of integer literals in its different notation forms as decimal form, octal form, hexadecimal form and binary form.

Learning Objectives

  • Four basic forms of integer literals which are decimal, octal, hexadecimal and binary forms.

Source Code

TC++ #3801

Source Code

class IntegerLiterals
{
public static void main(String[] args)
{
int a=65;
int b=0101;
int c=0x41;
int d=0b01000001;
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
}
}

Test it !

Run Output

65
65
65
65

Code Understanding

int a=65;
This decimal form of integer literal where decimal value 65 is assigned to a;

int b=0101;
This is octal form of decimal value 65 which is 101 in octal. To represent in the form of octal literal we prefix it with zero (0). So we write it as 0101.

int c=0x41;
This is hexadecimal  form of decimal value 65 which is 41 in hex. To represent in the form of hexadecimal literal we prefix it with zero and x (0x). So we write it as 0x41.

int d=0b01000001;
This is binary  form of decimal value 65 which is 01000001 in binary. To represent in the form of binary literal we prefix it with zero and b (0b). So we write it as 0b01000001.

System.out.println(a); System.out.println(b);  System.out.println(c); System.out.println(d);
Here we print all the variables initialised with different forms of integer literals. All outputs will come as 65 as default form for literals printing is decimal type only.

Notes

  • The memory content of values are always in binary form, other three forms are simply for sake of easy understanding or easier arithmetic representations.
  • For hexadecimal and binary prefixes both upper and lowercase x and b are allowed. which means –
    – 0x41 and 0X41 are same and also
    -0b01000001 and 0B01000001 are same.
  • The hexadecimal number assignment can also be done in uppercase, lowercase or mixed case assignment. This means that 0xFACE, 0xface and 0xFace all will mean the same decimal number which would be 64206 .Students are encouraged to do these variations and try the above code.

Common Errors

  • Some times student try to prefix the character O instead of character zero (0) for octal numbers. Only zero (0) prefixing is correct.


Suggested Filename(s): IntegerLiterals.java



Share

sunmitra| Created: 11-Mar-2018 | Updated: 11-Mar-2018|






Back