byte a=10;byte b=20;byte c=a+b;/error!! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 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?

6th Jun 2018, 4:56 AM
Ferdous Rayhan
Ferdous Rayhan - avatar
7 Answers
+ 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.
6th Jun 2018, 5:23 AM
***
+ 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
6th Jun 2018, 4:59 AM
***
+ 10
or u can write directly : byte a = 10; byte b = 10; System.out.print(a+b);
6th Jun 2018, 5:28 AM
***
+ 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);
6th Jun 2018, 5:26 AM
Hatsy Rei
Hatsy Rei - avatar
+ 1
byte a=10; how it is an instance of type int!!?
6th Jun 2018, 5:06 AM
Ferdous Rayhan
Ferdous Rayhan - avatar
+ 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???
6th Jun 2018, 5:17 AM
Ferdous Rayhan
Ferdous Rayhan - avatar
+ 1
thanks a lot!
6th Jun 2018, 6:26 AM
Ferdous Rayhan
Ferdous Rayhan - avatar