SlideShare a Scribd company logo
1 of 5
 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.
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();
}
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[])
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
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.

More Related Content

What's hot

Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple ProgramsUpender Upr
 
Data structure scope of variables
Data structure scope of variablesData structure scope of variables
Data structure scope of variablesSaurav Kumar
 
The Ring programming language version 1.5.1 book - Part 19 of 180
The Ring programming language version 1.5.1 book - Part 19 of 180The Ring programming language version 1.5.1 book - Part 19 of 180
The Ring programming language version 1.5.1 book - Part 19 of 180Mahmoud Samir Fayed
 
The Ring programming language version 1.5.1 book - Part 30 of 180
The Ring programming language version 1.5.1 book - Part 30 of 180The Ring programming language version 1.5.1 book - Part 30 of 180
The Ring programming language version 1.5.1 book - Part 30 of 180Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 27 of 210
The Ring programming language version 1.9 book - Part 27 of 210The Ring programming language version 1.9 book - Part 27 of 210
The Ring programming language version 1.9 book - Part 27 of 210Mahmoud Samir Fayed
 
Reacting with ReactiveUI
Reacting with ReactiveUIReacting with ReactiveUI
Reacting with ReactiveUIkiahiska
 
Working effectively with legacy code
Working effectively with legacy codeWorking effectively with legacy code
Working effectively with legacy codeShriKant Vashishtha
 
Java8: Language Enhancements
Java8: Language EnhancementsJava8: Language Enhancements
Java8: Language EnhancementsYuriy Bondaruk
 
CS106 Lab 10 - Functions (passing by value)
CS106 Lab 10 - Functions (passing by value)CS106 Lab 10 - Functions (passing by value)
CS106 Lab 10 - Functions (passing by value)Nada Kamel
 
Operator Overloading & Type Conversions
Operator Overloading & Type ConversionsOperator Overloading & Type Conversions
Operator Overloading & Type ConversionsRokonuzzaman Rony
 
Concurrency in Programming Languages
Concurrency in Programming LanguagesConcurrency in Programming Languages
Concurrency in Programming LanguagesYudong Li
 
Kotlin Perfomance on Android / Александр Смирнов (Splyt)
Kotlin Perfomance on Android / Александр Смирнов (Splyt)Kotlin Perfomance on Android / Александр Смирнов (Splyt)
Kotlin Perfomance on Android / Александр Смирнов (Splyt)Ontico
 
classes & objects in cpp overview
classes & objects in cpp overviewclasses & objects in cpp overview
classes & objects in cpp overviewgourav kottawar
 

What's hot (20)

Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple Programs
 
Data structure scope of variables
Data structure scope of variablesData structure scope of variables
Data structure scope of variables
 
The Ring programming language version 1.5.1 book - Part 19 of 180
The Ring programming language version 1.5.1 book - Part 19 of 180The Ring programming language version 1.5.1 book - Part 19 of 180
The Ring programming language version 1.5.1 book - Part 19 of 180
 
Simple Java Programs
Simple Java ProgramsSimple Java Programs
Simple Java Programs
 
The Ring programming language version 1.5.1 book - Part 30 of 180
The Ring programming language version 1.5.1 book - Part 30 of 180The Ring programming language version 1.5.1 book - Part 30 of 180
The Ring programming language version 1.5.1 book - Part 30 of 180
 
The Ring programming language version 1.9 book - Part 27 of 210
The Ring programming language version 1.9 book - Part 27 of 210The Ring programming language version 1.9 book - Part 27 of 210
The Ring programming language version 1.9 book - Part 27 of 210
 
Reacting with ReactiveUI
Reacting with ReactiveUIReacting with ReactiveUI
Reacting with ReactiveUI
 
C# Method overloading
C# Method overloadingC# Method overloading
C# Method overloading
 
Working effectively with legacy code
Working effectively with legacy codeWorking effectively with legacy code
Working effectively with legacy code
 
