- Java Tutorial
- Java Introduction
- Java Features
- Java Simple Program
- JVM, JDK and JRE
- Java Syntax
- Java Comments
- Java Keywords
- Java Variables
- Java Literals
- Java Separators
- Java Datatypes
- Java Operators
- Java Statements
- Java Strings
- Java Arrays
- Control Statement
- Java If
- Java If-else
- Java If-else-if
- Java Nested If
- Java Switch
- Iteration Statement
- Java For Loop
- Java For Each Loop
- Java While Loop
- Java Do While Loop
- Java Nested Loop
- Java Break/Continue
- Java Methods
- Java Methods
- Java Method Parameters
- Java Method Overloading
- Java Recursion
- Java OOPS
- Java OOPs
- Java Classes/Objects
- Java Inheritance
- Java Polymorphism
- Java Encapsulation
- Java Abstraction
- Java Modifiers
- Java Constructors
- Java Interface
- Java static keyword
- Java this keyword
- Java File Handling
- Java File
- Java Create File
- Java Read/Write File
- Java Delete File
- Java Program To
- Add Two Numbers
- Even or Odd Numbers
- Reverse a String
- Swap Two Numbers
- Prime Number
- Fibonacci Sequence
- Palindrome Strings
- Java Reference
- Java String Methods
- Java Math Methods
Java Program To Swap Two Number
Here's a simple Java program to swap two numbers:
public class SwapNumbers {
public static void main(String[] args) {
// Define two numbers
int a = 5;
int b = 10;
System.out.println("Before swapping:");
System.out.println("a = " + a);
System.out.println("b = " + b);
// Swap numbers
int temp = a;
a = b;
b = temp;
System.out.println("\nAfter swapping:");
System.out.println("a = " + a);
System.out.println("b = " + b);
}
}
This program initializes two variables a and b with values 5 and 10 respectively. It then prints the values of a and b before swapping. After that, it swaps the values of a and b using a temporary variable temp, and finally prints the values of a and b after swapping.