what is the difference between Integer and int in java?

Posted chenqr

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了what is the difference between Integer and int in java?相关的知识,希望对你有一定的参考价值。

int is a primitive type, Variables of int type store the actual binary value for the Integer type you want to represent.

Integer is a class, no diffeeent from any other in the java language. Variables of type Integer store the references to Integer Objects.

Note that every primiry type has wrapper class:

  byte has Byte

  long has Long

  boolean has Boolean

  float has Float

  double has Double

Wrapper class inherit from Object class, and primitive don‘t. So you can be used in collections with Object reference.

 

1. Integer与Integer的比较

public static void compare() {
        Integer i = new Integer(100);
        Integer i2 = new Integer(100);
        System.out.println( i == i2);    //false
}

2. int与Integer的比较

public static void compareIntWithInteger() {
        Integer i = new Integer(100);
        int i2 = 100;
        System.out.println( i == i2);    //true
}

 


以上是关于what is the difference between Integer and int in java?的主要内容,如果未能解决你的问题,请参考以下文章

What is the difference between Reactjs and Rxjs?

What is the maximum length of a URL in different browsers?

What is the difference between sed and awk

What is the difference between sed and awk

What is the difference between J2EE and Spring

What is the difference between Shrink Database and File?