1

I have been working on a project and i saw some references on web and they initialized :

int val= 0x000; output 0
int val1= 0x001; output 1

How exactly java is converting this?

Thanks

Caffeinated
  • 11,982
  • 40
  • 122
  • 216
Rohit Garg
  • 115
  • 2
  • 10

5 Answers5

4

It's an hexadecimal (base 16 instead of base 10). Hexadecimals starts with 0x.... And it can contain these digits: 0123456789ABCDEF

Octals (base 8) starts with 0... and can containt digits less than 8 (01234567)

int dec = 123; // decimal: 1*(10^2) + 2*(10^1) + 3*(10^0) = 123
int oct = 0123; // octal: 1*(8^2) + 2*(8^1) + 3*(8^0) = 83
int hex = 0x123; // hexadecimal: 1*(16^2) + 2*(16^1) + 3*(16^0) = 291
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
2

You can do int val = 0; and int val = 1; with decimal notation..

The 0x before the number indicate an hexadecimal notation...

All notations are:

0b to binary: int i = 0b10101010110;

nothing to decimal: int i = 123;

0 to octal: int i = 0123345670;

0x to hexadecimal: int i = 0xAEF123;

Teo
  • 3,143
  • 2
  • 29
  • 59
0

As a matter of fact, Java does not "convert" but "interpret" the values (as hexadecimal).

Smutje
  • 17,733
  • 4
  • 24
  • 41
0

Numbers starting with 0x are hexadecimal. Java converts them (like decimal ones, too) to binary and saves them.

0

This hexadecimal number system (base 16)

Start with 0x...

(Octals start with 0...)

Link

Community
  • 1
  • 1
lgabster
  • 695
  • 7
  • 17