Convert decimal integer to octal in Java

Java 8Object Oriented ProgrammingProgramming

To convert decimal to octal, use the toOctalString() method in Java. Let’s say the following is our decimal.

int decInt = 21;

To convert it to Octal, we need to use the toOctalString() method.

String myOct = Integer.toOctalString(decInt);

The following is the complete example that converts decimal to octal.

Example

 Live Demo

public class Demo {
   public static void main(String []args) {
      int decInt = 21;
      System.out.println("Decimal Integer = "+decInt);
      String myOct = Integer.toOctalString(decInt);
      System.out.println("Octal = "+myOct);
   }
}

Output

Decimal Integer = 21
Octal = 25
raja
Published on 10-Dec-2018 12:51:40
Advertisements