java 板


LINE

※ 引述《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







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Soft_Job 或 站内搜寻

TOP