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

Code Learning #JAVA#3801

## 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

 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); } } .linedwrap { border: 1px solid #c0c0c0; padding-top: 5px; padding-bottom: 5px; border-radius: 4px; background-color:#F8F8F8; } .linedtextarea { padding: 0px; margin: 0px; background-color:#F8F8F8; } .linedtextarea textarea, .linedwrap .codelines .lineno { font-size: 12pt; font-family: monospace; line-height: normal !important; } .linedtextarea textarea { padding-right:0.3em; padding-top:0.3em; border: 0; } .linedwrap .lines { margin-top: 0px; width: 20px; float: left; overflow: hidden; border-right: 1px solid #c0c0c0; margin-right: 10px; } .linedwrap .codelines { padding-top: 5px; } .linedwrap .codelines .lineno { color:#AAAAAA; padding-right: 2px; padding-top: 0.0em; text-align: right; white-space: nowrap; } .linedwrap .codelines .lineselect { color: red; }

## 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

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

×