Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: JAVA - Swap two string in a method without built-in functions

  1. #1
    Junior Member
    Join Date
    Jun 2020
    Posts
    11
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default JAVA - Swap two string in a method without built-in functions

    Hello,

    I want to swap two string in JAVA without built-in function like this.
    How can I fix it?

    Thanks
    import java.util.*;
     
    public class MainClass {
     
     
    	public static void Swap(String myString, String yourString)
    	{
    		String S = myString;
    		myString = yourString;
    		yourString = S;
    	}	public static void main(String[] args) {
     
    		Scanner S = new Scanner(System.in);
    		String Str = S.nextLine();
    		String Str2 = S.nextLine();
    		Swap(Str,Str2);
    		System.out.printf("My String %s \n",Str);
    		System.out.printf("Your String %s \n",Str2);
     
     
    	}
     
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JAVA - Swap two string in a method without built-in functions

    swap two string in JAVA without built-in function like this.
    swap is not a built-in method.
    swap(str,str2);
    Note: the swap method will NOT change the values of str or str2

    Naming conventions say variable names and method names should start with a lower-case letter.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    shervan_360 (June 6th, 2020)

  4. #3
    Junior Member
    Join Date
    Jun 2020
    Posts
    11
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: JAVA - Swap two string in a method without built-in functions

    Quote Originally Posted by Norm View Post
    swap is not a built-in method.
    swap(str,str2);
    Note: the swap method will NOT change the values of str or str2

    Naming conventions say variable names and method names should start with a lower-case letter.
    Yes, swap is not built-in method. now how can I fix it? because in JAVA we don't have reference type and String is immutable.
    How can I swap two string in that method?

    Thanks

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JAVA - Swap two string in a method without built-in functions

    A method can not change the values of the variables used in its call.
    It can change the contents of an array or of a class object that is passed to it if the object allows it.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Why is the swap method static?
    By jenniferruurs in forum Java Theory & Questions
    Replies: 1
    Last Post: October 4th, 2019, 09:31 AM
  2. Write a Java program to swap two variables.
    By Ricky Buen in forum What's Wrong With My Code?
    Replies: 8
    Last Post: April 9th, 2018, 12:43 PM
  3. Replies: 1
    Last Post: April 10th, 2014, 06:07 AM
  4. 1. Built a calculator that contains the following functions
    By karem2012 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 27th, 2012, 10:02 AM
  5. Replies: 1
    Last Post: April 19th, 2012, 02:46 AM