SlideShare a Scribd company logo
1 of 1
Download to read offline
Hey,
Why is it not possible in Java to write a swap routine that swaps two variables of the same
primitive type?
Solution
Java passes method argument as reference by value and hence a direct swap is not possible (this
is possible in language like C where you can pass reference instead of value and get swapped
output).
That being said here is how you can do swap of primitive for example int:

More Related Content

More from FORTUNE2505

In Aristotles Poetics he speaks of epics and tragedies. How do.pdf
In Aristotles Poetics he speaks of epics and tragedies. How do.pdfIn Aristotles Poetics he speaks of epics and tragedies. How do.pdf
In Aristotles Poetics he speaks of epics and tragedies. How do.pdf
FORTUNE2505
 
Name the type of chemical messenger is released from the axon termina.pdf
Name the type of chemical messenger is released from the axon termina.pdfName the type of chemical messenger is released from the axon termina.pdf
Name the type of chemical messenger is released from the axon termina.pdf
FORTUNE2505
 
Marilee Jones, the former dean of admissions of the Massachusetts In.pdf
Marilee Jones, the former dean of admissions of the Massachusetts In.pdfMarilee Jones, the former dean of admissions of the Massachusetts In.pdf
Marilee Jones, the former dean of admissions of the Massachusetts In.pdf
FORTUNE2505
 
