📜  Java整数和整数的区别示例

📅  最后修改于: 2021-09-13 03:12:13             🧑  作者: Mango

在Java, int 是原始数据类型,而 Integer 是 Wrapper 类

  • int,作为原始数据类型的灵活性较低。我们只能在其中存储整数的二进制值。
  • 由于 Integer 是 int 数据类型的包装类,它为我们在存储、转换和操作 int 数据方面提供了更大的灵活性。
  • Integer 是一个类,因此它可以调用类中定义的各种内置方法。 Integer 类型的变量存储对 Integer 对象的引用,就像任何其他引用(对象)类型一样。

例子:

// Valid
int n = 20;
//valid
Integer n = 45;

// Valid
Integer.parseInt("10");
// Not Valid
int.parseInt("10");

重要的不同点:

  1. 转换为字符串变量:我们不能直接或什至通过转换将字符串值(仅包含整数)分配给 int 变量。但是,我们可以使用 Integer(String) 构造函数将 String 分配给 Integer 类型的对象。我们甚至可以使用 parseInt(String) 将 String字面量转换为 int 值。
    // Java program to illustrate 
    // difference between
    // int and Integer 
      
    public class Main {
        public static void main(String args[])
        {
      
            Integer a = new Integer("123");
            // Casting not possible
            // int a = (int)"123";
            // Casting not possible
            // int c="123";
      
            // Casting possible using methods
            // from Integer Wrapper class
            int b = Integer.parseInt("123");
            System.out.print(a + new Float("10.1"));
        }
    }
    
    输出:
    133.1
    
  2. 将值直接转换为其他基数:我们可以分别使用 toBinaryString()、toOctalString() 或 toHexString() 将存储在 Integer 类中的整数值直接转换为二进制、八进制或十六进制格式。这在 int 类型的变量中是不可能的。
    // Java program to illustrate 
    // difference between
    // int and Integer 
      
    public class Main {
        public static void main(String args[])
        {
            String bin = Integer.toBinaryString(123);
            String oct = Integer.toOctalString(123);
            String hex = Integer.toHexString(123);
            System.out.print(bin + "\n" + oct + "\n" + hex);
        }
    }
    
    输出:

    1111011
    173
    7b
    
  3. 对数据执行操作: Integer 类还允许我们分别使用 reverse()、rotateLeft() 和 rotateRight() 反转我们的数字或向左或向右旋转它。我们需要定义自己的逻辑来对 int 变量执行这些操作,因为它不是内置类。
    // Java program to illustrate 
    // difference between
    // int and Integer 
      
    public class Main {
        public static void main(String args[])
        {
            // mainethods convert integer to its binary form,
            // apply the desired operation 
            // and then returns the decimal form
            // of the newly formed binary number
            // (12)10->(1100)2 -> 
            // rotate left by 2 units -> (110000)2->(48)10
            int rL = Integer.rotateLeft(12, 2);
      
            // (12)10->(1100)2 -> 
            // rotate right by 2 units -> (0011)2->(3)10
            int rR = Integer.rotateRight(12, 2);
      
            //(12)10 -> (00000000000000000000000000001100)2 
            // -> reverse ->(00110000000000000000000000000000)2
            // -> (805306368)10
              
            // int is of 32 bits
            int rev = Integer.reverse(12);
            System.out.print("Left Rotate : " + rL 
              + "\nRight rotate : " + rR + "\nReverse : " + rev);
        }
    }
    
    输出:
    Left Rotate : 48
    Right rotate : 3
    Reverse : 805306368
    
  4. 灵活性:整数包装类为我们提供了对现有 int 数据类型的更大灵活性。除了预定义的运算符之外,我们还可以对 int 值执行许多操作。 Integer 类用于需要将 int 变量视为对象的地方。由于 Wrapper 类继承了 Object 类,因此它们可以在具有 Object 引用或泛型的集合中使用。因此,我们向现有的 int 数据类型添加了可空性属性。
    从Java 5 开始,我们就有了自动装箱的概念,其中原始数据类型会自动转换为包装类,反之亦然。因此,我们可以在任何原始数据类型和任何 Wrapper 类之间执行任何算术或逻辑运算。
    // Java program to illustrate
    // auto-boxing
    import java.util.function.Function;
    import java.util.function.Function;
    public class Main {
        public static void main(String args[])
        {
      
            Integer a = new Integer("12");
            Integer d = new Integer("13");
            int b = 2;
            double c = 3.1;
            Double f = new Double("12.1");
            int d2 = a + d;
            System.out.println("Sum of 2 Integer objects :" 
                + (a + d));
            System.out.println("Sum of an Integer object 
                and int value :" + (a + b));
            System.out.println("Sum of an Integer object 
                and double value :" + (a + c));
            System.out.println("Sum of an Integer object 
                and Double object :" + (a + f));
        }
    }
    
    输出:
    Sum of 2 Integer objects :25
    Sum of an Integer object and int value :14
    Sum of an Integer object and double value :15.1
    Sum of an Integer object and Double object :24.1
    

除了 Integer 之外,我们还有更多Java对应于数据类型的包装类。它们如下:

Java中基本类型的等效包装类