C# Loops
C# LoopsC# Loops
C# Loops
 
Templates
TemplatesTemplates
Templates
 
Scala taxonomy
Scala taxonomyScala taxonomy
Scala taxonomy
 
Java8: Language Enhancements
Java8: Language EnhancementsJava8: Language Enhancements
Java8: Language Enhancements
 
CS106 Lab 10 - Functions (passing by value)
CS106 Lab 10 - Functions (passing by value)CS106 Lab 10 - Functions (passing by value)
CS106 Lab 10 - Functions (passing by value)
 
Loop c++
Loop c++Loop c++
Loop c++
 
MCRL2
MCRL2MCRL2
MCRL2
 
Operator Overloading & Type Conversions
Operator Overloading & Type ConversionsOperator Overloading & Type Conversions
Operator Overloading & Type Conversions
 
Concurrency in Programming Languages
Concurrency in Programming LanguagesConcurrency in Programming Languages
Concurrency in Programming Languages
 
Kotlin Perfomance on Android / Александр Смирнов (Splyt)
Kotlin Perfomance on Android / Александр Смирнов (Splyt)Kotlin Perfomance on Android / Александр Смирнов (Splyt)
Kotlin Perfomance on Android / Александр Смирнов (Splyt)
 
classes & objects in cpp overview
classes & objects in cpp overviewclasses & objects in cpp overview
classes & objects in cpp overview
 

Similar to Call by value and call by reference and varaiable length arguments

Autoboxing and unboxing
Autoboxing and unboxingAutoboxing and unboxing
Autoboxing and unboxingGeetha Manohar
 
Intro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technologyIntro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technologyworldchannel
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentalsHCMUTE
 
parameter passing in c#
parameter passing in c#parameter passing in c#
parameter passing in c#khush_boo31
 
The Art of Java Type Patterns
The Art of Java Type PatternsThe Art of Java Type Patterns
The Art of Java Type PatternsSimon Ritter
 
String and string buffer
String and string bufferString and string buffer
String and string bufferkamal kotecha
 
Programing with java for begniers .pptx
Programing with java for begniers  .pptxPrograming with java for begniers  .pptx
Programing with java for begniers .pptxadityaraj7711
 
Arrays and function basic c programming notes
Arrays and function basic c programming notesArrays and function basic c programming notes
Arrays and function basic c programming notesGOKULKANNANMMECLECTC
 
Topic20Arrays_Part2.ppt
Topic20Arrays_Part2.pptTopic20Arrays_Part2.ppt
Topic20Arrays_Part2.pptadityavarte
 
Materials for teachers and students java-en
Materials for teachers and students java-enMaterials for teachers and students java-en
Materials for teachers and students java-enGeorgeta Manafu
 
Hive Functions Cheat Sheet
Hive Functions Cheat SheetHive Functions Cheat Sheet
Hive Functions Cheat SheetHortonworks
 
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...vekariyakashyap
 

Similar to Call by value and call by reference and varaiable length arguments (20)

Java 5 Features
Java 5 FeaturesJava 5 Features
Java 5 Features
 
Autoboxing and unboxing
Autoboxing and unboxingAutoboxing and unboxing
Autoboxing and unboxing
 
Computer programming 2 Lesson 10
Computer programming 2  Lesson 10Computer programming 2  Lesson 10
Computer programming 2 Lesson 10
 
Intro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technologyIntro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technology
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
 
parameter passing in c#
parameter passing in c#parameter passing in c#
parameter passing in c#
 
Java String Handling
Java String HandlingJava String Handling
Java String Handling
 
The Art of Java Type Patterns
The Art of Java Type PatternsThe Art of Java Type Patterns
The Art of Java Type Patterns
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
 
Programing with java for begniers .pptx
Programing with java for begniers  .pptxPrograming with java for begniers  .pptx
Programing with java for begniers .pptx
 
Arrays and function basic c programming notes
Arrays and function basic c programming notesArrays and function basic c programming notes
Arrays and function basic c programming notes
 
