看到这段代码工作,我感到很惊讶.我认为char和int是
Java中的两种不同的数据类型,我不得不将char转换为int来为此提供ascii equivelent.为什么这样做?
String s = "hello"; int x = s.charat(1); System.out.println(x);
解决方法
char可以自动转换为int.见
JLS 5.1.2:
The following 19 specific conversions on primitive types are called the widening primitive conversions:
…
- char to int,long,float,or double
…
A widening conversion of a signed integer value to an integral type T simply sign-extends the two’s-complement representation of the integer value to fill the wider format. A widening conversion of a char to an integral type T zero-extends the representation of the char value to fill the wider format.
(重点补充)
原文地址:https://www.jb51.cc/java/124799.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。