0% found this document useful (0 votes)
12 views13 pages

Java SE (Argument Passing)

Uploaded by

vatasem985
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)
12 views13 pages

Java SE (Argument Passing)

Uploaded by

vatasem985
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
You are on page 1/ 13

INTRODUCTION TO JAVA PROGRAMMING

LANGUAGE

(ELECTIVE-1)
LECTURE-16
Today’s Agenda

• Argument passing.

• Passing variables

• Passing array reference.

• Returning array reference.


Argument Passing

• Java only allows pass by value, there is no pass by reference.


But yes references are passed but same as pass by value.

• So, we can say that in Java we can pass arguments as…

1. Passing variable as Argument.

2. Passing references as argument.


Passing variable
as argument

• In java also on passing variables as argument, a copy gets


generated which is termed as formal argument.

• The method performs all operation on these formal


arguments and hence no change can be made on actual
arguments.

• Let us understand this through an example


Example
class Demo
{
public void increment(int x, int y)
{
x++;
y++;
}
}
class Test
{
public static void main(String [ ] args)
{
Demo D=new Demo();
int x=10, y=20;
S.O.P(“Before Increment x= ”+x+“y= ”+y);
D.increment(x, y);
S.O.P(“After Increment x= ”+x+“y= ”+y);
}
}
Improved Program
Passing Reference

class Demo public void show( )


{ {
private int x; System.out.println(“x= ”+x);
private int y; System.out.println(“y= ”+y);
public Demo(int i, int j) }
{ }
x=i;
y=j;
}
public void increment(Demo D)
{
D.x++;
D.y++;
}
class Test
{
public static void main(String [ ] args)
{
Demo D=new Demo(10,20);
D.show( );
D.increment(D);
D.show( );
}
}
Execise

• WAP to swap two integer data members of a class. Later you


can modify it to accept input from the user.

• Sample Output :-
Solution
Passing Array Reference

class Demo d.doubler(arr);


{ for(int i=0;i<arr.length;i++)
public void doubler(int [ ] brr) S.O.P(arr[i]);
{ }
for(int i=0; i<brr.length; i++) }
brr[i]=brr[i]*2;
}
}
class Test
{
public static void main(String [ ] args)
{
int [] arr={10,20,30,40,50};
Demo d=new Demo( );
Returning Array Reference
class Demo
{
public int[ ] createArray(int n)
{
int [ ] brr=new int [n];
return brr;
}
}
class Test
{
public static void main(String [ ] args)
{
Demo d=new Demo( );
int [ ] arr=d.createArray(5);
System.out.println(“Length of array is ”+arr.length);
}
}
Practice

• WAP to declare an integer array of 10 elements and


ask the user to input values in it. Now again ask
the user to input another number and pass the
array and number to a method called search. This
method should be defined in class called
ArraySearch. The method should return all the
positions where the number is occurring.
Assuming that the number will occur maximum 5
times.

• Design another method in the same class called


count() which should return total number of
elements in the array which are greater than the
number passed, smaller than the number passed
and equal to the number passed.
End Of Lecture 16

You might also like