2015-03-02 6 views
7

संकलन त्रुटि: एक काम के बाएं हाथ की ओर एक चरक्यों लूप बूलियन मान को स्वीकार नहीं कर रहा है?

class A { 
    public static void main(String[] args) { 

     for(true;true;true) {//compilation error 

     } 
    } 
} 

होना चाहिए लेकिन जब मैं इस तरह की कोशिश की है, वहाँ कोई संकलन त्रुटि

class A { 
    public static void main(String[] args) { 

     for (getBoolean(); true; getBoolean()) { 

     } 
    } 

    public static boolean getBoolean() { 
    return true; 
    } 
} 

getBoolean (है) एक बूलियन लौटा रहा है मूल्य, तो पहले मामले के लिए क्यों लूप सीधे बूलियन मान स्वीकार नहीं कर रहा है?

+6

आप क्या करने की कोशिश कर रहे हैं? यह समझ में नहीं आता है। – Brandon

+1

आपका प्रश्न झूठी आधार पर बनाया गया प्रतीत होता है। मुझे त्रुटि प्राप्त नहीं होती है 'पहले नमूना संकलित करते समय' असाइनमेंट का बायां हाथ एक चर होना चाहिए '; मुझे त्रुटि मिलती है कि 'सत्य' एक कथन नहीं है, जो पूरी तरह सटीक है। –

+0

@blm क्या इससे कोई फर्क पड़ता है? यह एक वैध सवाल है। – Voldemort

उत्तर

14

JLS से:

BasicForStatement: 
    for (ForInitopt ; Expressionopt ; ForUpdateopt) Statement 

ForStatementNoShortIf: 
    for (ForInitopt ; Expressionopt ; ForUpdateopt) StatementNoShortIf 

ForInit: 
    StatementExpressionList 
    LocalVariableDeclaration 

ForUpdate: 
    StatementExpressionList 

StatementExpressionList: 
    StatementExpression 
    StatementExpressionList , StatementExpression 

Then:

StatementExpression: 
    Assignment 
    PreIncrementExpression 
    PreDecrementExpression 
    PostIncrementExpression 
    PostDecrementExpression 
    MethodInvocation 
    ClassInstanceCreationExpression 

आप Method Invocation देख सकते हैं की अनुमति दी है और literal value नहीं है।

+0

ग्रेट उत्तर :-) – squiroid

4

डॉक

for (initialization; termination; 
    increment) { 
    statement(s) 
} 

और intialization और वेतन वृद्धि अभिव्यक्ति (काम) आसान नहीं बूलियन लेकिन जावा समारोह कॉल में होना चाहिए के अनुसार अभिव्यक्ति के रूप में माना जाता है इसलिए इसे सही ढंग से मूल्यांकन करेंगे।

संबंधित मुद्दे