简体   繁体   English

意外的java charAt输出

[英]Unexpected java charAt output

The code is 该代码是

String veggie = "eggplant";
int length = veggie.length();
char zeroeth = veggie.charAt(0);
char third = veggie.charAt(4);
String caps = veggie.toUpperCase();
System.out.println(veggie + " " + caps);
System.out.println(zeroeth + " " + third + " " + length);
System.out.println(zeroeth + third + length);

The output reads: 输出为:

eggplant EGGPLANT   
e 1 8   
217

This doesn't make sense to me. 这对我来说没有意义。 Referencing a charAt outputs numbers instead of characters. 引用charAt输出数字而不是字符。 I was expecting it to output the characters. 我期望它输出字符。 What did I do wrong? 我做错了什么?

The second line should actually be: 第二行实际上应该是:

e l 8

(note that the second value is a lower-case L, not a 1) which probably doesn't violate your expections. (请注意,第二个值是小写的L,而不是1),这可能不会违反您的期望。 Although your variable is confusingly called third despite it being the fifth character in the string. 尽管您的变量尽管是字符串中的第五个字符,但仍被混淆地称为“ third ”。

That just leaves the third line. 那只剩下第三行。 The type of the expression 表达式的类型

zeroeth + third + length

is int ... you're performing an arithmetic addition. int ...您正在执行算术加法。 There's no implicit conversion to String involved, so instead, there's binary numeric promotion from each operand to int . 没有涉及到String的隐式转换,因此,从每个操作数到int都有二进制数值提升。 It's effectively: 有效的是:

System.out.println((int) zeroeth + (int) third + (int) length);

It's summing the UTF-16 code units involved in 'e', 'l' and 8 (the length). 它是对涉及'e','l'和8(长度)的UTF-16代码单元求和。

If you want string conversions to be involved, then you could use: 如果要涉及字符串转换,则可以使用:

System.out.println(String.valueOf(zeroeth) + third + length);

Only the first addition needs to be a string concatenation... after that, it flows due to associativity. 只有第一个加法项需要为字符串连接...之后,由于关联性,它会流动。 (ie x + y + z is (x + y) + z ; if the type of x + y is String , then the second addition also becomes a string concatention.) (即x + y + z(x + y) + z ;如果x + y的类型为String ,则第二个加法也将成为字符串可乘积。)

The compiler interprets all variables as values rather than a string. 编译器将所有变量解释为值而不是字符串。

Try System.out.println("" + zeroeth + third + length); 尝试System.out.println(“” + zeroeth +第三+长度);

This line is doing integer arithmetic: 该行正在执行整数运算:

System.out.println(zeroeth + third + length);

In other words, it is adding the unicode values of each character (ie e is 101, l is 108, 8 is 8). 换句话说,它将每个字符的unicode值相加(即e为101,l为108,8为8)。 To do String concatenation, you can add an empty String to the front: 要进行字符串连接,可以在前面添加一个空字符串:

System.out.println("" + zeroeth + third + length);

Since it is evaluated left-to-right, it will first do String concatenation (not addition). 由于它是从左到右求值的,因此它将首先进行String连接(而不是加法)。 It will continue to do this for third and length. 它将继续这样做并持续第三次。 Adding "" at the end won't work, because addition will occur first. 在末尾添加“”将不起作用,因为首先会发生添加。

You can use the method of the wrapper class Character to display the string values of char variables: 您可以使用包装器类Character的方法来显示char变量的字符串值:

System.out.println(Character.toString(zeroeth) + Character.toString(third) + length);

This way, you always work with String values and there are no possibilities for the numeric values of the chars to be displayed or added and you don't need to concatenate with empty strings ("") to convert the char variables to String values. 这样,您将始终使用String值,并且无法显示或添加char的数值,并且无需将空字符串(“”)串联即可将char变量转换为String值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM