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
Related
This question already has an answer here:
What is the type of a ternary expression with int and char operands? [duplicate]
(1 answer)
Closed 2 years ago.
This subject is already asked but I didn't understand it.
You'll find my program below. When I run this program it prints X and 88. When I remove do the declaration of int i = 0 it returns X and X. I found some explanations here : Unexpected output when using a ternary operator and final variable but i didn't understand it so much. I don't understand why this program returns X and 88 instead of X and X.
public class Ternary {
public static void main(String[] args) {
char x = 'X';
int i = 0;
System.out.println(true ? x : 0);
System.out.println(false ? i : x);
}
}
please read the rules of ternary operator here
https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.25
1st one is result of If one of the operands is of type T where T is byte, short, or char, and the other operand is a constant expression of type int whose value is representable in type T, then the type of the conditional expression is T. T being type char in this case and constant expression being 0
so the output is of type char which is x
2nd one is case for Otherwise, binary numeric promotion is applied to the operand types, and the type of the conditional expression is the promoted type of the second and third operands.
read about binary numeric promotion here https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.6.2
last case of point 2 applies here Otherwise, both operands are converted to type int. and converting char to int gives its ascii value which is 88
and hence the output is 88
This question already has answers here:
How can I properly compare two Integers in Java?
(10 answers)
Closed 3 years ago.
I was coding an algorithm issue, below code can't pass case
public void pop() {
if (s1.pollFirst() == minStack.peekFirst())
minStack.pollFirst();
}
however below can,
public void pop() {
int tmp = s1.pollFirst() ;
if (tmp == minStack.peekFirst())
minStack.pollFirst();
}
the only difference is how I use s1,pollFirst() return result. I can't figure out real difference here.
Thanks
Comparing two Integer Objects with values less than -128 or bigger than 127 using == will always result in false. However, if you compare Integer with primitive int, it will give you true if the actual value is the same.
int n1=128;
Integer n2=127;
Integer n3=127;
Integer n4=128;
Integer n5=128;
System.out.println(n1==n2); //false
System.out.println(n2==n3); //true
System.out.println(n4==n5); //false
System.out.println(n1==n5); //true
In the second example you are assigning the value to the primitive int, therefore it is automatically unboxed.
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
This question already has answers here:
What is the purpose of Java's unary plus operator?
(7 answers)
Closed 8 years ago.
I ran across this while reviewing a colleague's code. She'd left it in by accident (it used to be a String concatenation), and I assumed that it wouldn't compile. Turns out I was wrong, so I tried seeing what that operator did:
public static void main(String[] args) {
int i = -1;
System.out.println(String.format("%s", +i));
System.out.println(String.format("%s", +i));
}
As far as I can tell, it does nothing, but I'm curious if there is a reason it's allowed to compile. Is there some hidden functionality to this operator? It's similar to ++i, but you'd think the compiler would barf on +i.
That is the plus unary operator +. It basicaly it does numeric promotion, so "if the operand is of compile-time type byte, short, or char, it is promoted to a value of type int".
Another unary operator is the increment operator ++, which increments a value by 1. The increment operator can be applied before (prefix operator) or after (postfix operator) the operand. The difference is that the prefix operator (++i) evaluates to the incremented value, whereas the postfix operator (i++) evaluates to the original value.
int i = -1;
System.out.println(+i); // prints -1
System.out.println(i++); // prints -1, then i is incremented to 0
System.out.println(++i); // i is incremented to 1, prints 1
This question already has answers here:
The concatenation of chars to form a string gives different results
(5 answers)
why does a char + another char = a weird number
(4 answers)
Closed 8 years ago.
We had an odd thing in our logging happen today. Printed is a list of integers and longs separated by commas, like the following code:
public class Main {
public static void main(String[] args) throws InterruptedException {
long l = 10;
System.out.println(l + ';' + "text");
}
}
The problem was that the ; disappeared from the output.
The problem here is caused by the overload of the + operator. It acts in one way when operating on a String and a long, and another when operating on a char and a long. When one of the operands is a string it will try to cast the other operand to a string if it's not already one and then concatenate the two.
But when the operators are numbers, like int and long, the + operator acts as the normal mathematical plus operator. And since char is a number and nothing else, ';' + l is treated as a numerical operation and thus the output of the code in the question is 69text and not 10;text.