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.
Related
This question already has answers here:
In Java, is the result of the addition of two chars an int or a char?
(8 answers)
Closed 1 year ago.
I once decided to add two chars together, and it gave me a number. Here's the code:
class Main {
public static void main(String[] args) {
System.out.println('a'+'b');
}
}
Output: 195.
I've searched in a lot of places, but I still couldn't figure out why a char + char = int. Can someone explain this to me?
NOTE: THIS IS NOT A DUPLICATE!! The other question is asking the data type of an added char. This question asks why this happens. Those are DIFFERENT QUESTIONS WITH DIFFERENT ANSWERS!
You're adding their ASCII values:
'a'+'b' = 97+98 = 195
If you want to concatanate, try this :
System.out.println("a"+"b");
5.1. Kinds of Conversion
This code implicitly casts them to String:
System.out.println("" + 'a' + 'b'); //ab
And this code casts to double:
System.out.println(.8 + 'a' + 'b'); //195.8
This question already has answers here:
How to format strings in Java
(10 answers)
Closed 3 years ago.
I am working on this very simple java problem which requires to print both the objects money and isTrue without skipping a line.
I have tried casting both objects to string but doesn't work. I know I could have two print statements but they would print on 2 differents lines. I need both to print as 9999.99false
class Main {
public static void main(String[] args) {
double money = 9999.99;
boolean isTrue = false;
System.out.println(money + isTrue);
}
}
The output expected is 9999.99false
Thanks!
What is happening right now is the compiler is seeing that you are trying to add a double and a boolean together using the + operator, which it does not know how to do. One option is to use the below code:
System.out.println("" + money + isTrue);
The first String literal tells the compiler to add a String and a double, which the compiler can do successfully by implicitly converting the double to a String. The same thing happens with the boolean.
Since String is a class, and double and boolean are primitive types, casting does not work between the two like it would in C# (see here for more information on that).
This produces your expected output when run:
9999.99false
This question already has answers here:
Why does this Java code with "+ +" compile?
(8 answers)
Explanation about a Java statement
(6 answers)
Closed 4 years ago.
Wasn't sure exactly how to word the question, but I noticed something strange while constructing a date. I found that if I construct a date like this
new Date(+ 1)
it compiled just fine, and so did
new Date(+ + + 1)
If I execute the following, the output is 1
public static void main(String[] args) {
int x = 1;
System.out.println(+ + + + x);
}
Can anyone explain what it is that the JVM thinks I am doing?
It's the unary operator (+). You can always add a + to a numeral and that will give you the positive value of the number.
Because you're spacing the tokens out in such a fashion, the lexer is not interpreting anything here as incrementation, so you're adding four unary (+) operations to a value 1.
It's treating it like this:
System.out.println(+ (+ (+ (+ x))));
This is no different than
System.out.println(- (- (- (- x))));
This question already has answers here:
In Java, is the result of the addition of two chars an int or a char?
(8 answers)
Closed 4 years ago.
IntelliJ IDEA Capture
Why i am getting 152, I think it will give me an error.
Please explain it.
public class character {
public static void main(String[] args) {
char myCharValue1 = 'A';
char myCharValue2 = '2';
char myCharValue3 = '%';
System.out.println(myCharValue1 + myCharValue2 + myCharValue3);
}
}
That is because chars refer to a number, which in turn has an ASCII representation.
Looking at an ASCII table you can see that the chars A, 2 and % have following values respectivly: 65, 50 and 37.
Adding those numbers together, you'll end up with 152 which is what you got in your example.
To print out those chars you could use following:
System.out.printf("%s%s%s&n", myCharValue1 + myCharValue2 + myCharValue3);
Which will print A2% (and a newline)
The concatenation + is for String. What you're doing is adding the numeric values of your chars and printing them together.
If you start with "" and then use + as Patrick Parker shows in his comment, it will become concatenation instead of simple addition and you'll get the result you expect.
This question already has answers here:
The concatenation of chars to form a string gives different results
(5 answers)
Closed 9 years ago.
I have a string str, let's say it's value is "hell".
The below statement returns "205hellhe" instead of "hehellhe"
return (str.charAt(0)) + (str.charAt(1))+str+(str.charAt(0)) + (str.charAt(1));
why (str.charAt(0)) + (str.charAt(1)) is returning 205 instead of "he" and why the same statement is returning "he" at the end?
Expressions are evaluated Left-To-Right.
Hence first two chars are evaluated and added, which results in a char (value = 205).
Next this char(=205) is added to a string, which results in String.
Hence the strange output.
Fix:
Use a StringBuilder instead
public static void main(String[] args) {
String str = "hell";
StringBuilder buff = new StringBuilder();
buff.append(str.charAt(0))
.append(str.charAt(1))
.append(str)
.append(str.charAt(0))
.append(str.charAt(1));
System.out.println(buff.toString());// prints 'hehellhe'
}
This is happening because the values of the ASCII codes of the characters are being added instead of being concatenated. In ASCII, h is represented as 104, and e as 101. The concatenation isn't working, probably because both operands in this case are chars/integers.