Analyze this code, Don’t give up, read & do some research on - TopicsExpress



          

Analyze this code, Don’t give up, read & do some research on this & Run this program also, based on output, understand this below concept -------------------------------------------------------------------------- Integer i1 = 127; Integer i2 = 127; Integer i3 = 128; Integer i4 = 128; System.out.println(i1 == i2); //true System.out.println(i3 == i4); //false Long i5 = 127l; Long i6 = 127l; Long i7 = 500l; Long i8 = 500l; System.out.println(i5 == i6); //true System.out.println(i7 == i8); //false Byte i9 = 100; Byte i10 = 100; System.out.println(i9 == i10); //true Short i11 = 70; Short i12 = 70; Short i13 = 700; Short i14 = 700; System.out.println(i11 == i12); //true System.out.println(i13 == i14); //false Float i15 = (float) 125; Float i16 = (float) 125; Float i17 = (float) 128; Float i18 = (float) 128; System.out.println(i15 == i16); //false System.out.println(i17 == i18); //false Double i19 = 127d; Double i20 = 127d; Double i21 = 128d; Double i22 = 128d; System.out.println(i19 == i20); //false System.out.println(i21 == i22); //false ----------------------------------------------------------------------------- What I found is if we create object for byte, short, int, long (whole numbers), it is checking the range. If the range is between 0-126, and content is same means, it is not creating new object. It is referring the same object, Here Integer i1 = 127; Integer i2 = 127; I1, i2 values are same & it value (127) is between 0-126, so only one object is created, i1 & i2 both are referring same object, that is why System.out.println(i1 == i2); gives true. Here Integer i3 = 128; Integer i4 = 128; I3, i3 values are same but it value (128) is not between 0-126, so two objects are created, i1 & i2 both are referring different object, that is why System.out.println(i3 == i4); gives false This 0-126 range is for byte type. So it is checking the content value between 0-126 for byte, short, int, long types (whole numbers), but it is not checking for float & double (floating point values). For float & double, even values are same & between 0-126, it is not worrying about that, simply creating new objects for both float & double. Here Float i15 = (float) 125; Float i16 = (float) 125; it is not checking for float & double (floating point values). For float & double, even values are same & between 0-126, it is not worrying about that, simply creating new objects for both float & double. System.out.println(i15 == i16); That is why output is false. Refer the Image for Better Understanding.. Give ur comments..... For more info, Join here https://facebook/groups/sashi.kumar/
Posted on: Wed, 26 Mar 2014 09:51:41 +0000

Trending Topics



Recently Viewed Topics




© 2015