集册 Java实例教程 使用构造函数和valueOf()方法创建整数对象之间的区别

使用构造函数和valueOf()方法创建整数对象之间的区别

欢马劈雪     最近更新时间:2020-01-02 10:19:05

426
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用构造函数和valueOf()方法创建整数对象之间的区别

public class Main {

  public static void main(String[] args) {


    // Create two Integer objects using constructors
    /*来自 
     N o w J a v a . c o m - 时  代  Java*/

    Integer iv1 = new Integer(25);

    Integer iv2 = new Integer(25);

    System.out.println("iv1 = iv1 = " + iv1 + ", iv2 = " + iv2);

  

    // Compare iv1 and iv2 references

    System.out.println("iv1 == iv2: " + (iv1 == iv2));


    // Let us see if they are equal in values

    System.out.println("iv1.equals(iv2): " + iv1.equals(iv2));/*from 时 代 J a v a - N o w J a v a . c o m*/


    System.out.println("\nUsing the valueOf() method:");


    // Create two Integer objects using the valueOf() 

    Integer iv3 = Integer.valueOf(25);

    Integer iv4 = Integer.valueOf(25);

    System.out.println("iv3 = " + iv3 + ", iv4 = " + iv4);


    // Compare iv3 and iv4 references

 
展开阅读全文