Beyond the EU: DORA and NIS 2 Directive's Global Impact
Call by value and call by reference and varaiable length arguments
1. Call by value and call by reference
Call by value: this approach copies the value of arguments in to format
parameters of subroutine. Therefore changesmade to the parameter of the
subroutine have no effecton the argument in call.
Example:
Class Test
{
Void noChange(int i,int
j)
{
i=i+j;
j=-j;
}
}
class Callvalue
{
Public static void main(String arr[])
{
Test ob= new Test();
Int a=15,b=20;
System.out.println(“a and b before
claa:”+a+” “+b);
Ob.noChange(a,b)
System.out.println(a and b after
call:”+a+” “+b);
}
}
Output:
A and b before call 15 20
A and b after call 15 20
Call by Reference:
When we pass object(ob) to a method situation is different. When we create a
variable of class type we create reference.
When we pass an object as an argument in reality we are not passing the
objectitself but only the reference that refer to that object. so object passedto
method is effectively callby reference, changesto objectinside method do
effect.
2. Class Test
{
Int a,b;
Test(int i,int j)
{
A=I;
B=j;
}
Void change(Test ob)
{
Ob.a=ob.a+ob.b;
Ob.b=-ob.b;
}
}
Class Passobj
{
Public static void main(String arr[])
{
Test ob= new Test(15,20);
System.out.println(“a and b before
call”+ob.a+””+ob.b);
Ob.change(ob);
System.out.println(“a and b after
call”+ob.a+””+ob.b);
}
}
Output:==========================================
a and b before call 15 20
a and b after call 35 -20
variablelength argument:
Here the parameterlist for a variable argument method is not fixed but
rather variable in length.
The variable length argument is specifiedby three periods(…).
Example :
Class Varargs
{
Static void VaTest(int …v)
{
System.out.println(“number
of args:”+v.length);
For(int i=0;i<v.length;i++)
{
System.out.println(“arg”+i+”:”+v[i
]);
System.out.println();
}
3. System.out.println(“contents:”
);
Public static void main(String arr[])
{
VaTest(10);
VaTest(1,2,3);
VaTest();
}
}
Output:
No of args:1
Content:
Arg 0:10
No of args:3
Content:
Arg 0:1
Arg1:2
Arg2:3
No of args:0
Content:
Inside VaTest() v is operated on as array. This is because v is an
array. The … simple tells the compiler that a variables no. of
arguments will be used and that these arguments will be stored in
the array referred to by v.
Class Vararg
{
Public static void
main(string arg[])
4. Static void VaTest(string msg,int …v)
{
System.out.println(msg+v.length);
System.out.println();
For(int i=0;i<v.length;i++)
System.out.println(“arg”+i+”:”+v[i]);
System.out.println();
}
{
VaTest(“one vararg”,10);
VaTest(“three
Vararg:”,1,2,3);
VaTest(“No varargs:”);
}
Static
1. Static may be a variable.
2. Static may be a method
3. Static may be a block
Static Block:
Class Staticblock
{
Public static void main(String
arr[])
{
System.out.println(“main
method executed after”);
}
Static
{
System.out.println(“static block
is executed first”);
}
}
Static block can be used to check condition before the execution of
main method. So output of program is
Static block is executed first
Main method executed after
5. Static methods:
Class Language
{
Public static void
main(String arr[])
{
Display();
}
Static void display()
{
System.out.println(“java is
good I am inside static
method”);
}
}
Static method in java can be called without creating an object of
class.
Class Diff
{
Public static void main(String arr[])
{
Display(); //calling without
object
Diff t= new Diff(); //object ref.
t.show(); // calling using object
}
Static void display()
{
System.out.println(“Program is
Good”);
}
Void show()
{
System.out.println(“java is good
so I am inside method so called
me with object”);
}
}
So instance method require an object of its class to be created before
it can be called while static method does not require object creation.