问题
I just recently started learning Basics in Java, and was testing around initializing a string variable by concatenating primitive variables.
public class Main{
public static void main(String[] args){
byte lbyte = 3;
short lshort = 1;
int lint = 1;
long llong = 0;
float lfloat = 2.0f;
double ldouble = lfloat;
char lchar = 'H';
boolean lbool = true;
String lstring = " w0r1d ";
String lOutput = lchar+lbyte+lshort+lint+llong+lstring+ldouble+' '+lbool;
System.out.println(lOutput);
}
}
In this code, my objective was to create a string that would output:
H3110 w0r1d 2.0 true
However, when i run it the output is: 77 w0r1d 2.0 true
The unexpected result is the 77, while the rest is fine. Even if i would assume the number variables would get added, it would only be a total of 5. The variable lchar is also apparently "absorbed" into the numbers.
Where did the 77 come from and what happened to the H in lchar?
Edit: the goal is to use as much primitive variables as possible in the concatenation.
Edit2: Thanks for all the helpful answers.
回答1:
The ASCII / Unicode value for 'H'
is 72. The additions are processed left to right, so lchar + lbyte
is 'H' + (byte) 3
which is equal to 72 + 3
.
You'll only get a string result from +
if one of the operands is a string. That doesn't happen until you finally concatenate lstring
, which explains why all of the numerical (and the char
) variables are added together to get 77. Everything to the right of lstring
is concatenated one by one, each converted to a string, since the left hand operands of all those +
s are all strings at that point.
A quick fix is to start with ""
to force everything to be done with strings.
String lOutput = ""+lchar+lbyte+lshort+lint+llong+lstring+ldouble+' '+lbool;
回答2:
The 77 came from when you were adding the chars. When adding chars it adds their values since they act like integers. Also the other variables which are numbers might have added together. 'H' = 72 in java and 72 + 3 = 75 and 75 + 1 = 76 and finally 76 + 1 = 77.
To fix this you can put "" at the beginning of where you are building your string.
Fix:
String lOutput = ""+lchar+lbyte+lshort+lint+llong+lstring+ldouble+' '+lbool;
回答3:
Java evaluates expressions like that left to right so what's happening is that you're telling Java to add a char and a byte together and Java will then take the integer value of the character (72) and add the integer value of the byte (3) to it.
So what you're really telling Java is to do this:
String lOutput = (72+3+1+1+0)+" w0r1d"+2.0+' '+true;
When Java comes to +" w0r1d"
it will convert the first part into a String and concatenate them, but before that you're adding types where +
is defined as addition and therefore Java will sum them up to 77.
To get the desired behaviour you need to start out with a String, so doing this will work as you want it to:
String lOutput = String.valueOf(lchar)+lbyte+lshort+lint+llong+lstring+ldouble+' '+lbool;
回答4:
This is for automatic type conversion.Look the in the ASCI chart the integer representation of capital 'H' is 72. At first the 'H' is upgraded to int
from char
and then it is concatenated with the string
right to it
来源:https://stackoverflow.com/questions/30339537/string-contatenation-with-primitives-in-java