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.


 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);


Decimal Integer = 21
Octal = 25
Updated on 26-Jun-2020 07:24:36