Java Programming5
Java Programming5
Argument/Parameter passing
In general, there are two ways that a computer language can pass an argument to a
subroutine. The first way is call-by-value. This method copies the value of an argument into the
formal parameter of the subroutine. Therefore, changes made to the parameter of the subroutine
have no effect on the argument. The second way an argument can be passed is call-by-reference.
In this method, a reference to an argument (not the value of the argument) is passed to the
parameter. Inside the subroutine, this reference is used to access the actual argument specified in
the call. This means that changes made to the parameter will affect the argument used to call the
subroutine. As you will see, Java uses both approaches, depending upon what is passed.
For example, consider the following
program: // Simple types are passed by value.
class Test {
void meth(int i, int j)
{ i *= 2;
j /= 2;
}
}
class CallByValue {
public static void main(String args[]) {
Test ob = new Test();
int a = 15, b = 20;
System.out.println("a and b before call: "
+ a + " " + b);
ob.meth(a, b);
System.out.println("a and b after call: " +
a + " " + b);
}
}
The output from this program is shown here: a and b before call: 15 20
a and b after call: 15 20
Recursion
Java supports recursion. Recursion is the process of defining something in terms of itself.
As it relates to Java programming, recursion is the attribute that allows a method to call itself. A
method that calls itself is said to be recursive.The classic example of recursion is the
computation of the factorial of a number. The factorial of a number N is the product of all the
whole numbers between 1 and N.
// A simple example of
recursion(factorial). class Factorial {
// this is a recursive function
int fact(int n) {
int result;
if(n==1) return 1;
result = fact(n-1) * n;
return result;
}
}
class Recursion {
public static void main(String args[]) {
Factorial f = new Factorial();
System.out.println("Factorial of 3 is " + f.fact(3));
System.out.println("Factorial of 4 is " + f.fact(4));
System.out.println("Factorial of 5 is " + f.fact(5));
}
}
The output from this program is shown here:
Factorial of 3 is 6
Factorial of 4 is 24
Factorial of 5 is 120
String class
Although the String class will be examined in depth in Part II of this book, a short
exploration of it is warranted now, because we will be using strings in some of the example
programs shown toward the end of Part I. String is probably the most commonly used class in
Java’s class library. The obviousareaveryimportantreasonpartof for programming.
The first thing to understand about strings is that every string you create is actually an object of
type String. Even string constants are actually String objects. For example, in the statement
The second thing to understand about strings is that objects of type String are immutable; once a
String object is created, its contents cannot be altered. While this may seem like a serious
restriction, it is not, for two reasons:
■ If you need to change a string, youthe can modifications.
■ Java definesString ,calledpeerStringBufferclass,whichallowsofstrings to be altered, so
all of the normal string manipulations are still available in Java.
(StringBuffer is described in Part II of this book.)
Strings can be constructed a variety of ways. The easiest is to use a statement like this:
String myString = "this is a test";
Once you have created a String object, you can use it anywhere that a string is allowed. For
example, this statement displays myString:
System.out.println(myString);
Java defines one operator for String objects: +. It is used to concatenate two strings.
For example, this statement
The String class contains several methods that you can use. Here are a few. You can
test two strings for equality by using equals( ). You can obtain the length of a string by
calling the length( ) method. You can obtain the character at a specified index within a
string by calling charAt( ). The general forms of these three methods are shown here:
boolean equals(String object)
int length( )
char charAt(int index)
As you will see in the following section, string arrays play an important part
in many Java programs.