Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wrote a simple Java code to swap 2 numbers as under:
import java.util.*;
import java.io.*;
import java.lang.*;
public class magicname2{
    
    public static void main(String args[]) throws java.util.NoSuchElementException
{
    Scanner sc=new Scanner(System.in);
   
     System.out.println("Enter a string value: ");
      String st = sc.nextLine();
 
     int j = 0;
      char temp = 0;
      char[] character =new char[st.length()];
      for(int m=0; m<=st.length()-1; m++)
      {
          character[m]= st.charAt(m);
      }
      for(int i=0; i < character.length; i++) {
         for(j=0; j < character.length; j++) {
            if(character[j] > character[i]) {
               temp = character[i];
               character[i] = character[j];
               character[j] = temp;
            }
         }
      }
      System.out.println("After Sorting:");
      
        String str1=new String(character);
System.out.println("Magic name is:"+str1);
}
}

Can anyone plz explain in detail what is the exact use of temp variable and how it is being utilized in swapping?

What I have tried:

import java.util.*;
import java.io.*;
import java.lang.*;
public class magicname2{
    
    public static void main(String args[]) throws java.util.NoSuchElementException
{
    Scanner sc=new Scanner(System.in);
   
     System.out.println("Enter a string value: ");
      String st = sc.nextLine();
 
     int j = 0;
      char temp = 0;
      char[] character =new char[st.length()];
      for(int m=0; m<=st.length()-1; m++)
      {
          character[m]= st.charAt(m);
      }
      for(int i=0; i < character.length; i++) {
         for(j=0; j < character.length; j++) {
            if(character[j] > character[i]) {
               temp = character[i];
               character[i] = character[j];
               character[j] = temp;
            }
         }
      }
      System.out.println("After Sorting:");
      
        String str1=new String(character);
System.out.println("Magic name is:"+str1);
}
}
Posted
Updated 3-Feb-23 21:36pm
Comments
Member 15627495 4-Feb-23 3:08am    
the 'temp' var permit you to hold a var, before its being replace by the next value.
A = B ; B is safe. ( allocated in A )
B = C ; before a new value replace it. ( B allocate by a different value )
C = A ; then you reload B in a new place ( as A contains B, you reload B in C )


this way, you keep all your vars, no loss ! and you process a swap with different vars.
It's 3 instructions lines, for a permutation ( swap ) of 2 values.

1 solution

You are kidding, right?
Java
temp = character[i];
character[i] = character[j];
character[j] = temp;

Save a copy of character[i] for later:
Java
temp = character[i];
Copy the value in character[j] into character[i] - if you hadn't saved a copy first, you would now have no idea what value it was.
Java
character[i] = character[j];
Copy the original contect of character[i] into character[j] so they are swapped.
Java
character[j] = temp;


To be honest, if you can't work that out code that trivial on your own, I'd suggest that computers are not for you ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900