package myintergertest;
/**
*
* @author Engineering
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//this one does not increment
Integer n = new Integer(0);
System.out.println("n=" + n);
Increment(n);
System.out.println("n=" + n);
Increment(n);
System.out.println("n=" + n);
Increment(n);
System.out.println("n=" + n);
Increment(n);
//this one will increment
MyIntegerObj myInt = new MyIntegerObj(1);
Increment(myInt);
System.out.println("myint = " + myInt.get());
Increment(myInt);
System.out.println("myint = " + myInt.get());
Increment(myInt);
System.out.println("myint = " + myInt.get());
}
public static void Increment(Integer n) {
//NO. this doesn't work because a new reference is being assigned
//and references are passed by value in java
n++;
}
public static void Increment(MyIntegerObj n) {
//this works because we're still operating on the same object
//no new reference was assigned to n here.
n.plusplus(); //I didn't know how to implement a ++ operator...
}
}
उन सभी के लिए परिणाम n = 0 है। इंटीजर एन एक ऑब्जेक्ट है और इसलिए संदर्भ द्वारा पारित किया गया है, तो कॉलर विधि (मुख्य) में वृद्धि क्यों नहीं दिखाई देती है? मुझे आउटपुट होने की उम्मीद है n = 0 n = 1 n = 2 आदि ...किसी अन्य विधि से जावा में एक वर्ग इंटीजर संदर्भ मान कैसे बढ़ाएं
अद्यतन: नोटिस मैंने उपरोक्त कोड उदाहरण अपडेट किया है। अगर मैं सही ढंग से समझ रहा हूं, तो जॉन स्कीट ने इस सवाल का जवाब दिया कि मेरा इंटेंट क्यों बढ़ेगा और क्यों नहीं। ऐसा इसलिए है क्योंकि एन को बढ़ती विधि में एक नया संदर्भ दिया गया है। लेकिन मेरे सदस्य को एक नया फ़ंक्शन असाइन नहीं किया गया है क्योंकि यह सदस्य फ़ंक्शन को कॉल कर रहा है।
क्या ऐसा लगता है जैसे मैं सही ढंग से समझता हूं?
क्या आप कहने का मतलब था '** ऑब्जेक्ट्स ** मूल्य से गुजर चुके हैं - ...'? – FrustratedWithFormsDesigner
नहीं, उसने नहीं किया।Primitives और संदर्भ मूल्य से पारित कर रहे हैं। वस्तुएं बिल्कुल पारित नहीं होती हैं; वे ढेर पर रहते हैं। – danben
मुझे लगता है कि मैं समझता हूं, मेरे अपडेट देखता हूं और मुझे बताता हूं। धन्यवाद!!! – cchampion