English 中文(简体)
Java的char类行为有些奇怪?
原标题:The char type in Java behaves somewhat in a strange way?
  • 时间:2011-11-04 02:28:08
  •  标签:
  • java
  • char

Let s consider the following simple expressions in Java.

char c= A ;
int i=c+1;

System.out.println("i = "+i);

这一点在 Java和返回66中完全有效,即c+1的特性(Unicode)的相应价值。


String temp="";
temp+=c;
System.out.println("temp = "+temp);

这一点在Java和Sting类型变量temp中过于有效,自动接受一类的果园,并生产temp=A


以下所有发言在 Java也令人惊讶地有效!

Integer intType=new Integer(c);
System.out.println("temp = "+intType);

Double  doubleType=new Double(c);
System.out.println("temp = "+doubleType);

Float floatType=new Float(c);
System.out.println("temp = "+floatType);

BigDecimal decimalType=new BigDecimal(c);
System.out.println("temp = "+decimalType);

虽然C是一种char,但可以不错地提供,所有上述说法都被视为有效陈述。 它们分别产生以下产出。

temp = 65
temp = 65.0
temp = 65.0
temp = 65

在这种情况下,char/strong>的内部行为是什么。 Java的类型?

最佳回答

慈善是一种原始数字综合体,因此必须遵守这些信标的所有规则,包括转换和升级。 您希望就此读到,而家庭、青年和体育部是这方面的最佳来源之一:,《转化与推广。 尤其是,读到“5.1.2 扩大主机转换”的简短轨道。

问题回答

果园类似于16轨。 你们可以把其他东西推到强硬。 它是特征的捷径。

如果你希望避免这种混淆,你可以使用特性标语。

<代码>char和byte可互换(指统法协会编码表值),因此,char变量可作为数字处理,就像byte一样。

Edit: Further to Henery s comment below, you are right of course, Java does use unicode to represent character content (original text updated to reflect this) - but as we know, the first 127 ASCII and Unicode chars more or less match, and so any manipulation performed within the character range 0..127 would be the same for both ASCII and Unicode, meaning that converting between the two are trivial.

The old-timers amongst us were probably taught and remember most of the ASCII table, rather than the Unicode table, and indeed I still keep a ASCII crib sheet in my desk for quick referal. Oh, and +1 for Henery s comment, for making me actually clarify what I wrote.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...