Lecture 7 arrays
Lecture   7 arraysLecture   7 arrays
Lecture 7 arrays
 
Topic20Arrays_Part2.ppt
Topic20Arrays_Part2.pptTopic20Arrays_Part2.ppt
Topic20Arrays_Part2.ppt
 
Second chapter-java
Second chapter-javaSecond chapter-java
Second chapter-java
 
Second chapter-java
Second chapter-javaSecond chapter-java
Second chapter-java
 
Materials for teachers and students java-en
Materials for teachers and students java-enMaterials for teachers and students java-en
Materials for teachers and students java-en
 
Array Cont
Array ContArray Cont
Array Cont
 
Java ppt
Java pptJava ppt
Java ppt
 
Hive Functions Cheat Sheet
Hive Functions Cheat SheetHive Functions Cheat Sheet
Hive Functions Cheat Sheet
 
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
 

More from vishal choudhary (20)

SE-Lecture1.ppt
SE-Lecture1.pptSE-Lecture1.ppt
SE-Lecture1.ppt
 
SE-Testing.ppt
SE-Testing.pptSE-Testing.ppt
SE-Testing.ppt
 
SE-CyclomaticComplexityand Testing.ppt
SE-CyclomaticComplexityand Testing.pptSE-CyclomaticComplexityand Testing.ppt
SE-CyclomaticComplexityand Testing.ppt
 
SE-Lecture-7.pptx
SE-Lecture-7.pptxSE-Lecture-7.pptx
SE-Lecture-7.pptx
 
Se-Lecture-6.ppt
Se-Lecture-6.pptSe-Lecture-6.ppt
Se-Lecture-6.ppt
 
SE-Lecture-5.pptx
SE-Lecture-5.pptxSE-Lecture-5.pptx
SE-Lecture-5.pptx
 
XML.pptx
XML.pptxXML.pptx
XML.pptx
 
SE-Lecture-8.pptx
SE-Lecture-8.pptxSE-Lecture-8.pptx
SE-Lecture-8.pptx
 
SE-coupling and cohesion.ppt
SE-coupling and cohesion.pptSE-coupling and cohesion.ppt
SE-coupling and cohesion.ppt
 
SE-Lecture-2.pptx
SE-Lecture-2.pptxSE-Lecture-2.pptx
SE-Lecture-2.pptx
 
SE-software design.ppt
SE-software design.pptSE-software design.ppt
SE-software design.ppt
 
SE1.ppt
SE1.pptSE1.ppt
SE1.ppt
 
SE-Lecture-4.pptx
SE-Lecture-4.pptxSE-Lecture-4.pptx
SE-Lecture-4.pptx
 
SE-Lecture=3.pptx
SE-Lecture=3.pptxSE-Lecture=3.pptx
SE-Lecture=3.pptx
 
Multimedia-Lecture-Animation.pptx
Multimedia-Lecture-Animation.pptxMultimedia-Lecture-Animation.pptx
Multimedia-Lecture-Animation.pptx
 
MultimediaLecture5.pptx
MultimediaLecture5.pptxMultimediaLecture5.pptx
MultimediaLecture5.pptx
 
Multimedia-Lecture-7.pptx
Multimedia-Lecture-7.pptxMultimedia-Lecture-7.pptx
Multimedia-Lecture-7.pptx
 
MultiMedia-Lecture-4.pptx
MultiMedia-Lecture-4.pptxMultiMedia-Lecture-4.pptx
MultiMedia-Lecture-4.pptx
 
Multimedia-Lecture-6.pptx
Multimedia-Lecture-6.pptxMultimedia-Lecture-6.pptx
Multimedia-Lecture-6.pptx
 
Multimedia-Lecture-3.pptx
Multimedia-Lecture-3.pptxMultimedia-Lecture-3.pptx
Multimedia-Lecture-3.pptx
 

Recently uploaded

Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.MateoGardella
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 

Recently uploaded (20)

Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
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.