- 2
byte a=10;byte b=20;byte c=a+b;/error!!
An instance of type int can not be assigned to a variable of type byte....what is the instance of type actually???????and why in that code byte a and b is an instance of int?
7 Réponses
+ 12
Ferdous Rayhan I want to tell '+' (addition) is not applicable to byte types if u don't specify what type u r using. It's an instance of type int.
+ 10
Ferdous Rayhan The java instanceof operator is used to test whether the object is an instance of the specified type (class or subclass or interface).
The instanceof in java is also known as type comparison operator because it compares the instance with type. It returns either true or false. If we apply the instanceof operator with any variable that has null value, it returns false.
Simple example of java instanceof
Let's see the simple example of instance operator where it tests the current class.
Input:
class Simple1{
public static void main(String args[]){
Simple1 s= new Simple1();
System.out.println(s instanceof Simple1); //true
}
}
Output:
true
+ 10
or u can write directly :
byte a = 10;
byte b = 10;
System.out.print(a+b);
+ 7
Tell the compiler that you know what you are doing, that you know no lossy conversion will happen.
byte a = 10;
byte b = 10;
byte c = (byte)(a+b);
System.out.print(c);
+ 1
byte a=10; how it is an instance of type int!!?
+ 1
here 10;20 those are int types??...byte a =10; here isnot 'a' is a byte type?why instance of int comes here ...but im using byte..not int..got my point???
+ 1
thanks a lot!