שאלה ב-JAVA
שאלה ב-JAVA, העברת פרמטרים בפונקציות. פרמטרים עוברים כ-refrence, ולכן אם אשנה את האוביקט בתוך הפונקציה גם מחוצה לה הוא ישתנה. למה בתוכנית הבאה זה לא כך? public class X { public static void main(String[] args) { X myX = new X(); Y myY = null; myX.f(myY); if (myY == null) System.out.println("myY is null here. why???"); // ^^^ why did i get here? passing by refrence means that if the function changed the object it´s changed here also, right? ^^^^ } // end of main func public void f(Y myY) { myY = new Y(); // myY is no longer null. } } // end of class X
שאלה ב-JAVA, העברת פרמטרים בפונקציות. פרמטרים עוברים כ-refrence, ולכן אם אשנה את האוביקט בתוך הפונקציה גם מחוצה לה הוא ישתנה. למה בתוכנית הבאה זה לא כך? public class X { public static void main(String[] args) { X myX = new X(); Y myY = null; myX.f(myY); if (myY == null) System.out.println("myY is null here. why???"); // ^^^ why did i get here? passing by refrence means that if the function changed the object it´s changed here also, right? ^^^^ } // end of main func public void f(Y myY) { myY = new Y(); // myY is no longer null. } } // end of class X