Redian新闻
>
BufferedWriter里的write()
avatar
BufferedWriter里的write()# Java - 爪哇娇娃
c*n
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')就可以了么?
avatar
g*g
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里面去,怎么写呢?

avatar
c*n
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 +.
avatar
g*g
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 +.

avatar
c*n
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.
avatar
c*n
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 +.
avatar
p*p
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 +.

avatar
r*n
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_

avatar
m*t
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 +.

avatar
m*t
10

Good point, too.

【在 g*****g 的大作中提到】
: char->short->int->double, you don't need to cast in
: that direction.

avatar
c*t
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.

avatar
c*n
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.

avatar
m*t
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.

avatar
m*t
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 +.

avatar
m*t
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".

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。