Re: [問題] 有關三元運算子:?

看板java作者 ((short)(-15074))時間15年前 (2008/10/20 20:18), 編輯推噓5(504)
留言9則, 4人參與, 最新討論串2/2 (看更多)
※ 引述《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

10/20 20:39, , 1F
有時候我會這樣寫:(new Main()).run(); 就用過即丟這樣
10/20 20:39, 1F

10/20 21:17, , 2F
這個是後面有接 它算是MethodInvocation那種情形
10/20 21:17, 2F

10/20 21:19, , 3F
可是那第七種情形就是只有new而已 前面沒變數接後面沒呼叫
10/20 21:19, 3F

10/20 21:21, , 4F
我想不到java為什麼要允許這種Expression可以寫成statement
10/20 21:21, 4F

10/20 22:25, , 5F
喔,我懂你的意思了,那這樣寫就真的很奇怪了...
10/20 22:25, 5F

10/20 22:27, , 6F
也許... new Widget(parent); 這樣?反過來比較好就是了...
10/20 22:27, 6F

10/21 07:41, , 7F
new Thread().start(); 這也是第七種,懶人寫法 XD
10/21 07:41, 7F

10/21 07:43, , 8F
喔,我懂意思了,這種有 new JFrame(); 一個 GUI 程式。
10/21 07:43, 8F

10/27 11:41, , 9F
原來如此呀 那是語言先天設計上的限制囉 感謝您~~
10/27 11:41, 9F
文章代碼(AID): #18_7S9MX (java)
文章代碼(AID): #18_7S9MX (java)