0% found this document useful (0 votes)
2 views10 pages

Java Method Binding and Parameter Passing

The document discusses various concepts in Java programming, including method binding (static and dynamic), parameter passing methods (call-by-value and call-by-reference), recursion, inner classes, and the string class. It provides examples for each concept to illustrate their functionality and usage. Additionally, it outlines built-in methods of the String class for handling string data.

Uploaded by

tayyab.15.9381
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

Java Method Binding and Parameter Passing

The document discusses various concepts in Java programming, including method binding (static and dynamic), parameter passing methods (call-by-value and call-by-reference), recursion, inner classes, and the string class. It provides examples for each concept to illustrate their functionality and usage. Additionally, it outlines built-in methods of the String class for handling string data.

Uploaded by

tayyab.15.9381
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

METHOD BINDING

Connecting a method call to the method body is known as binding.


There are two types of binding
1. Static Binding (also known as Early Binding).
2. Dynamic Binding (also known as Late Binding).
Static Binding
When type of the object is determined at compiled time(by the compiler), it is known as static binding.
If there is any private, final or static method in a class, there is static binding.
Example
class Dog
{
private void eat(){[Link]("dog is eating...");}
public static void main(String args[])
{
Dog d1=new Dog();
[Link]();
}
}
Dynamic binding
When type of the object is determined at run-time, it is known as dynamic binding.
Example
class Animal
{
void eat()
{
[Link]("animal is eating...");
}
}
class Dog extends Animal
{
void eat()
{
[Link]("dog is eating...");
}
public static void main(String args[])
{
Animal a=new Dog();
[Link]();
}
}
In the above example object type cannot be determined by the compiler, because the instance of Dog is
also an instance of Animal. So compiler doesn't know its type, only its base type.

DE
PARAMETER PASSING METHODS
Parameter passing in Java refers to the mechanism of transferring data between methods or functions.
Java supports two types of parameters passing techniques
1. Call-by-value
2. Call-by-reference.
1. Call-by-Value
In Call-by-value the copy of the value of the actual parameter is passed to the formal parameter of the
method. Any of the modifications made to the formal parameter within the method do not affect the
actual parameter.
Example
public class CallByValueExample
{
public static void main(String[] args)
{
int num = 10;
[Link]("Before calling method:"+num);
modifyValue(num);
[Link]("After calling method:"+num);
}
public static void modifyValue(int value)
{
value=20;
[Link]("Inside method:"+value);
}
}
Output:
Before calling method: 10
Inside method: 20
After calling method: 10

DE
Call-by-Reference
call by reference" is a method of passing arguments to functions or methods where the memory
address (or reference) of the variable is passed rather than the value itself. This means that changes
made to the formal parameter within the function affect the actual parameter in the calling
environment.
In "call by reference," when a reference to a variable is passed, any modifications made to the
parameter inside the function are transmitted back to the caller. This is because the formal parameter
receives a reference (or pointer) to the actual data.
Example
class CallByReference
{
int a,b;
CallByReference(int x,int y)
{
a=x;
b=y;
}
void changeValue(CallByReference obj)
{
obj.a+=10;
obj.b+=20;
}
}
public class CallByReferenceExample
{
public static void main(String[] args)
{
CallByReference object=new CallByReference(10, 20);
[Link]("Value of a: "+object.a +" & b: " +object.b);
[Link](object);
[Link]("Value of a:"+object.a+ " & b: "+object.b);
}
}
Output:
Value of a: 10 & b: 20
Value of a: 20 & b: 40

DE
RECURSION IN JAVA
Recursion in java is a process in which a method calls itself continuously. A method in java that calls
itself is called recursive method. It makes the code compact but complex to understand.
Syntax:
returntype methodname()
{
methodname();
}
Example
public class RecursionExample3
{
static int factorial(int n)
{
if (n == 1)
return 1;
else
return(n * factorial(n-1));
}
public static void main(String[] args)
{
[Link]("Factorial of 5 is: "+factorial(5));
}
}

DE
INNER CLASSES
• Inner class means one class which is a member of another class.
• We use inner classes to logically group classes and interfaces in one place so that it can be more
readable and maintainable.
Syntax of Inner class
class Outer_class
{
//code
class Inner_class
{
//code
}
}
Types of Inner classes
There are four types of inner classes.
1. Member Inner class
2. Local inner classes
3. Anonymous inner classes
4. Static nested classes
1. MEMBER INNER CLASS
A non-static class that is created inside a class but outside a method is called member inner class.
Syntax:
class Outer
{
//code
class Inner
{
//code
}
}

