From 58e26e46aeb1bff108e1cf9e5f1a25f46ee22cde Mon Sep 17 00:00:00 2001 From: code5am <70967850+code5am@users.noreply.github.com> Date: Tue, 25 Oct 2022 21:36:16 +0530 Subject: [PATCH] Create swap.java --- 5.Java/swap.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 5.Java/swap.java diff --git a/5.Java/swap.java b/5.Java/swap.java new file mode 100644 index 0000000..ff675de --- /dev/null +++ b/5.Java/swap.java @@ -0,0 +1,24 @@ +public class SwapNumbers { + + public static void main(String[] args) { + + float first = 1.20f, second = 2.45f; + + System.out.println("--Before swap--"); + System.out.println("First number = " + first); + System.out.println("Second number = " + second); + + // Value of first is assigned to temporary + float temporary = first; + + // Value of second is assigned to first + first = second; + + // Value of temporary (which contains the initial value of first) is assigned to second + second = temporary; + + System.out.println("--After swap--"); + System.out.println("First number = " + first); + System.out.println("Second number = " + second); + } +}