Home »
Java »
Java find output programs
Java find output programs (Autoboxing & Unboxing) | set 1
Find the output of Java programs | Autoboxing & Unboxing | Set 1: Enhance the knowledge of Java Autoboxing & Unboxing concepts by solving and finding the output of some Java programs.
Submitted by Nidhi, on February 04, 2021
Question 1:
public class BoxingEx {
public static void main(String[] args) {
int intVar = 50;
Object Obj = intVar;
intVar = 100;
System.out.println(intVar);
System.out.println(Obj);
}
}
Output:
100
50
Explanation:
In the above program, we created a class BoxingEx that contains the main() method. The main() method is the entry point for the program. Here, we created a local variable intVar initialized with 50.
Object Obj = intVar;
In the above statement, we perform Autoboxing of variable intVar and assigned to the object Obj, and then we assigned the value 20 into variable intVar, and then print the value of intVar and Obj on the console screen.
Question 2:
public class BoxingEx {
public static void main(String[] args) {
int intVar = 50;
int result = 0;
Object Obj = intVar;
intVar = 100;
result = intVar + Obj;
System.out.println(result);
}
}
Output:
/BoxingEx.java:10: error: bad operand types for binary operator '+'
result = intVar + Obj;
^
first type: int
second type: Object
1 error
Explanation:
The above program will generate an error because of the below statement,
result = intVar + Obj;
Here, we added variable intVar with object Obj. But we cannot access add integer variable with object Obj.
Question 3:
public class BoxingEx {
public static void main(String[] args) {
int intVar = 50;
int result = 0;
Integer Obj = intVar;
intVar = 100;
result = intVar + Obj;
System.out.println(result);
}
}
Output:
150
Explanation:
In the above program, we created two integer variables intVar and result initialized with 50 and 0 respectively.
Integer Obj = intVar;
intVar = 100;
In the above statement we autobox the variable intVar into object of Wrapper Integer class, and assign value 100 to the variable intVar Then we added intVar and Obj and assign the result into result variable, and finally print the variable result on the console screen.
Question 4:
public class BoxingEx {
public static void main(String[] args) {
int intVar = 150;
int result = 0;
Object Obj = intVar;
intVar = 100;
result = intVar + (int) Obj;
System.out.println(result);
}
}
Output:
250
Explanation:
In the above program, we created two integer variables intVar and result initialized with 150 and 0 respectively.
Object Obj = intVar;
intVar = 100;
In the above statement, we autobox the variable intVar into object, and assign value 100 to the variable intVar.
result = intVar + (int)Obj;
In the above statement, we typecast object Obj into integer and added to the variable intVar and assigned the result to the variable result. After that print the value of the result variable on the console screen.
Question 5:
public class BoxingEx {
public static void main(String[] args) {
float floatVar = 12.5;
float result = 0;
Float Obj = floatVar;
floatVar = 13.6;
result = floatVar + (float) Obj;
System.out.println(result);
}
}
Output:
/BoxingEx.java:3: error: incompatible types:
possible lossy conversion from double to float
float floatVar = 12.5;
^
/BoxingEx.java:8: error: incompatible types:
possible lossy conversion from double to float
floatVar = 13.6;
^
2 errors
Explanation:
The above program will generate syntax errors because of the below statements,
float floatVar = 12.5;
floatVar = 13.6;
In the both above statement, we assigned double value instead of float value, here we need to use character 'F' in the suffix of a given value. The correct code is given below,
float floatVar = 12.5F;
floatVar = 13.6F;