How to Swap Values in JAVA

Hello Coders !!!
Today, I'm going to start our Fourth Project from the "Series of Small JAVA Projects" to enlighten JAVA basics and make you understand by writing codes and executing them.
So, Let's start with today's topic 'How to Swap Values in JAVA'
For Executing the code I'm going to use an IDE (Integrated Development Environment) called IntelliJ IDEA to run your code. This is one of the best IDE and very Smart IDE to help you write faster code in JAVA. Just Install its community version from its official website and start using it.
Steps to Create New Project in IntelliJ Idea as follows:-
---> Go to File > New > Project > Select Java > click Next >...
> check on Create project from template > & give your Project Name (I'm giving Fourth Project) > click on Finish.
---> Now click on a project folder and see the folder name 'src', hit right click on that folder and go to New then click on Package and give the name (I'm giving 'SwapValues' for this project) and hit Enter.
Now a new package will be created, then go to 'Print' & right-click, go to New & click on JAVA Class, go ahead and give a name for the JAVA class (I'm giving the name for this project 'SwapFirst') and hit Finish.
Here we go, Our Project, Package & JAVA class is created.
Here's the Code!
package SwapValues;
class SwapFirst
{
int num1, num2, a, b;
SwapFirst(int a, int b)
{
num1 = a;
num2 = b;
}
}
class SwapSecond
{
void swap(SwapFirst s)
{
int output = s.num1;
s.num1 = s.num2;
s.num2 = output;
}
}
class callref
{
public static void main(String args[])
{
SwapFirst s1 = new SwapFirst(20, 10);
System.out.println("Before Swapping :" + s1.num1 + "\t" + s1.num2);
SwapSecond s2 = new SwapSecond();
s2.swap(s1);
System.out.println("After Swapping :" + s1.num1 + "\t" + s1.num2);
}
}
/* If you aren't able to see full code on your mobile device, just rotate your screen to landscape mode. */
Tips:- After writing the code for the first time you need to "RUN" the code by hitting right-click and click on 'Run', only for the first time, after this, you can just click on the Run button available on Right-Hand Top corner.
This is 'How to Swap Values in JAVA' & this is how we take One Value and Swap the Value to another and print out the desired results in JAVA. Just Install IntelliJ IDEA from the link https://bit.ly/2RHmQRC and start coding.
For any queries you can just ask me in the comment below, I'll help you to the best of my knowledge.
Hope you like it. Stay Tuned, A lot more series of small JAVA projects are coming.
Don't forget to see our next project posting every other day.
Give your Love & Support.
HAPPY CODING !!!
Comments
Post a Comment