DE
Example
class TestMemberOuter
{
private int data=30;
class Inner
{
void msg()
{
[Link]("data is "+data);
}
}
public static void main(String args[])
{
TestMemberOuter obj=new TestMemberOuter();
[Link] in=[Link] Inner();
[Link]();
}
}
2. ANONYMOUS INNER CLASS
• In Java, a class can contain another class known as nested class. It's possible to create a nested
class without giving any name.
• A nested class that doesn't have any name is known as an anonymous class.
• An anonymous class must be defined inside another class. Hence, it is also known as an
anonymous inner class.
Example
abstract class Person
{
abstract void eat();
}
class TestAnonymousInner
{
public static void main(String args[])

DE
{
Person p=new Person()
{
void eat()
{
[Link]("nice fruits");
}
};
[Link]();
}
}

1. A class is created, but its name is decided by the compiler, which extends the Person class and
provides the implementation of the eat() method.
2. An object of the Anonymous class is created that is referred to by 'p,' a reference variable of Person
type.
3. LOCAL INNER CLASS
• A class i.e. created inside a method is called local inner class in java.
• If you want to invoke the methods of local inner class, you must instantiate this class inside the
method.
Example
public class localInner
{
private int data=30;
void display()
{
class Local
{
void msg()
{
[Link](data);
}
}
Local l=new Local();
[Link]();
}
public static void main(String args[])
{
localInner obj=new localInner();
[Link]();
}
}

DE
4. STATIC NESTED CLASS
• A static class i.e. created inside a class is called static nested class in java. It cannot access non-
static data members and methods. It can be accessed by outer class name.
• It can access static data members of outer class including private.
• Static nested class cannot access non-static (instance) data member or method.
Example
class TestOuter
{
static int data=30;
static class Inner
{
void msg()
{
[Link]("data is "+data);
}
}
public static void main(String args[])
{
[Link] obj=new [Link]();
[Link]();
}
}
In this example, you need to create the instance of static nested class because it has instance method
msg(). But you don't need to create the object of Outer class because nested class is static and static
properties, methods or classes can be accessed without object.

DE
EXPLORING STRING CLASS
• A string is a sequence of characters surrounded by double quotations. In a java programming
language, a string is the object of a built-in class String.
• The string created using the String class can be extended. It allows us to add more characters after
its definition, and also it can be modified.
Example
String siteName = "javaprogramming";
siteName = "javaprogramminglanguage";
String handling methods
In java programming language, the String class contains various methods that can be used to handle
string data values.
The following table depicts all built-in methods of String class in java.
[Link] Method Description
1 charAt(int) Finds the character at given index
2 length() Finds the length of given string
3 compareTo(String) Compares two strings
4 compareToIgnoreCase(String) Compares two strings, ignoring case
5 concat(String) Concatenates the object string with argument string.
6 contains(String) Checks whether a string contains sub-string
7 contentEquals(String) Checks whether two strings are same
8 equals(String) Checks whether two strings are same
9 equalsIgnoreCase(String) Checks whether two strings are same, ignoring case
10 startsWith(String) Checks whether a string starts with the specified string
11 isEmpty() Checks whether a string is empty or not
12 replace(String, String) Replaces the first string with second string
Replaces the first string with second string at all
replaceAll(String, String)
13 occurrences.
Extracts a sub-string from specified start and end index
substring(int, int)
14 values
15 toLowerCase() Converts a string to lower case letters
16 toUpperCase() Converts a string to upper case letters
17 trim() Removes whitespace from both ends
18 toString(int) Converts the value to a String object

DE
Example
public class JavaStringExample
{
public static void main(String[] args)
{
String title = "Java Programming";
String siteName = "String Handling Methods";
[Link]("Length of title: " + [Link]());
[Link]("Char at index 3: " + [Link](3));
[Link]("Index of 'T': " + [Link]('T'));
[Link]("Empty: " + [Link]());
[Link]("Equals: " + [Link](title));
[Link]("Sub-string: " + [Link](9, 14));
[Link]("Upper case: " + [Link]());
}
}

DE

You might also like