This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
I have list = [1,0,1], but each number in the List are of type String. I do
if ( String.join("",list) == "101"){
return true
}
As you can see, these to Strings are equal but it won't return true.
try this: using equals
if ( String.join("",list).equals("101")){
return true
}
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:
How can I convert a char to int in Java? [duplicate]
(4 answers)
Java: parse int value from a char
(9 answers)
Closed 3 years ago.
I was experimenting with char arrays and integers, when I found something that confused me a lot. In Java, if you type the following code:
String inputs = "123";
char[] inputs2 = inputs.toCharArray(); // Turn String into Char Array
int a = inputs2[0];
println(a);
I would have expected for "a" to return 1 because 1 is the first element in the array. However, it returned 49. Why does this occur? Is there any way to get around this issue?
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
This question already has answers here:
BigDecimal equals() versus compareTo()
(4 answers)
Closed 7 years ago.
The following code:
BigDecimal a = new BigDecimal("8900");
BigDecimal b = new BigDecimal("8.9E+3");
System.out.println(a.equals(b));
prints false. Why is so, if mathematically those numbers are equal?
You have to use compareTo():
BigDecimal a = new BigDecimal("8900");
BigDecimal b = new BigDecimal("8.9E+3");
System.out.println(a.compareTo(b) == 0);
This question already has answers here:
Is Integer Immutable
(12 answers)
Closed 9 years ago.
I know Integer is immutable in Java. But I tried this:
Integer i = 4;
i++;
System.out.println(i); // output is 5
Why the self-increment is still working? Did Java create a new Integer object?
i++;
is equivalent to:
i = i + 1;
which i now refers to a different Integer object (5).