Number System in Java Programming Language

Hemanth RajuHemanth Raju
1 min read

Table of contents

Number System in Java

There are four types of number systems in java programming Language.

  1. Binary Number System

  2. Octal Number System

  3. Decimal Number System

  4. Hexadecimal Number System

Binary Number System :

  • In this number system, we use “0b”(2 as base) as prefix to convert the binary into integer format.
class Demo{
     public static void main(String[] args){
              int a=0b1010;
              System.out.println(a);
        }
}
  • Output for above example is 10.

Octal Number System :

  • In this number system, we use “0”(8 as base)as prefix to convert the octal format into integer format.
class Demo{
     public static void main(String[] args){
              int a=033;
              System.out.println(a);
         }
}
  • Output for above example is 27.(not 33)

Decimal Number System :

  • In this number system, it is a normal decimal values(10 as base)which we call as integers.

  • Example is given below.

class Demo{
     public static void main(String[] args){
              int a=123;
              System.out.println(a);
        }
}
  • Output is 123

Hexa-Decimal Number System :

  • In this Number system, we use “0x”(16 as base)as prefix to convert the hexadecimal format into integer.

  • In the below code we can see the example.

class Demo{
     public static void main(String[] args){
              int a=0xAB;
              System.out.println(a);
       }
}
  • Output for the above program is 171.(A=10, B=11=> 16A+1B==171).
0
Subscribe to my newsletter

Read articles from Hemanth Raju directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Hemanth Raju
Hemanth Raju

I am a Software Engineer by profession. Loves to write articles on tech concepts and also contribute to open-source