bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Java/Java How To's
Java•Java How To's

Java How To Swap Two Variables

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 .

Previous

Java How To Add Two Numbers

Next

Java How To Find Even or Odd Numbers