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
Related
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:
How to Replace dot (.) in a string in Java
(4 answers)
Closed 8 years ago.
I am reading in java inputs in format below
2499.873639
32.374242
0.610059
...
Now i want to get rid of the decimal place and have them in this format
2499873639
32374242
610059
...
I have this code which does it for smaller number for not for larger numbers. The Larger numbers become negative (i think this overflowing) and giving it junk values.
BigDecimal b = new BigDecimal(a).multiply(new BigDecimal("1000000.")
If i increase the 0's by another two
BigDecimal b = new BigDecimal(a).multiply(new BigDecimal("100000000.")
It works for larger numbers but not smaller numbers. In short of having a bunch of if's is there anyway to fix this issue?
Use this :
BigDecimal b = new BigDecimal(a.toString().replace('.', ''));
String formattedInput = (String.valueOf(input)).replace(".", "");
You can do this with String replacement functions.
public static BigDecimal noDecimal(BigDecimal b) {
return new BigDecimal(b.toPlainString().replace(".", ""));
}
If you already have a String rather than a BigDecimal, this can be simplified to this:
public static BigDecimal noDecimal(String s) {
return new BigDecimal(s.replace(".", ""));
}
Convert the double to String, if it is not.
Then use String.replace('.', '');
Then Convert back to int or long, if necessary.
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.