This question already has answers here:
Why Integer class caching values in the range -128 to 127?
(5 answers)
Closed 7 years ago.
Can anyone explain why the o/p is like this for the code below:
public static void main(String[] args) {
Integer i1=127;
Integer i2=127;
Integer i3=128;
Integer i4=128;
System.out.println(i1==i2);
System.out.println(i3==i4);
}
O/p
true
false
Values from -128 to 127 for int are cached for boxing. That's why the first comparison returns true.
http://www.mohawksoft.org/?q=node/70
Related
This question already has answers here:
Why is 128==128 false but 127==127 is true when comparing Integer wrappers in Java?
(8 answers)
How can I properly compare two Integers in Java?
(10 answers)
Closed 7 months ago.
for(int i=0;i<luckNumber.size();i++) {
for(int j=0;j<luckNumber1.size();j++) {
if(luckNumber.get(i)==luckNumber1.get(j)) {
ans.add(luckNumber.get(i));
return ans;
}
}
}
The lucknumber and luckNumber1 are arraylist and for the value of 65277 the if condition is not able to compare as it should compare two integer and this code is working fine for 2 digit number contained in arraylist so i want to know why the code is not able to compare two simple integers
ans is another arraylist where i will store the result coming from if condition
This question already has answers here:
Division operation is giving me the wrong result [duplicate]
(3 answers)
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 5 years ago.
Here is my code, Eclipse always return integer value of results, even if h is double. Please help me to fix this.
public static void main(String[] args) {
double h=0.0;
for(int i=1;i<=1000;i++) {
h=h+ 1/i;
}
System.out.println("Harmonic sum "+h);
System.out.println("Harmonic sum "+String.format("%.4f", h));
Result:
Harmonic sum 1.0
Harmonic sum 1,0000
You need to cast the result of your division into double
Try this out:
h=h+ (double)1/i;
I hope this is what you were looking for.
This question already has answers here:
Odd behavior when Java converts int to byte?
(12 answers)
How are integers cast to bytes in Java?
(8 answers)
Why will byte not take 0xff in java?
(4 answers)
Closed 5 years ago.
I have this piece of code in java. I get the output as -1 (I was expecting an error though..). explain please! why?
public class Main {
public static void main(String[] args) {
int a=0xfffff;
byte b = (byte)a;
System.out.println(b);
}
}
This question already has answers here:
Why are integer literals with leading zeroes interpreted strangely?
(8 answers)
Closed 7 years ago.
public static void main(String str[]){// String type
System.out.println("Main()");
main(0100);}
public static void main(int a){//integer type
System.out.println(a);
}
answer should be 100 rather 64
In Java, if you put a 0 before a primitive literal, it is interpretted as octal. 100 in octal is 64 in decimal.
This question already has answers here:
Integer wrapper objects share the same instances only within the value 127? [duplicate]
(5 answers)
Closed 8 years ago.
I can't figure out why the output is different.
The output is same only in the range -128 to 127.
public class Check {
public static void main(String[ ] args) {
Integer i1=122;
Integer i2=122;
if(i1==i2)
System.out.println("Both are same");
if(i1.equals(i2))
System.out.println("Both are meaningful same");
}
}
Output:
Both are same
Both are meaningful same
public class Check {
public static void main(String[] args) {
Integer i1=1000;
Integer i2=1000;
if(i1==i2)
System.out.println("Both are same");
if(i1.equals(i2))
System.out.println("Both are meaningful same");
}
}
Output:
Both are meaningful same
You've encountered a caveat in the Java language where autoboxing for "small" values has a slightly different rule than autoboxing. ("Small" in this case means a number in the range of 127 to -128, as in a signed byte in C.) From JLS 5.1.7 Boxing Conversion:
If the value p being boxed is true, false, a byte, a char in the range \u0000 to \u007f, or an int or short number between -128 and 127, then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.
(Emphasis is mine.)
In the second example (where i1=i2=1000), the if(i1==i2) comparison results in false because the value of both objects is greater than 127. In that case == is a referential comparison, i.e. checking to see if the objects are actually the same object.