JavaHow to Convert Decimals to Octal in Java

How to Convert Decimals to Octal in Java

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Java Developer Tutorials

Radix conversion is an important aspect of programming, regardless of the language a developer uses. Java provides some utility methods that can help support these functions and conversions. In today’s quick tip Java programming tutorial, we will show programmers an example of how to convert decimal values to octal values.

The code below demonstrates how to convert decimal values to octal values using Java:

*/

public class ConvertToOctal
{
	
	int decimalNum = 345;
	int octalRadix = 8;
	
	public static void main(String args[])
	{
		ConvertToOctal convertToOctal = new ConvertToOctal();
		convertToOctal.proceed();
		
	}
	
	private void proceed()
	{
		String octalNum = Integer.toString(decimalNum, octalRadix);
        System.out.println("Octal value of " + decimalNum + " is " + octalNum);
	}
}

/*

Running this code in your code editor or integrated development environment (IDE) will result in the following output:

[root@mypc]# java ConvertToOctal
Octal value of 345 is 531

Read more Java programming tutorials and quick tips.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories