Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,933 questions

51,870 answers

573 users

How to swap two numbers with function in Java

1 Answer

0 votes
package javaapplication1;

public class JavaApplication1 {

    public static void main(String[] args) {

        int[] arr = new int[]{0, 0};
        int n1 = 50;
        int n2 = 35;
        
        arr[0] = n1;
        arr[1] = n2;
        
        swap(arr);
       
        n1 = arr[0];
        n2 = arr[1];
        
        System.out.println("n1 = " + n1);
        System.out.println("n2 = " + n2);
    }
    private static void swap(int[] arr) 
    {
        int tmp = arr[0];
        arr[0] = arr[1];
        arr[1] = tmp;
   }
}
  
/*
run:
 
n1 = 35
n2 = 50
  
*/

 



answered Sep 19, 2016 by avibootz

Related questions

1 answer 147 views
1 answer 170 views
2 answers 227 views
227 views asked Jan 11, 2022 by avibootz
4 answers 248 views
1 answer 554 views
1 answer 150 views
1 answer 156 views
156 views asked Apr 30, 2019 by avibootz
...