读书人

刚看了一个JAVA的print方法对立面的

发布时间: 2013-08-29 10:33:02 作者: rapoo

刚看了一个JAVA的print方法,对立面的c&0xff00不怎么理解。

 public void print(String s) throws IOException {
if (s==null) s="null";
int len = s.length();
for (int i = 0; i < len; i++) {
char c = s.charAt (i);

//
// XXX NOTE: This is clearly incorrect for many strings,
// but is the only consistent approach within the current
// servlet framework. It must suffice until servlet output
// streams properly encode their output.
//
if ((c & 0xff00) != 0) {// high order byte must be zero
String errMsg = lStrings.getString("err.not_iso8859_1");
Object[] errArgs = new Object[1];
errArgs[0] = new Character(c);
errMsg = MessageFormat.format(errMsg, errArgs);
throw new CharConversionException(errMsg);
}
write (c);
}
}


1,char是多少位?
0xff00 = 1111 1111 0000 0000 么?
这样,是不是说这个字符必须是 0000 0000 xxxx xxxx(x=0或者1)
这样,对于一个中文字符,是不是无法进行打印呢。

2,往这个方法里,传递一串中文会发生什么事情呢。



[解决办法]
1.字符=2字节=16位 你后面说的都对
2.throw new CharConversionException(errMsg);

读书人网 >J2EE开发

热点推荐