Flash cards
Review the key moves
1/4
Core idea
What is the main idea behind Java How To Swap Two Variables?
Lesson checks
Practice each idea before moving on
Short Mimo-style checks built from this lesson's code, terms, and sequence.
1Quick choice
Which statement best captures the main point of this lesson?
2Fill blank
Complete the missing token from the example code.
___ temp = a;3Order
Put the learning moves in the order that makes the concept easiest to apply.
Explanation: We save a in a temporary variable, move b into a , and then move the saved value into b .
Exchange the values of two variables using a temporary variable:
Swap Two Variables
Swap Two Variables
Exchange the values of two variables using a temporary variable:
Example
int a = 5;
int b = 10;
int temp = a;
a = b;
b = temp;
System.out.println("a = " + a + ", b = " + b);Explanation: We save a in a temporary variable, move b into a , and then move the saved value into b .