0% found this document useful (0 votes)
3 views

Java Lesson 7-3

The document explains the concept of parameter passing in Java, specifically focusing on call-by-value and call-by-reference methods. It outlines how Java handles simple data types and objects differently during method calls, and provides examples to illustrate these concepts. Additionally, it discusses the use of static members and organizing classes into packages.

Uploaded by

pyaepyaephyo.p35
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Java Lesson 7-3

The document explains the concept of parameter passing in Java, specifically focusing on call-by-value and call-by-reference methods. It outlines how Java handles simple data types and objects differently during method calls, and provides examples to illustrate these concepts. Additionally, it discusses the use of static members and organizing classes into packages.

Uploaded by

pyaepyaephyo.p35
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Java Programming : Call-by-Value

Parameter Passing
Dr. Ei Ei Moe
Lecturer

©https://blogs.ashrithgn.com/
Objectives

• To understand how the arguments are passed to the parameters using the pass-by-
value scheme

• To know how to declare reserved word static in class method and variable

• To know how to organize classes into a package

Faculty of Computer Science


Call-by-Value Parameter Passing

• When you pass an item of a simple data type to a method, Java passes a copy of the
data in the item, which is called passing by value.

• Because the method gets only a copy of the data item, the code in the method cannot
affect the original data item at all.

• This way of passing the value of arguments is called a pass-by-value or call-by-


value scheme.
Faculty of Computer Science
Pass-by-Reference

• When you pass an object to a method, Java actually passes a reference to the object,
which is called passing by reference.

• Passing by reference means that the code in the method can reach the original
object.

• In fact, any changes made to the passed object affect the original object.

Faculty of Computer Science


Example : Pass-by-Value

class Employee {
int salary; public class EmployeeMain {
String name; public static void main(String[] args) {

Employee(int sal, String empname) { Employee empobj = new Employee(500, “Aye Aye”);
salary = sal; System.out.println(empobj.salary+ “ ”+
name = empname; }
empobj.name);
empobj.passByValue(empobj.salary);
public void passByValue(int sal) {
System.out.println(“Salary : ”+ sal); System.out.println(empobj.salary);
int increaseSalary = sal + 100; Output
}}
System.out.println(“Increase Salary :”+increaseSalary); 500 Aye Aye
} Salary : 500
Increase Salary :600
} 500
Faculty of Computer Science
Example: Pass-by-Reference

class Employee {
public class EmployeeMain {
int salary;
String name; public static void main(String[] args) {
Employee(int sal, String empname) {
Employee empobj = new Employee(500, “Aye Aye”);
salary = sal;
name = empname; System.out.println(empobj.salary + “” +
}
empobj.name);
public void passByReference(Employee emp) {
empobj.passByReference(empobj);
System.out.println(“Original Name ” + emp.name);
Employee e = emp; System.out.println(empobj.name);
}}
Output
e.name = “Su Su”;
500 Aye Aye
System.out.println(“Update Name:” + e.name); Original Name Aye Aye
}} Update Name:Su Su
Su Su
Faculty of Computer Science
Define Class Methods and Variables

• Static keyword can be used with class, variable, method, and block.

• Static members belong to the class instead of a specific instance.

• If you make a member static, you can access it without object.

static String name = “Ma Ma”;


static void display() {
System.out.println(“My Name is = " + name);
}

Faculty of Computer Science


Method Invocation

invoke min (i, j)


Pass the value of i to num1
Pass the value of j to num2

public static void main(String[] args) { public static int min(int num1, int num2) {
int i = 2;
int j = 5; int result;

int k = min(i, j); if (num1 <num2)


result = num1;
System.out.println( "The minimum between " + i else
+ " and " + j + " is " + k); result = num2;
return result;
} }

Faculty of Computer Science


Method Invocation

Execute the print statement

public static void main(String[] args) { public static int min(int num1, int num2) {
int i = 2; (num1 < num2) is true
int j = 5; int result; since num1 is 2 and num2
is 5
int k = min(i, j); if (num1 < num2)
result = num1; result is now 2
System.out.println( "The minimum between " + else
i + " and " + j + " is " + k); result = num2;

} return result;
}

return result, which is 2

Faculty of Computer Science


Organizing Classes into a Package

• The correct way to reuse programmer-defined classes from many different programs
is to place reusable classes in a package.

• A package is a Java class library.

import myutil.*;
class MyClass {
Student s1;
...
}

Faculty of Computer Science


Summary

• How the arguments are passed to the parameters

• How to organize classes

NEXT Topic
• Exception Handling

Faculty of Computer Science

You might also like