Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Frequently Asked Java Programs: 32 – Swap Two String Variables Without Using Third Variable

Posted on 01/14/2025 By admin

Hello Folks,

As part of Frequently Asked Java Programs In Interviews For Freshers And Experienced, in this post we will see a java program to swap two String variables without using third variable.

Problem Statement:-

Before Swapping:

String s1= “Java”;

String s2= “Selenium”;

After Swapping:

String s1= “Selenium”;

String s2= “Java”;

We have already seen Swapping two number variables without using third variable.

First concatenate S1 and S2 and store in any existing variable say s1. After step 1 , we will variables with values as:-

s1= SeleniumJava

s2= Java

Since we have concatenated string in s1, so let’s swap value for s2 first.

s2 should have “Selenium” which starts from zeroth index of s1. If we minus length of s2 from length of concatenated string s1 i.e. (12-4=8) will be swapped value for s2. So use subString method of String class and put starting index as zero and end index as 8.

s2= s1.subString(0, (s1.length()-s2.length);

After Step 2, we will have below values:-

s1= SeleniumJava

s2= Selenium

We have done half of work.

To swap value of s1 , We can use subString method again and put starting index as length of swapped value of s2 i.e. 8. As from 8th index of s1 is desired value. No need to give last index as it will go by default to end if not provided. After Step 3. you will have below values:-

s1= “Java”;

s2= “Selenium”;

package StringPrograms;


public class SwapTwoStringVariablesWitoutThirdVariable {
        
        
        public static void main(String[] args) {
                
                String s1 = "Java";
                String s2= "Selenium";
                
                
                System.out.println("-----Before swapping--------");
                System.out.println("S1= "+s1);
                System.out.println("S2= "+s2);
                
                
                // Concatenate value of S2 with S1. Note here that value of S2 will be changed. 
                s1= s1+s2;
                // The swap value of S2 will start from zeroth index in concatenated string. End index will be 
                // total length of concatenated string minus length of S2. 
                s2= s1.substring(0,(s1.length()-s2.length()));
                // The swap value of S1 will be starting at the end of length of swap value of S2 in concatenated 
                // string S1.
                s1= s1.substring(s2.length());
                
                
                System.out.println("-----Afetr swapping--------");
                System.out.println("S1= "+s1);
                System.out.println("S2= "+s2);
                
        }

}

—–Before swapping——– S1= Java S2= Selenium —–After swapping——– S1= Selenium

S2= Java

Uncategorized

Post navigation

Previous Post: JSON with JSONPath
Next Post: java interview question

Related Posts

July 15, 2018 – Make Selenium Easy Uncategorized
Make Selenium Easy – Page 2 of 57 – Uncategorized
Amod Mahajan, Author at Make Selenium Easy – Page 41 of 41 Uncategorized
September 17, 2018 – Make Selenium Easy Uncategorized
indepDep – Make Selenium Easy Uncategorized
Hierarchy of Selenium Classes and Interfaces – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark