c*********n 发帖数: 128 | 1 write
public void write(int c)
throws IOException
Write a single character.
Overrides:
write in class Writer
Throws:
IOException - If an I/O error occurs
这里说的是write a single character,但怎么输入参数却是int呢?
比如我要写一个字符 'c'到这个 BufferedWriter bw里面去,怎么写呢?
bw.write('c')就可以了么? |
g*****g 发帖数: 34805 | 2 char can be casted to int, I think it should work.
【在 c*********n 的大作中提到】 : write : public void write(int c) : throws IOException : Write a single character. : Overrides: : write in class Writer : Throws: : IOException - If an I/O error occurs : 这里说的是write a single character,但怎么输入参数却是int呢? : 比如我要写一个字符 'c'到这个 BufferedWriter bw里面去,怎么写呢?
|
c*********n 发帖数: 128 | 3 我觉得逻辑应该是反过来的, + 运算符只对String类型重载
好比method的重载
已经有了
methodName(int parameter)
现在重新写一个
methodName(String strParameter)
Well that's what it's special about - String is the only class that supports
overloading +. |
g*****g 发帖数: 34805 | 4 char->short->int->double, you don't need to cast in
that direction.
【在 c*********n 的大作中提到】 : 我觉得逻辑应该是反过来的, + 运算符只对String类型重载 : 好比method的重载 : 已经有了 : methodName(int parameter) : 现在重新写一个 : methodName(String strParameter) : : Well that's what it's special about - String is the only class that supports : overloading +.
|
c*********n 发帖数: 128 | 5 我已经试过了, 这个的确是可以的
但java不是号称是严格检查数据类型的语言么?
难道这里也可以像c语言一样自动转换类型而不需要在c前面加上(int)?
char can be casted to int, I think it should work.
【在 g*****g 的大作中提到】 : char can be casted to int, I think it should work.
|
c*********n 发帖数: 128 | 6 我觉得逻辑应该是反过来的, + 运算符只对String类型重载
好比method的重载
已经有了
methodName(int parameter)
现在重新写一个
methodName(String strParameter)
Well that's what it's special about - String is the only class that supports
overloading +. |
p***p 发帖数: 559 | 7 Primitive Type is not object
but string is special object, is it right_
【在 c*********n 的大作中提到】 : 我觉得逻辑应该是反过来的, + 运算符只对String类型重载 : 好比method的重载 : 已经有了 : methodName(int parameter) : 现在重新写一个 : methodName(String strParameter) : : Well that's what it's special about - String is the only class that supports : overloading +.
|
r****n 发帖数: 1088 | 8 String is not primitive type. String extends from Object. String is immutable,
which makes String "special".
【在 p***p 的大作中提到】 : Primitive Type is not object : but string is special object, is it right_
|
m******t 发帖数: 2416 | 9
Well that's what it's special about - String is the only class that supports
overloading +.
【在 c*********n 的大作中提到】 : 我觉得逻辑应该是反过来的, + 运算符只对String类型重载 : 好比method的重载 : 已经有了 : methodName(int parameter) : 现在重新写一个 : methodName(String strParameter) : : Well that's what it's special about - String is the only class that supports : overloading +.
|
m******t 发帖数: 2416 | 10
Good point, too.
【在 g*****g 的大作中提到】 : char->short->int->double, you don't need to cast in : that direction.
|
|
|
c*****t 发帖数: 1879 | 11 The reason is actually because all string literals are stored in a
hash table in class file. So duplicate strings are eliminated in the
compiled class file.
There are some horrible programmers at Sun that does things like
variable == "Center"
in the code. Maybe they knew it better, but still it is a bad
approach.
【在 g*****g 的大作中提到】 : char->short->int->double, you don't need to cast in : that direction.
|
c*********n 发帖数: 128 | 12 char->short?
or
byte->short?
char->short->int->double, you don't need to cast in
that direction.
【在 g*****g 的大作中提到】 : char->short->int->double, you don't need to cast in : that direction.
|
m******t 发帖数: 2416 | 13
Good question. I have always wondered why, too.
【在 c*********n 的大作中提到】 : char->short? : or : byte->short? : : char->short->int->double, you don't need to cast in : that direction.
|
m******t 发帖数: 2416 | 14
Right, that's what's special about String. I don't see any material difference
in what we are saying, man. 8-)
【在 c*********n 的大作中提到】 : 我觉得逻辑应该是反过来的, + 运算符只对String类型重载 : 好比method的重载 : 已经有了 : methodName(int parameter) : 现在重新写一个 : methodName(String strParameter) : : Well that's what it's special about - String is the only class that supports : overloading +.
|
c*********n 发帖数: 128 | 15 thanks!
http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter02/castsMixing.html
【在 g*****g 的大作中提到】 : char->short->int->double, you don't need to cast in : that direction.
|
m******t 发帖数: 2416 | 16
immutable,
Hmm, I'm not sure about that - many classes are immutable. I think String is
special because it's the only non-primitive type that supports operator +, and
the compiler does some special optimization supporting that.
【在 r****n 的大作中提到】 : String is not primitive type. String extends from Object. String is immutable, : which makes String "special".
|