Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

Java Programming5

Overloaded constructors allow a class to have multiple constructor methods that differ in their parameters. This allows constructing class objects in different ways. Java supports both call-by-value and call-by-reference parameter passing depending on whether a primitive type or object reference is passed. Recursion is when a method calls itself, either directly or indirectly. A classic recursive example is calculating factorials. The String class represents character strings. String objects are immutable but can be concatenated or compared. Common string methods include length(), charAt(), and equals().

Uploaded by

preetham a
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Java Programming5

Overloaded constructors allow a class to have multiple constructor methods that differ in their parameters. This allows constructing class objects in different ways. Java supports both call-by-value and call-by-reference parameter passing depending on whether a primitive type or object reference is passed. Recursion is when a method calls itself, either directly or indirectly. A classic recursive example is calculating factorials. The String class represents character strings. String objects are immutable but can be concatenated or compared. Common string methods include length(), charAt(), and equals().

Uploaded by

preetham a
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Overloading Constructor

In addition to overloading normal methods, you can also overload constructor


methods. In fact, for most real-world classes that you create, overloaded constructors
will be the norm, not the exception. To Boxunderstandclass why developed in the preceding chapter. Following
is the latest version of Box:
class Box {
double width;
double height;
double depth;
// This is the constructor for Box.
Box(double w, double h, double d)
{ width = w;
height = h;
depth = d;
}
// compute and return volume
double volume() {
return width * height * depth;
}
}

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

System.out.println("This is a String, too");

the string ―ThisStringisconstant. Fortunately,String,Javahandlestoo‖String isconstants


in the same way that other computer language worry about this.

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

String myString = "I" + " like " + "Java.";

results in myString containing ―I like Java.‖

The following program demonstrates the preceding concepts:


// Demonstrating Strings.
class StringDemo {
public static void main(String args[]) {
String strOb1 = "First String";
String strOb2 = "Second String";
String strOb3 = strOb1 + " and " + strOb2;
System.out.println(strOb1);
System.out.println(strOb2);
System.out.println(strOb3);
}
}
The output produced by this program is shown here:
First String
Second String
First String and Second String

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)

Here is a program that demonstrates these methods:


// Demonstrating some String methods.
class StringDemo2 {
public static void main(String args[]) {
String strOb1 = "First String";
String strOb2 = "Second
String"; String strOb3 = strOb1;
System.out.println("Length of strOb1: "
+ strOb1.length());
System.out.println("Char at index 3 in strOb1: "
+ strOb1.charAt(3));
if(strOb1.equals(strOb2))
System.out.println("strOb1 == strOb2");
else
System.out.println("strOb1 != strOb2");
if(strOb1.equals(strOb3))
System.out.println("strOb1 == strOb3");
else
System.out.println("strOb1 != strOb3");
}
}

This program generates the following output:


Length of strOb1: 12
Char at index 3 in strOb1: s
strOb1 != strOb2
strOb1 == strOb3
Of course, you can have arrays of strings, just like you can have arrays of any
other type of object. For example:
// Demonstrate String
arrays. class StringDemo3 {
public static void main(String args[]) {
String str[] = { "one", "two", "three" };
for(int i=0; i<str.length; i++)
System.out.println("str[" + i + "]: "
+ str[i]);
}
}

Here is the output from this


program: str[0]: one
str[1]: two
str[2]: three

As you will see in the following section, string arrays play an important part
in many Java programs.

You might also like