作者LPH66 ((short)(-15074))
看板java
标题Re: [问题] 有关三元运算子:?
时间Mon Oct 20 20:18:15 2008
※ 引述《chchwy (mat)》之铭言:
: JAVA的三元运算子?:似乎限制颇多
: 比如说 下面这句
: (x==9)?i++:j++;
: 单独存在的话compile竟然不会过
: 一定要改成下面的样子才会过
: int y=(x==9) i++:j++;
: 就是左边一定要有一个assingment叙述
: 这我实在百思不得其解
: 也不能插入一个以上的statement,像这样
: int y= (x==9) ? (a=1, b=1) : (j++);
: 这些在C++里都是合法的叙述
: 为什麽拿到JAVA来就都行不通了呢?
: 有没有什麽书或网站有介绍JAVA在?:运算子上的限制呢?
: 感谢各位
从Java Language Specification里来挖资料吧:
这个是Java完整的BNF文法:
http://java.sun.com/docs/books/jls/third_edition/html/syntax.html
首先是 ?: 本身, 它是以Expression的一部份出现:
Expression:
Expression1 [AssignmentOperator Expression1]]
Expression1:
Expression2 [Expression1Rest]
Expression1Rest:
? Expression : Expression1
(以下略)
所以可以知道整个 ?: 是被当成一个Expression
然後是可以单独写在一行的Expression:
Statement:
(中略)
StatementExpression ;
StatementExpression:
Expression
看来这份文法并没有禁止这种东西
来看看详细的说明吧:
http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.8
它里面只给
Assignment
PreIncrementExpression
PreDecrementExpression
PostIncrementExpression
PostDecrementExpression
MethodInvocation
ClassInstanceCreationExpression
这七种Expression可以当成Statement来用
Assignment 是指 = += -= 等等的
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.26
PreIncrementExpression PreDecrementExpression
PostIncrementExpression PostDecrementExpression
这四个是++和--
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.15
MethodInvocation 是method的呼叫
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.12
ClassInstanceCreationExpression 是new的使用
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.9
这七种都不是?:的 ConditionalExpression
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.25
因此?:自然不能当成statement来用了
回头看14.8有这麽一段字:
Unlike C and C++, the Java programming language allows only certain forms of
expressions to be used as expression statements. Note that the Java
programming language does not allow a "cast to void" - void is not a type -
so the traditional C trick of writing an expression statement such as:
(void) ... ; // incorrect!
does not work. On the other hand, the language allows all the most useful
kinds of expressions in expressions statements, and it does not require a
method invocation used as an expression statement to invoke a void method,
so such a trick is almost never needed.
If a trick is needed, either an
assignment statement (§15.26) or a local variable declaration statement
(§14.4) can be used instead.
这应该回答了你的第一个问题了
第二个问题就要回到 , 这东西上面了
C语言的 , 可以拿来串expression
但java没有这种用法
(例如你直接写一行 i=1,j=2; 同样也不会过)
於是自然不能这样插入了
---
换个方式想, java大概当初设计时就是要让人写出比较容易看得懂的code
所以这种奇怪的写法自然一开始就不让你写
与其写 (x==9)?i++:j++; 还不如写 if(x==9){i++;}else{j++;}
至少我认为後者的可读性比前者好 (虽然我在写C时也常常写前者(遮脸))
--
说起来这下换我搞不懂了...
上面那七种Expression 前六种很好理解
但第七种这...有人会直接写 new xxx(); 然後不指定给变数也不接别的东西吗 @_@
(似乎一new出来结束後就会变成gc候选...
这样它做的事好像只能在constructor里面的样子
这到底能有什麽用途 @_@
和static method的功能重覆了不说 甚至感觉起来比static method还差...)
--
[LPH] Oops, your OOP's a problem? 说:
你现在还是看不到狗?
************* 说:
看得到 只是 他们不会跑 就一直呆呆在那边 一直在起点
[LPH] Oops, your OOP's a problem? 说:
你要按"ㄅㄧㄤˋ"它们才会跑啊@@"
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.112.250.80
1F:推 godfat:有时候我会这样写:(new Main()).run(); 就用过即丢这样 10/20 20:39
2F:→ LPH66:这个是後面有接 它算是MethodInvocation那种情形 10/20 21:17
3F:→ LPH66:可是那第七种情形就是只有new而已 前面没变数接後面没呼叫 10/20 21:19
4F:→ LPH66:我想不到java为什麽要允许这种Expression可以写成statement 10/20 21:21
5F:推 godfat:喔,我懂你的意思了,那这样写就真的很奇怪了... 10/20 22:25
6F:推 godfat:也许... new Widget(parent); 这样?反过来比较好就是了... 10/20 22:27
7F:推 H45:new Thread().start(); 这也是第七种,懒人写法 XD 10/21 07:41
8F:→ H45:喔,我懂意思了,这种有 new JFrame(); 一个 GUI 程式。 10/21 07:43
9F:推 chchwy:原来如此呀 那是语言先天设计上的限制罗 感谢您~~ 10/27 11:41