In this tutorial, we will discuss a detailed step-by-step guide for swapping two numbers without using a third variable in Java. This is a commonly asked question in Java interviews and Automation Testing. Through this Java program, interviewers aim to evaluate your logical skills and knowledge of Java programming.
Example:
package TestAutomationCentral; public class SwapNumber { public static void main(String[] args) { //TestAutomationCentral.com //Swap two numbers without third variable int x = 2, y = 4; System.out.println("Before Swap"); System.out.println("x: " + x + "\n" + "y: " + y); x = x + y; y = x - y; x = x - y; System.out.println("After Swap"); System.out.println("x: " + x + "\n" + "y: " + y); } }