Upload files to "OOP/Java/Lab/Week7"
This commit is contained in:
parent
4091242dd9
commit
50fb2e24af
39
OOP/Java/Lab/Week7/AutoBoxUnBoxDemo.java
Normal file
39
OOP/Java/Lab/Week7/AutoBoxUnBoxDemo.java
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
28
OOP/Java/Lab/Week7/ObjectCountDemo.java
Normal file
28
OOP/Java/Lab/Week7/ObjectCountDemo.java
Normal file
@ -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());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user