Know how the complement of mRNAs in a cell can be assayed (in plants.pdf
Know how the complement of mRNAs in a cell can be assayed (in plants.pdfKnow how the complement of mRNAs in a cell can be assayed (in plants.pdf
Know how the complement of mRNAs in a cell can be assayed (in plants.pdf
FORTUNE2505
 
I need to implement in c++ this non-List member function void print.pdf
I need to implement in c++ this non-List member function void print.pdfI need to implement in c++ this non-List member function void print.pdf
I need to implement in c++ this non-List member function void print.pdf
FORTUNE2505
 
I have another assignment due for an advance java programming class .pdf
I have another assignment due for an advance java programming class .pdfI have another assignment due for an advance java programming class .pdf
I have another assignment due for an advance java programming class .pdf
FORTUNE2505
 
4. Real gross domestic prodact A. measures the value of fimal goods a.pdf
4. Real gross domestic prodact A. measures the value of fimal goods a.pdf4. Real gross domestic prodact A. measures the value of fimal goods a.pdf
4. Real gross domestic prodact A. measures the value of fimal goods a.pdf
FORTUNE2505
 
create your own Security Management Model using the NIST Special Pub.pdf
create your own Security Management Model using the NIST Special Pub.pdfcreate your own Security Management Model using the NIST Special Pub.pdf
create your own Security Management Model using the NIST Special Pub.pdf
FORTUNE2505
 
Consider the following simple macroeconomic model C, 0 + ,K + 2.pdf
Consider the following simple macroeconomic model C, 0 + ,K + 2.pdfConsider the following simple macroeconomic model C, 0 + ,K + 2.pdf
Consider the following simple macroeconomic model C, 0 + ,K + 2.pdf
FORTUNE2505
 
Can you convert this code into a C++ code.pdf
Can you convert this code into a C++ code.pdfCan you convert this code into a C++ code.pdf
Can you convert this code into a C++ code.pdf
FORTUNE2505
 

More from FORTUNE2505 (19)

Researchers argue about whether Oldowan toolmakers were hunters or s.pdf
Researchers argue about whether Oldowan toolmakers were hunters or s.pdfResearchers argue about whether Oldowan toolmakers were hunters or s.pdf
Researchers argue about whether Oldowan toolmakers were hunters or s.pdf
 
In Aristotles Poetics he speaks of epics and tragedies. How do.pdf
In Aristotles Poetics he speaks of epics and tragedies. How do.pdfIn Aristotles Poetics he speaks of epics and tragedies. How do.pdf
In Aristotles Poetics he speaks of epics and tragedies. How do.pdf
 
Name the type of chemical messenger is released from the axon termina.pdf
Name the type of chemical messenger is released from the axon termina.pdfName the type of chemical messenger is released from the axon termina.pdf
Name the type of chemical messenger is released from the axon termina.pdf
 
Marilee Jones, the former dean of admissions of the Massachusetts In.pdf
Marilee Jones, the former dean of admissions of the Massachusetts In.pdfMarilee Jones, the former dean of admissions of the Massachusetts In.pdf
Marilee Jones, the former dean of admissions of the Massachusetts In.pdf
 
Know how the complement of mRNAs in a cell can be assayed (in plants.pdf
Know how the complement of mRNAs in a cell can be assayed (in plants.pdfKnow how the complement of mRNAs in a cell can be assayed (in plants.pdf
Know how the complement of mRNAs in a cell can be assayed (in plants.pdf
 
In Module 5 the you are here slide indicates we are currently in.pdf
In Module 5 the you are here slide indicates we are currently in.pdfIn Module 5 the you are here slide indicates we are currently in.pdf
In Module 5 the you are here slide indicates we are currently in.pdf
 
Identify the role you believe mobile devices have on email investiga.pdf
Identify the role you believe mobile devices have on email investiga.pdfIdentify the role you believe mobile devices have on email investiga.pdf
Identify the role you believe mobile devices have on email investiga.pdf
 
I need to implement in c++ this non-List member function void print.pdf
I need to implement in c++ this non-List member function void print.pdfI need to implement in c++ this non-List member function void print.pdf
I need to implement in c++ this non-List member function void print.pdf
 
I have another assignment due for an advance java programming class .pdf
I have another assignment due for an advance java programming class .pdfI have another assignment due for an advance java programming class .pdf
I have another assignment due for an advance java programming class .pdf
 
4. Real gross domestic prodact A. measures the value of fimal goods a.pdf
4. Real gross domestic prodact A. measures the value of fimal goods a.pdf4. Real gross domestic prodact A. measures the value of fimal goods a.pdf
4. Real gross domestic prodact A. measures the value of fimal goods a.pdf
 
Hypothesis What is your hypothesis for the genetic characteristi.pdf
Hypothesis  What is your hypothesis for the genetic characteristi.pdfHypothesis  What is your hypothesis for the genetic characteristi.pdf
Hypothesis What is your hypothesis for the genetic characteristi.pdf
 
How is a process shown in a DFD(data flow diagramSolution Data.pdf
How is a process shown in a DFD(data flow diagramSolution  Data.pdfHow is a process shown in a DFD(data flow diagramSolution  Data.pdf
How is a process shown in a DFD(data flow diagramSolution Data.pdf
 
5. As know, a virulent strain ofstaphylococcus aureus has arisen that.pdf
5. As know, a virulent strain ofstaphylococcus aureus has arisen that.pdf5. As know, a virulent strain ofstaphylococcus aureus has arisen that.pdf
5. As know, a virulent strain ofstaphylococcus aureus has arisen that.pdf
 
Description of the labCinnamaldehyde was separated from a solutio.pdf
Description of the labCinnamaldehyde was separated from a solutio.pdfDescription of the labCinnamaldehyde was separated from a solutio.pdf
Description of the labCinnamaldehyde was separated from a solutio.pdf
 
Describe benefits and shortcomings of transmission control protocol .pdf
Describe benefits and shortcomings of transmission control protocol .pdfDescribe benefits and shortcomings of transmission control protocol .pdf
Describe benefits and shortcomings of transmission control protocol .pdf
 
create your own Security Management Model using the NIST Special Pub.pdf
create your own Security Management Model using the NIST Special Pub.pdfcreate your own Security Management Model using the NIST Special Pub.pdf
create your own Security Management Model using the NIST Special Pub.pdf
 
Consider the following simple macroeconomic model C, 0 + ,K + 2.pdf
Consider the following simple macroeconomic model C, 0 + ,K + 2.pdfConsider the following simple macroeconomic model C, 0 + ,K + 2.pdf
Consider the following simple macroeconomic model C, 0 + ,K + 2.pdf
 
12. Explain why concentrated benefits and widely disbursed costs are.pdf
12. Explain why concentrated benefits and widely disbursed costs are.pdf12. Explain why concentrated benefits and widely disbursed costs are.pdf
12. Explain why concentrated benefits and widely disbursed costs are.pdf
 
Can you convert this code into a C++ code.pdf
Can you convert this code into a C++ code.pdfCan you convert this code into a C++ code.pdf
Can you convert this code into a C++ code.pdf
 

Recently uploaded

Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
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
PECB
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 

Recently uploaded (20)

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
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
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
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
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
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
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
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 

Hey,Why is it not possible in Java to write a swap routine that sw.pdf

  • 1. Hey, Why is it not possible in Java to write a swap routine that swaps two variables of the same primitive type? Solution Java passes method argument as reference by value and hence a direct swap is not possible (this is possible in language like C where you can pass reference instead of value and get swapped output). That being said here is how you can do swap of primitive for example int: