Weil Jimmer's BlogWeil Jimmer's Blog


int 和 Integer 的比較 Java 程式設計
No Comments

Published:
Updated:2017-05-19 19:47:56

int test_int_1 = 1;
int test_int_2 = 1;
System.out.println(test_int_1==test_int_2);//true
int test_int_3 = new Integer(1);
int test_int_4 = new Integer(1);
System.out.println(test_int_3==test_int_4);//true
int test_int_5 = new Integer("1");
int test_int_6 = new Integer("1");
System.out.println(test_int_5==test_int_6);//true
int test_int_7 = Integer.valueOf(1);
int test_int_8 = Integer.valueOf(1);
System.out.println(test_int_7==test_int_8);//true
int test_int_9 = Integer.valueOf("1");
int test_int_10 = Integer.valueOf("1");
System.out.println(test_int_9==test_int_10);//true
Integer test_integer_1 = 1;
Integer test_integer_2 = 1;
System.out.println(test_integer_1==test_integer_2);//true
System.out.println(test_integer_1.equals(test_integer_2));//true
Integer test_integer_3 = new Integer("1");
Integer test_integer_4 = new Integer("1");
System.out.println(test_integer_3==test_integer_4);//false
System.out.println(test_integer_3.equals(test_integer_4));//true
Integer test_integer_5 = Integer.valueOf("1");
Integer test_integer_6 = Integer.valueOf("1");
System.out.println(test_integer_5==test_integer_6);//true
System.out.println(test_integer_5.equals(test_integer_6));//true

據我實驗觀察,所得 只要是 int==int 的比較,一定是比較值,若為Integer==int,也一定是比較其值。

唯一為False的情況是當有new出現時:

new Integer("1")==new Integer("1");//false

用 valveOf 得出來的 Integer 類別,進行比較(==),也是 valueOf括號內的值(不管類別是String還是int…)(都小於等於127大於等於負128的情況)是否相等。

test_integer_6 = Integer.valueOf("1");
Integer test_integer_7 = Integer.valueOf(1);
System.out.println(test_integer_6==test_integer_7);//true
System.out.println(test_integer_6.equals(test_integer_7));//true

System.out.println(test_integer_1==test_integer_3);//false
System.out.println(test_integer_1.equals(test_integer_3));//true
System.out.println(test_integer_5==test_integer_3);//false
System.out.println(test_integer_5.equals(test_integer_3));//true
System.out.println(test_integer_7==test_integer_3);//false
System.out.println(test_integer_7.equals(test_integer_3));//true

若 Integer 比較 Integer ,其中之一關鍵字有 new ,== 比較子出來的結果可能就為 False 。

以下為特殊例子!數值小於等於127大於等於負128時不會new,但若不在此區間時,就會以new宣告。

以new宣告的時候==通常都是false,

進行 比較 int 時若其值相同會轉發True。

Integer test_special_1 = Integer.valueOf(127);//等於Integer test_special_1 = 127;
Integer test_special_2 = Integer.valueOf(127);
System.out.println(test_special_1==test_special_2);//true
System.out.println(test_special_1.equals(test_special_2));//true
Integer test_special_3 = Integer.valueOf(128);//等於Integer test_special_1 = 128;
Integer test_special_4 = Integer.valueOf(128);
System.out.println(test_special_3==test_special_4);//false
System.out.println(test_special_3.equals(test_special_4));//true

若本人有誤,歡迎更正。


This entry was posted in Experience, Functions, Java By Weil Jimmer.

About Weil Jimmer

avatar

Hi! Everyone! My name is Weil Jimmer. This is my personal blog. I'm a webmaster of this site. I hope the site will be popular. Now, Let's go! Enjoy gaining more knowledge.
More Details About Me : https://weils.net/profile.php

Leave a Reply

Name*:

Email*:

Website:

Privacy:   

Comment*:

Visitor Count

pop
nonenonenone

Note

WPA2 已被 KRACK 攻擊破解!請更新 WiFi 軟體。

歡迎前來本站。

Search

Republic Of China
Privacy is your right and ability to be yourself and express yourself without the fear that someone is looking over your shoulder and that you might be punished for being yourself, whatever that may be.

It is quality rather than quantity that matters.

I WANT Internet Freedom.

Reality made most of people lost their childishness.

Justice,Freedom,Knowledge.

Support/Donate

This site also need a little money to maintain operations, not entirely without any cost in the Internet. Your donations will be the best support and power of the site.
MethodBitcoin Address
bitcoin1NRMVGGpm2T1pmeejisLSEhCXfCefEW9V4
paypal

The Lie We Live

youtube

The Questions We Never Ask

youtube

Support The Zeitgeist Movement

The Zeitgeist Movement

Man

youtube

In The Fall

youtube

Facebook is EATING the Internet

Facebook

Recent Comments

sickcatail on STEAM免費序號

包包 on STEAM免費序號

問問題 on 最棒的手機通訊軟體 Telegram

unocme on STEAM免費序號

窩仔 on STEAM免費序號

Categories

Announcement (4)

Bash (2)

C (1)

C# (4)

C++ (1)

Experience (38)

Flash (2)

Free (10)

Functions (35)

Games (13)

General (44)

HTML (7)

Java (12)

JS (7)

Mood (23)

Note (24)

Office (1)

PHP (9)

Privacy (3)

Product (9)

Python (4)

Software (8)

The Internet (15)

Tools (12)

VB.NET (8)

WebHosting (7)

Wi-Fi (5)

XML (4)