Why are the 2 Characters not equal using ==? [duplicate] - java

This question already has answers here:
Comparing Character, Integer and similar types in Java: Use equals or ==?
(9 answers)
Boxed Primitives and Equivalence
(4 answers)
Using == operator in Java to compare wrapper objects
(8 answers)
Compare reference two character with same value in Java
(3 answers)
Closed 5 years ago.
Why does this code block return false...
char ra1= '\u30E9';
char ra2= '\u30E9';
Character RA1= ra1;
Character RA2= ra2;
System.out.println("Does Character RA1 == Character RA2? " + (RA1 == RA2)); //returns false
...when this code block returns true?
char a1= 'a';
char a2= 'a';
Character A1= a1;
Character A2= a2;
System.out.println("Does Character A1 == Character A2? " + (A1 == A2)); //returns true
They seem like they're doing the same thing to me, and both Characters hold the same char value so I don't understand why == returns false for one and true for the other

Related

Integer.toString() vs. Integer.parseInt() [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 2 years ago.
How come the first condition is false and the second is true? I was sure they were both true.
System.out.println(Integer.toString(3) == "3");
System.out.println(Integer.parseInt("3") == 3);
Integer.parseInt converts a String to a primitive int and primitives can be compared with ==. However, Integer.toString produces a String object and == for objects checks that they are the exact same reference; use String#equals instead to compare the values of the Strings.
System.out.println(Integer.toString(3).equals("3"));
System.out.println(Integer.parseInt("3") == 3);
The above code outputs:
true
true

How do I return a number representing the count of tiles that match the letter that was passed in to the method? [duplicate]

This question already has answers here:
What is the difference between == and equals() in Java?
(26 answers)
Closed 4 years ago.
For Strings you have to use equals to compare them, because == only compares the references.
Does it give the expected result if I compare chars with == ?
I have seen similar questions on stackoverflow, E.g.
What is the difference between == vs equals() in Java?
However, I haven't seen one that asks about using == on chars.
Yes, char is just like any other primitive type, you can just compare them by ==.
You can even compare char directly to numbers and use them in calculations eg:
public class Test {
public static void main(String[] args) {
System.out.println((int) 'a'); // cast char to int
System.out.println('a' == 97); // char is automatically promoted to int
System.out.println('a' + 1); // char is automatically promoted to int
System.out.println((char) 98); // cast int to char
}
}
will print:
97
true
98
b
Yes, but also no.
Technically, == compares two ints. So in code like the following:
public static void main(String[] args) {
char a = 'c';
char b = 'd';
if (a == b) {
System.out.println("wtf?");
}
}
Java is implicitly converting the line a == b into (int) a == (int) b.
The comparison will still "work", however.

How does the compareTo method work for Strings? if a word comes after another word in the alphabet then it returns positive or negative? [duplicate]

This question already has answers here:
How does the compareTo() method, compare strings? [closed]
(2 answers)
Closed 4 years ago.
Let's say i write this:
String a = "Hello";
String b = "Goodbye";
int compare = a.compareToIgnoreCase(b);
System.out.println(compare);
What will the printout be?
It compares character by character for each string.
For example in this case
When a = "hello" and b = "Goodbye"
It checks first character of a with first character of b and computes the relative difference, if it's same then it checks the next character, else it computes the difference and returns it.
If string a is greater than string b it returns a positive difference else it returns a negative difference.
in this case the out are be 1, because H comes after of G

Char casting in java [duplicate]

This question already has answers here:
Why does the ternary operator unexpectedly cast integers?
(3 answers)
Closed 5 years ago.
In my java code I have
result.append((num%b2)>=10?((char)('A'+((num%b2)-10))):(num%b2));
Where num and b2 are both integers. When num%b2 is greater than or equal to 10
((char)('A'+((num%b2)-10)))
is appended to the stringbuilder result. However instead of appending a char it appends an int value with the ASCII number of the char I wanted to return. Why will it not return the char?
It's because (num%b2) is an int.
The type of a conditional expression is the common type by which the two operands can be represented. So, if the "true" operand is a char but the "false" operand is an int, the result of someCondition ? someChar : someInt is an int.
It's a lot clearer if you just write it as a plain old if-else statement:
if (num%b2 >= 10) {
result.append((char)('A'+((num%b2)-10)));
} else {
result.append(num%b2);
}
Just convert it to string, and it will append it as string:
(char)('A'+((num%b2)-10)) + "" // this will do nice and quick conversion

Int and Char comparison by "==" [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
Why is this ok? What exactly does it compare?
int i = 10;
char c = 10;
if( c == i)
System.out.println("We are Equal");
And the same in this situation:
String s1 = "Null";
String s2 = new String(s1);
if( s1 == s2)
System.out.println("We are Equal");
I get that we're not comparing the contents of the variable.
In the first example, the two literal values of the integers are being compared, I.e. 10 == 10.
In the second example, your are comparing String objects, meaning the value of the actual Objects, not its content, are getting compared. In order to compare the content of these string objects, I.e. "Blah" == "Blah", you should use the string method String.compare(String strToCompare
Strings are objects. You are comparing references for the strings. Char/ints are primitives so no objects.

Categories