Java Integer.toOctalString() – Examples

In this tutorial, we will learn about Java Integer.toOctalString() method, and learn how to use this method to find the octal representation of given integer, with the help of examples.

toOctalString(int i)

Integer.toOctalString() returns a string representation of the integer argument as an unsigned integer in base 8.

ADVERTISEMENT

Syntax

The syntax of toOctalString() method with the integer as parameter is

Integer.toOctalString(int i)

where

ParameterDescription
iThe integer whose octadecimal representation has to be found.

Returns

The method returns value of type String.

Example 1 – toOctalString()

In this example, we will take an integer and find its octal representation using Integer.toOctalString() method.

Java Program

public class Example {
	public static void main(String[] args){
		int i = 21;
		String result = Integer.toOctalString(i);
		System.out.println("Result of toOctalString("+i+") = " + result);
	}
}

Output

Result of toOctalString(21) = 25

Example 2 – toOctalString() – Negative Integer

In this example, we will take a negative integer and find its octal representation using Integer.toOctalString() method. Since, the integer is negative, and considered as an unsigned integer in base 8 by the method, we get the complement form of the integer.

Java Program

public class Example {
	public static void main(String[] args){
		int i = -21;
		String result = Integer.toOctalString(i);
		System.out.println("Result of toOctalString("+i+") = " + result);
	}
}

Output

Result of toOctalString(-21) = 37777777753

Conclusion

In this Java Tutorial, we have learnt the syntax of Java Integer.toOctalString() method, and also how to use this method with the help of Java example programs.