📜  说明八进制整数用法的Java程序

📅  最后修改于: 2022-05-13 01:54:51.067000             🧑  作者: Mango

说明八进制整数用法的Java程序

八进制是一种数字系统,其中数字以 8 的幂表示。因此所有整数都可以表示为八进制数。此外,八进制数中的所有数字都在 0 和 7 之间。在Java中,我们可以通过在初始化时添加 0 来存储八进制数。它们被称为八进制字面量。用于存储的数据类型是 int。

十进制到八进制的转换2

用于将十进制转换为八进制的方法是 Integer.toOctalString(int num)

句法:

public static String toOctalString(int num)

参数:该方法接受整数类型的单个参数num ,需要将其转换为字符串。

返回值:该函数将整数参数的字符串表示形式返回为基数为 8 的无符号整数。

示例 1

Java
import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
        // Variable Declaration
        int a;
 
        // storing normal integer value
        a = 20;
        System.out.println("Value of a: " + a);
 
        // storing octal integer value
        // just add 0 followed octal representation
        a = 024;
        System.out.println("Value of a: " + a);
 
        // convert octal representation to integer
        String s = "024";
        int c = Integer.parseInt(s, 8);
        System.out.println("Value of c: " + a);
 
        // get octal representation of a number
        int b = 50;
        System.out.println(
            "Octal Representation of the number " + b
            + " is: " + Integer.toOctalString(b));
    }
}


Java
// Arithmetic operations on Octal numbers
 
import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
        int a, b;
 
        // 100
        a = 0144;
 
        // 20
        b = 024;
 
        System.out.println("Value of a: " + a);
        System.out.println("Value of b: " + b);
        System.out.println("Addition: " + (a + b));
        System.out.println("Subtraction: " + (a - b));
        System.out.println("Multiplication: " + (a * b));
        System.out.println("Division: " + (a / b));
    }
}



输出
Value of a: 20
Value of a: 20
Value of c: 20
Octal Representation of the number 50 is: 62

例2:也可以对这个八进制整数进行不同的算术运算。操作与对 int 数据类型执行的操作相同。

Java

// Arithmetic operations on Octal numbers
 
import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
        int a, b;
 
        // 100
        a = 0144;
 
        // 20
        b = 024;
 
        System.out.println("Value of a: " + a);
        System.out.println("Value of b: " + b);
        System.out.println("Addition: " + (a + b));
        System.out.println("Subtraction: " + (a - b));
        System.out.println("Multiplication: " + (a * b));
        System.out.println("Division: " + (a / b));
    }
}


输出
Value of a: 100
Value of b: 20
Addition: 120
Subtraction: 80
Multiplication: 2000
Division: 5