Convert decimal integer to octal in Java


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

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 26-Jun-2020

230 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements