java - Reference comparison using == operator -


this question has answer here:

public class autoboxingandunboxing  {     public static void main(string[] args)      {         integer x = 127;         integer y = 127;         system.out.println(x == y);//true          integer = 128;         integer b = 128;         system.out.println(a == b);//false         system.out.println(a); // prints 128     } } 

how come x==y true , a==b false? if based on value(integer -128 127) 'a' should print -128 right?

when comparing integer objects, == operator might work numbers between [-128,127]. @ jls:

if value p being boxed true, false, byte, or char in range \u0000 \u007f, or int or short number between -128 , 127 (inclusive), let r1 , r2 results of 2 boxing conversions of p. case r1 == r2.

since values you're comparing not in mentioned range, result evaluated false unless use integer#equals.


Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -