Saturday, December 31, 2016

Number System in JAVA

Number System in JAVA

-------------------------------------------------------------------------------------------------------
To represent numbers in programming language programming language must follow a particular system called as "Number Systems".

There are four types of number system to represents numbers:

1. Binary Number System
2. Octal Number System
3. Decimal Number System
4. Hexa Decimal Number System

All the above four number system are allowed in java but the default number system is "Decimal Number System".

1. Binary Number:
To represent numbers in Binary Number system, we have to use the symbols like 0's & 1's, but the number must be prefixed either with Ob or OB.

EX:
int a = 10; --------> It is not valid number, it is decimal number.
int b = 0b1010; ----> valid
int c = 0B1010; ----> valid
int d = Ob1010; ----> invalid

Note: Binary number system is not supported by JAVA upto JAVA6 version, but it is supported by JAVA7 and above versions.

2. Octal Number:
To represent numbers in octal number system, we have to use a symbols like 0,1,2,3,4,5,6,7,8,9 but the number be prefixed with 0.

EX:
int a = 10; ------->  it is not octal number, it is decimal number.
int b = 01234; ----> valid
int c = O3456; -----> invalid
int d = 03458; -----> invalid(8 is not included in octal number)

3. Decimal Number:
To represent number in decimal number system, we have to use the numbers like 0,1,2,3,4,5,6,7,8,9. the provided number must not have any prefix value.


4 Hexa Decimal Number:
To represent number in Hexa decimal number system, we have to use the symbols like 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f but the number must be prefixed either with '0x' or '0X'.

EX:
int a = 10; -----> it is not hexa decimal number, it is decimal number.
int b = 0x1020; ---> valid
int c = 0X1020; ---> valid
int d = 0x12abcde; ---> valid
int e = 0x12efg; ----> invalid

Note : if we provide any number system in java application the compiler will recognize that number and its number system on the basis of its prefix value and compiler will convert that number into decimal number system and compiler will process that number as like decimal number.

No comments:

Post a Comment