Swap() method in java , why this output?
-
I have this simple code and i would like to swap the variables a and b but the output is the same as in the beginning and i want to know why? my code public class Main { static ...show more
-
Answer:
You don't need a temp variable: public class Main { static String a; static String b; public static void main(String[] args) { a = "First String"; b = "Second String"; swap(a, b); System.out.println("The strings after swapping are "+a+" "+b); } public static void swap(String first, String second) { a = second; b = first; } }
C7VCA2IUEDH6HW472FFCXPRIYE at Yahoo! Answers Visit the source
Other answers
The changes are local. You can do this public class Main { static String temp; static String a1; static String b1; public Main(){ a1 ="First String"; b1 = "Second String"; swap(a1,b1); System.out.println("The strings after swapping are "+a1+" "+b1); } public static void main(String[] args ){ new Main(); } private static void swap(String a, String b){ a1 = b; b1 =a; System.out.println(temp); } }
sope
Related Q & A:
- Why cannot we use static keyword inside a method in java?Best solution by Stack Overflow
- How to get output on screen with java?Best solution by Stack Overflow
- How to get actual return type of a generic static method in Java?Best solution by stackoverflow.com
- What's the output from this program using fork() and why?Best solution by Stack Overflow
- Why is it important to use the Scientific Method?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.