search
HomeJavaJavagetting StartedExamples to explain the integer and int types in java

Examples to explain the integer and int types in java

Comparison of Integer and Int types

The Integer type is an object class. It is a boxing package of the basic type of an int. When we call the integer object When, Integer will point to the address of the int basic type it wraps.

If you compare Integer with int type, the system will automatically convert Integer into int type. At this time, when we compare int type, we will automatically compare the value of the address instead of the memory. Compare. Observe the following example:

(Recommended tutorial: java quick start)

public static Integer getIntegerExample1 = 128 ;
public static int getIntExample1 = 128 ;
System.out.print("结果: ");
System.out.println(getIntegerExample1 == getIntExample1);
结果: true

When we compare two Integer types, then the system will Compare the memory addresses. Because the memory allocation addresses are different, the results are different. Observe the following example:

public static Integer getIntegerExample3 = 128 ;
public static Integer getIntegerExample_3 = 128 ;
System.out.print("结果: ");
System.out.println(getIntegerExample3 == getIntegerExample_3);
结果: false

However, we have another situation, that is, when the size of the Integer value is between -127-127 When , Integer will be selected directly from the constant pool. Then when you compare the values ​​​​of two Integer in the constant pool, it will indicate that the two Integer point to the same memory address.

public  static Integer getIntegerExample2 = 127 ;
public static Integer getIntegerExample_2 = 127;
System.out.print("结果: ");
System.out.println(getIntegerExample2 == getIntegerExample_2);
结果: true

Related video tutorial recommendations: java video tutorial

The above is the detailed content of Examples to explain the integer and int types in java. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.