iT邦幫忙

0

java stack integer comparison

  • 分享至 

  • xImage

Why using == to compare 2 int in the stack would return false but using >,< is fine?

import java.util.*;

class Solution {

    public static void main(String[] args) {
        Stack<Integer> a = new Stack<>();
        Stack<Integer> b = new Stack<>();
        Stack<Integer> c = new Stack<>();
        a.push(1026);
        b.push(1024);
        c.push(1026);
        System.out.println(a.peek());
        System.out.println(b.peek());
        System.out.println(c.peek());
        if (a.peek() > (b.peek())) {
            System.out.println(true);
        }else
        System.out.println(false);

        if (a.peek() == (c.peek())) {
            System.out.println(true);
        }else
        System.out.println(false);

        if (a.peek().equals(c.peek()) ) {
            System.out.println(true);
        }else
        System.out.println(false);
    }
}

1026
1024
1026
true
false
true

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
海綿寶寶
iT邦大神 1 級 ‧ 2022-04-26 13:21:28

簡單說
int 用 ==
Integer 用 compare/equals
資料來源

我要發表回答

立即登入回答