From 50fb2e24af56a65eb559cf5fa937c8fd9de08842 Mon Sep 17 00:00:00 2001 From: Aadit Agrawal Date: Sat, 14 Sep 2024 01:56:58 +0530 Subject: [PATCH] Upload files to "OOP/Java/Lab/Week7" --- OOP/Java/Lab/Week7/AutoBoxUnBoxDemo.java | 39 ++++++++++++++++++++++++ OOP/Java/Lab/Week7/ObjectCountDemo.java | 28 +++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 OOP/Java/Lab/Week7/AutoBoxUnBoxDemo.java create mode 100644 OOP/Java/Lab/Week7/ObjectCountDemo.java diff --git a/OOP/Java/Lab/Week7/AutoBoxUnBoxDemo.java b/OOP/Java/Lab/Week7/AutoBoxUnBoxDemo.java new file mode 100644 index 0000000..344703d --- /dev/null +++ b/OOP/Java/Lab/Week7/AutoBoxUnBoxDemo.java @@ -0,0 +1,39 @@ +public class AutoBoxUnBoxDemo { + public static void main(String[] args) { + // Autoboxing examples + Integer intObj = 10; + Double doubleObj = 3.14; + Character charObj = 'A'; + Boolean boolObj = true; + + System.out.println("Autoboxing examples:"); + System.out.println("Integer: " + intObj); + System.out.println("Double: " + doubleObj); + System.out.println("Character: " + charObj); + System.out.println("Boolean: " + boolObj); + + // Unboxing examples + int intValue = intObj; + double doubleValue = doubleObj; + char charValue = charObj; + boolean boolValue = boolObj; + + System.out.println("\nUnboxing examples:"); + System.out.println("int: " + intValue); + System.out.println("double: " + doubleValue); + System.out.println("char: " + charValue); + System.out.println("boolean: " + boolValue); + + // Demonstrating autoboxing in method calls + printObject(50); // autoboxing int to Integer + + // Demonstrating unboxing in expressions + Integer sum = intObj + 5; // unboxing for addition, then autoboxing result + System.out.println("\nSum after unboxing and autoboxing: " + sum); + } + + public static void printObject(Object obj) { + System.out.println("\nObject passed: " + obj); + } +} + diff --git a/OOP/Java/Lab/Week7/ObjectCountDemo.java b/OOP/Java/Lab/Week7/ObjectCountDemo.java new file mode 100644 index 0000000..f9d003d --- /dev/null +++ b/OOP/Java/Lab/Week7/ObjectCountDemo.java @@ -0,0 +1,28 @@ +class ObjectCount { + private static int count = 0; + + public ObjectCount() { + count++; + } + + public static int getCount() { + return count; + } +} + +public class ObjectCountDemo { + public static void main(String[] args) { + System.out.println("Initial count: " + ObjectCount.getCount()); + + ObjectCount obj1 = new ObjectCount(); + System.out.println("Count after creating obj1: " + ObjectCount.getCount()); + + ObjectCount obj2 = new ObjectCount(); + System.out.println("Count after creating obj2: " + ObjectCount.getCount()); + + ObjectCount obj3 = new ObjectCount(); + System.out.println("Count after creating obj3: " + ObjectCount.getCount()); + + System.out.println("Final count: " + ObjectCount.getCount()); + } +}