public class GreatQues {
public static void main(String[] args) {
int a = 10;
int b =20;
System.out.println("Sum of the numbers : " + a + b);
//System.out.println("Diff of the numbers : " + a - b); // (Line 1) Compile time error
System.out.println("Product of the numbers : " + a * b);
System.out.println("Division of the numbers : " + a / b);
short x = 127;
short y = 140;
//short z = x + y; // Compile time error (Line 2)
System.out.println(127 + 140);
}
}
Output :
Sum of the numbers : 1020
Product of the numbers : 200
Division of the numbers : 0
267
Explanation :
When we write sysout ( String + some integer), the compiler treats that interger as a String only and just appends the value to the String.
- operator is not overloaded in String, therefore, the Line 1 gives compile time error saying : - operator is undefined for arguments of type String and int.
Line 2 gives compile time error because any mathematical operation on two shorts / ints / bytes always gives int as result.
1 comment:
could you post other questions as well
Post a Comment