|
阅读:1051回复:16
The + operator in System.out.print()[讨论]
Look at following codes:
class SC2{ public static void main(String [] args){ SC2 s = new SC2(); s.start(); } void start(){ int a = 3; int b = 4; System.out.print(" " + 7 + 2 + " "); System.out.print(a + b); System.out.print(" " + a + b + " "); System.out.print(foo() + a + b + " "); System.out.println(a + b + foo()); } String foo(){ return "foo"; } } $ java SC2 72 7 34 foo34 7foo why not 72 7 34 foo34 34foo ? look at the line with System.out.println(a + b +foo()) the method foo() returns a String. Why it add 3 and 4? And anothoer class: class PassA{ public static void main (String [] args){ PassA p = new PassA(); p.start(); } void start(){ long [] a1 = {3,4,5}; long [] a2 = fix(a1); System.out.print(a1[0] + a1[1] + a1[2] + " "); System.out.println(a2[0] + a2[1] + a2[2]); } long [] fix(long [] a3){ a3[1] = 7; return a3; } } $ java PassA 15 15 Its also happens in these codes. there's a blank in the line System.out.print(a1[0] + a1[1] + a1[2] + " ") but it also adds the operands. If not it will print 3 7 5 3 7 5 I can't understand it. The rule: If either operand being evaluated is a Sting, the + operator will concatenate the two operands; if both operands are numeric, the + operator will add the two operands. -------------------- [a=http://blog.csdn.net/GOJYO/]向北的路灯灭了.我不知道我是谁.NorTH.Path[/a] ~ ﭬﮔﮘﭾﮁﮉﮝ 幸福是一雙溫暖的紅唇 ♥ 等待迷路的蝴蝶 三叶草是不是四片叶子 管宁、华歆共园中锄菜。见地有片金,管挥锄与瓦砾不异,华捉而掷去之。又尝同席读书,有乘轩冕过门者,宁读书如故,歆废书出观。宁割席分坐,曰:“子非吾友也。” [ 2005-03-17 13:19:11 0000 修改 ] |
|
|
|
1C#
发布于:2005-03-17 13:33
Re:The + operator in System.out.print()[讨论]
前后关系的原因。。。-_-#
--------------------
[a=http://blog.csdn.net/GOJYO/]向北的路灯灭了.我不知道我是谁.NorTH.Path[/a]
~ ﭬﮔﮘﭾﮁﮉﮝ 幸福是一雙溫暖的紅唇 ♥ 等待迷路的蝴蝶 三叶草是不是四片叶子 管宁、华歆共园中锄菜。见地有片金,管挥锄与瓦砾不异,华捉而掷去之。又尝同席读书,有乘轩冕过门者,宁读书如故,歆废书出观。宁割席分坐,曰:“子非吾友也。” |
|
|
|
2C#
发布于:2005-03-17 13:35
Re:The + operator in System.out.print()[讨论]
前后关系的原因?
System.out.println(a + b + foo()); 的字符串在后面。 而 + 只管本身前后两个操作数 所以是相加而不是把字符串连起来。 下面的代码同理。 -------------------- [a=http://blog.csdn.net/GOJYO/]向北的路灯灭了.我不知道我是谁.NorTH.Path[/a] ~ ﭬﮔﮘﭾﮁﮉﮝ 幸福是一雙溫暖的紅唇 ♥ 等待迷路的蝴蝶 三叶草是不是四片叶子 管宁、华歆共园中锄菜。见地有片金,管挥锄与瓦砾不异,华捉而掷去之。又尝同席读书,有乘轩冕过门者,宁读书如故,歆废书出观。宁割席分坐,曰:“子非吾友也。” |
|
|
|
3C#
发布于:2005-03-17 14:27
Re:The + operator in System.out.print()[讨论]
恩,如果在print的参数中包含+,若+的两端包含有String对象就会转换成StringBuffer的append 操作.
--------------------
痛饮苦丁茶…… |
|
|
|
4C#
发布于:2005-03-17 19:26
Re:The + operator in System.out.print()[讨论]
0000 什么时候玩java了,牛的时侯可一定要上来多喷喷阿, |
|
|
|
5C#
发布于:2005-03-17 21:10
Re:The + operator in System.out.print()[讨论]
HoHo~才看到第三章呢~过两月再说吧:)
--------------------
[a=http://blog.csdn.net/GOJYO/]向北的路灯灭了.我不知道我是谁.NorTH.Path[/a]
~ ﭬﮔﮘﭾﮁﮉﮝ 幸福是一雙溫暖的紅唇 ♥ 等待迷路的蝴蝶 三叶草是不是四片叶子 管宁、华歆共园中锄菜。见地有片金,管挥锄与瓦砾不异,华捉而掷去之。又尝同席读书,有乘轩冕过门者,宁读书如故,歆废书出观。宁割席分坐,曰:“子非吾友也。” |
|
|
|
6C#
发布于:2005-04-22 23:34
Re:The + operator in System.out.print()[讨论]
今天晚上刚看到一段关于这个题目的简单介绍,敲下键盘加深记忆。
当+的两个操作数中有一个是字符串类型的时候,编译器会自动把另外一个操作数转换成字符串类型。这另一个字符串有2种情况(也就是java的两种数据类型),一种是简单类型,一种是引用类型。 如果是简单类型,会调用String 的valueOf()方法,该方法可以把各种简单类型转换成String类型。 如果是引用类型,又分为两种情况,如果是String 的话就先暂不说了。如果是其他的引用类型的话,会调用该引用类型的toString()方法返回该引用的描述。每个引用类型都会有toString()方法的,因为这个方法是从Object继承下来的。 -------------------- 痛饮苦丁茶…… |
|
|
|
7C#
发布于:2005-04-22 23:38
Re:The + operator in System.out.print()[讨论]
下面来做测试:
测试一: String str = "abs"; System.out.println(str.toString()); System.out.println(str); 打印出 abs abs 测试二: System.out.println(12); System.out.println(String.valueOf(12)); 其实上面两个例子不能说明什么问题。 -------------------- 痛饮苦丁茶…… |
|
|
|
8C#
发布于:2005-04-22 23:46
Re:The + operator in System.out.print()[讨论]
测试三:
class StrTest { StrTest() {} public String toString() { return ("toString() extends from Object"); } public static void main(String[] args) { StrTest strTest = new StrTest(); System.out.println(strTest); } } 打印出:toString() extends from Object -------------------- 痛饮苦丁茶…… |
|
|
|
9C#
发布于:2005-04-22 23:48
Re:The + operator in System.out.print()[讨论]
测试四:
class StrTest { StrTest() {} /* public String toString() { return ("toString() extends from Object"); } */ public static void main(String[] args) { StrTest strTest = new StrTest(); System.out.println(strTest); } } 打印出StrTest@XXXX 后面是个数字代码,还没有学习过. -------------------- 痛饮苦丁茶…… |
|
|
|
10C#
发布于:2005-04-22 23:57
Re:The + operator in System.out.print()[讨论]
比较测试三和测试四两段代码我门就不难看出,当我们打印一个对象是,其实是调用了该对象的toString()方法.如果该对象有继承的toString()方法,就用这个继承的方法,如果没有就用父类的toString()方法.
ps:上面的是从一个讲java语言的书中看到的,而看jdk文档的时候发现println(Object)调用print(Object),而print(Object)的string字符串是由valueOf(Object)方法返回的,再来一段测试代码. -------------------- 痛饮苦丁茶…… |
|
|
|
11C#
发布于:2005-04-23 00:02
Re:The + operator in System.out.print()[讨论]
测试五:
class StrTest { StrTest() {} public String toString() { return ("toString() extends from Object"); } public static void main(String[] args) { StrTest strTest = new StrTest(); System.out.println(String.valueOf(strTest)); } } 打印出:toString() extends from Object -------------------- 痛饮苦丁茶…… |
|
|
|
12C#
发布于:2005-04-23 00:10
Re:The + operator in System.out.print()[讨论]
结论就是当我们要打印一个对象(应该是任何一种值,包括引用值和简单类型值)的时候,先要调用String的valueOf()方法把该对象转换成字符串类型.我们去查看该方法的介绍,
valueOf public static String valueOf(Object obj) Returns the string representation of the Object argument. Parameters: obj - an Object. Returns: if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned. 终于恍然大悟,当valueOf()的对象是个空值的时候会返回一个null,反之则会调用该对象的toString()方法.这个测试三测试四有介绍就不多说了. 要测试这个只要把测试五中的 StrTest strTest = new StrTest(); 这行换成 StrTest strTest = null;就可以了 注意一定要一定要 = null,不然会发生编译期错误,提示可能该对象没有初始话,这个不在该主题范围就不说了. ------------ 有问题请告诉我哈,好改正 -------------------- 痛饮苦丁茶…… |
|
|
|
13C#
发布于:2005-04-23 14:40
Re:The + operator in System.out.print()[讨论]
哈哈,越来越像精华了
--------------------
[a=http://northway.blogchina.com]寻找失落的路灯 @ North.Light[/a]
~ ??????? 幸福是一雙溫暖的紅唇 ? 幸福的距离遥不可及 |
|
|
|
14C#
发布于:2005-05-06 11:33
Re:The + operator in System.out.print()[讨论]
之前的写错了,作了下简单测试:
class SysOutTest{
public static void main(String[] args){
char [] arr1 = new char[10];
System.out.println(" "+arr1);
}
}
打印结果: [C@14f8dab 估计是 arr1 这个“引用(?)"的地址。没有找到相关文档。 之前的错误原因是昨晚看同学写的时候他根本就没写到“执行体”内-_-# 等下做第二个测试。 -------------------- [a=http://northlight.digital-pulse.net/]寻找失落的路灯 @ North.Light[/a] ~ 幸福是一雙溫暖的紅唇 ~ . 亲吻阳光的穴居者 [ 2005-05-06 14:09:52 0000 修改 ] |
|
|
|
15C#
发布于:2005-05-06 15:00
Re:The + operator in System.out.print()[讨论]
Found these in javadocs
but what the word hashcode means? hash what? I can't understand these words: the hash code of the object in other words, I don't know how to hash an object -------------------- [a=http://northlight.digital-pulse.net/]寻找失落的路灯 @ North.Light[/a] ~ 幸福是一雙溫暖的紅唇 ~ [ 2005-05-06 15:02:32 0000 修改 ] |
|
|
|
16C#
发布于:2005-05-06 15:08
Re:The + operator in System.out.print()[讨论]
Got it!
-------------------- [a=http://northlight.digital-pulse.net/]寻找失落的路灯 @ North.Light[/a] ~ 幸福是一雙溫暖的紅唇 ~ |
|
|