When a method is called in a program, parameters can be passed two ways -
Call by Value
When a variable is passed as a parameter to a method, the method can use the value of the variable or manipulate the value in the method and can return the new value. But any changes in the variable value doesn't reflect after returning from the method. Method doesn't change the value of original variable. Here is the example -
Class CallByVal (CallByVal.java)
public class CallByVal { public static void main(String args[]) { int i = 5; System.out.println("Original Value: " + i); int y = doubleVal(i); System.out.println("After Method Called: " + i); System.out.println("Doubled Value: " + y); } public static int doubleVal(int x) { x = x * 2; return x; } }
A class CallByVal is created and an int variable i is declared and initialized to 5. There is one static* method defined in the class which returns the double of the passed value - doubleVal(). In the method, the parameter value is doubled. It returns the doubled value in different variable. After method, still the original variable i has the same value.
*Why static method? - static methods can call only static methods, main method is static method so called method is also static method.
Call by Reference
When a object is passed as a parameter to a method, then the reference of the object is passed to the method, and any changes to the object in the method, reflects after returning from the method. The method has still the reference to the original object and changes the value in the original object.
So, here is the example of passing object reference as the parameter -
Suppose there are two players and they throw the dice turn by turn and whoever has more, gets scored. They play 15 times. A class Score is created for to keep a track of scores. A constructor sets the initial value of score as 0. There are two methods in the class - setScore() and getScore().
Class Score (Score.java)
getScore() method is regular method which can only be accessed through the object of the Score class. It returns the score value for the particular player.
public class Score { private int val; Score(int i) { val = i; } static void setScore(Score sc) { sc.val++; } int getScore() { return val; } }setScore() is a static method so it can be accessed by class itself. It takes one parameter - Score class object. The method increases the score value by 1.
getScore() method is regular method which can only be accessed through the object of the Score class. It returns the score value for the particular player.
Class ScoreDemo (ScoreDemo.java)
Initial Score:
Player1 0
Player2 0
Final Score:
Player1 7
Player2 8
Notice that how the method setScore() is declared in the class Score, and how it is called in class ScoreDemo using the class name itself: Score. This is a good example of passing object reference to a method.
import java.lang.Math; public class ScoreDemo { public static void main(String args[]) { int max = 6; int min = 1; int range = max - min + 1; Score s1 = new Score(0); Score s2 = new Score(0); System.out.println("Initial Score: "); System.out.println("Player1 " + s1.getScore()); System.out.println("Player2 " + s2.getScore()); for(int i = 0; i < 15; i++) { int dice1 = (int)(Math.random() * range) + min; int dice2 = (int)(Math.random() * range) + min; if(dice1 > dice2) Score.setScore(s1); else Score.setScore(s2); } System.out.println(); System.out.println("Final Score: "); System.out.println("Player1 " + s1.getScore()); System.out.println("Player2 " + s2.getScore()); } }A class ScoreDemo initializes two variables max and min as 6 and 1, like a dice can have. A range is also set as total possible values. Two objects of class Score created. There is a for loop for 15 iterations - players throw dice turn by turn (number is generated randomly between 1 to 6 like dice). If player1 has more than player2, score1 value is increased. else score2 value is increased. Here is the sample output -
Initial Score:
Player1 0
Player2 0
Final Score:
Player1 7
Player2 8
Notice that how the method setScore() is declared in the class Score, and how it is called in class ScoreDemo using the class name itself: Score. This is a good example of passing object reference to a method.
No comments:
Post a Comment