साथ संकलन इस वर्ग पर विचार करें:त्रिगुट ऑपरेटर, Maven
package be.duo.test;
public class Main {
public static void main(String[] args) {
Main main = new Main();
main.execute();
}
private void execute() {
for(int i = 0; i < 10; i++) {
System.out.println("" + method());
}
}
private int method() {
return (Math.random() > 0.5d) ? 1 : null;
}
}
विधि() है लौटने प्रकार int
है, जो एक आदिम प्रकार है।
त्रिगुट वापसी कथन में इस्तेमाल ऑपरेटर पर विचार करें:
- यह जावा 8 डिफ़ॉल्ट संकलक के साथ संकलित है, लेकिन इस, क्यों रनटाइम पर एक NullPointerException में परिणाम होगा?
- Maven का उपयोग कर इस एक संकलन समय त्रुटि में परिणाम होगा
[ERROR] error: incompatible types: bad type in conditional expression
[ERROR] <null> cannot be converted to int
किसी क्यों यह अलग बर्ताव मुझे लिए व्याख्या कर सकते हैं?
किया संकलन Maven –
साथ यह javac 1.8.0_45 –
मैं जावा 1.8.0_31 के साथ एक NullPointerException जो उम्मीद की जा करने के लिए है पाने के साथ मेरे लिए ठीक संकलित (मुझे लगता है getIntValue() Integer.toString में बुलाया जा रहा है())। –