java.lang.Integer toOctalString()

Description :

This java tutorial shows how to use the toOctalString() method of Integer class under java.lang package. This method return a string equivalent in octal base number of the int method argument.

Method Syntax :

public static String toOctalString(int i)

Parameter Input :

DataType Parameter Description
int i the integer value that we want to convert into string in octal base number

Method Returns :

The toOctalString(int i) method simply returns the octal string equivalent of int method parameter.

Compatibility Version :

Requires Java 1.0.2 and up

Exception :


Discussion :

The toOctalString() method is static thus we should invoke it statically for example Integer.toOctalString(int i). This method simply converts the int parameter into octal or we could call it base 8. Make a note that counting numbers are of base 10. Normally we present octal number in upper case letter thus the following makes more sense Integer.toOctalString(int i).toUpperCase(). We have used the method toUpperCase() of String class to transform the result in invoking the toOctalString in upper case format.

The toOctalString() method would yield the same result in invoking Integer.toString(int i, int radix). This is just only a shortcut of calling the Integer.toString(int i,8). You might be asking when to use this method. Well, to answer this kind of concern, if you are only dealing with fix base 8, it would make sense to use the toOctalString() because it is simpler to look at. But if you are dealing with dynamic conversion then the toString(int i, int radix) method suits better.

There is maximum and minimum allowable value to be converted and this is can be determined using the two static method Integer.MAX_VALUE and Integer.MIN_VALUE respectively.

Java Code Example :

This java example source code demonstrates the use of toOctalString() method of Integer class. Basically we asked for a number through user input on the command shell and then we convert the same into Octal format.


import java.util.Scanner;

import static java.lang.System.*;

 * This example source code demonstrates the use of toHexString
 * of Integer class

public class IntegerToHexString {

	public static void main(String[] args) {
		// Ask user input
		// declare the scanner object
		Scanner scan = new Scanner(;
		// use scanner to get 
		int value = scan.nextInt();
		// close the scanner object
		// print the value in hex format
		out.println("Hex Converted:" + Integer.toHexString(value).toUpperCase());



Sample Output :

Running the toOctalString() method example source code of Integer class will give you the following output

Octal Conversion:173

Exception Scenario :

Exception in thread "main" java.util.InputMismatchException: For input string: "2147483648"
	at java.util.Scanner.nextInt(Unknown Source)
	at java.util.Scanner.nextInt(Unknown Source)

Similar Method :

  • N/A

Suggested Reading List :

References :