Primitive Variables as Parameters

advertisement
Primitive Variables as Parameters
The value of primitive variables can not be changed when used as actual parameters to a
method.
public class Mystery {
public static void main (String[] args) {
int x= 2;
double y = 5.4;
System.out.println("In Main, before the call to mystery, x ="
+ x + " and y = " + y);
mystery( x, y);
System.out.println("\nIn Main, after the call to mystery, x ="
+ x + " and y = " + y);
System.out.println("all done");
}// end main
public static void mystery (int a, double b) {
System.out.println("\nAt the beginning of Mystery, a ="
+ a + " and b = " + b);
a = 1982;
b = 789.54;
System.out.println("\nAt the end of Mystery, a ="
+ a + " and b = " + b);
} // at the end of mystery, not return is necessary
}
In Main, before the call to mystery, x =2 and y = 5.4
At the beginning of Mystery, a =2 and b = 5.4
At the end of Mystery, a =1982 and b = 789.54
In Main, after the call to mystery, x =2 and y = 5.4
all done
Reference Variables as Parameters
The values stored inside reference variable can be modified, when they are sent as
parameters to methods.
public class MysteryReference {
public static void main (String [] args) {
int x= 800;
Account myAccount= new Account("Camille Hayhurst", "304-500-4356", 500.0);
System.out.println("Before the call to mystery2, x = " + x +
" and the information in myAccount was ");
myAccount.printAccountInformation();
mystery2 ( x, myAccount);
System.out.println("After the call to mystery2, x = " + x +
" and the information in myAccount was ");
myAccount.printAccountInformation();
}
public static void mystery2 ( int a, Account anAccount) {
System.out.println("At the beginning of Mystery2, a= " + a +
" and the information in anAccount was ");
anAccount.printAccountInformation();
a = 4000;
anAccount.credit(4000);
System.out.println("At the end of Mystery2, a= " + a +
" and the information in anAccount was ");
anAccount.printAccountInformation();
}// end of mystery2, no return is necessary
}
Before the call to mystery2, x = 800 and the information in myAccount was
The account number is: 1
The Account holder is: Camille Hayhurst
The Account holders phone number is: 304-500-4356
The account balance is: 500.0
The account is overdrawn: false
At the beginning of Mystery2, a= 800 and the information in anAccount was
The account number is: 1
The Account holder is: Camille Hayhurst
The Account holders phone number is: 304-500-4356
The account balance is: 500.0
The account is overdrawn: false
At the end of Mystery2, a= 4000 and the information in anAccount was
The account number is: 1
The Account holder is: Camille Hayhurst
The Account holders phone number is: 304-500-4356
The account balance is: 4500.0
The account is overdrawn: false
After the call to mystery2, x = 800 and the information in myAccount was
The account number is: 1
The Account holder is: Camille Hayhurst
The Account holders phone number is: 304-500-4356
The account balance is: 4500.0
The account is overdrawn: false
Some problems with Strings:
When we use Strings as parameters we need to remember that strings can not be altered.
Therefore their values can not be changed inside a method.
public class MysteryString {
public static void main (String [] args) {
String name = new String("John Doe" );
System.out.println("The value of name before Mystery3 is : " + name);
mystery3 (name);
System.out.println("The value of name after Mystery3 is : " + name);
}
public static void mystery3( String someString) {
System.out.println("The value of someString at the beginning of "
+ " mystery3 is : " + someString);
someString = new String(" John Smith " );
System.out.println("The value of someString at the end of mystery3 is "
+ someString);
}// end of mystery3
}
The value of name before Mystery3 is : John Doe
The value of someString at the beginning of mystery3 is : John Doe
The value of someString at the end of mystery3 is John Smith
The value of name after Mystery3 is : John Doe
Download