Search code examples
javabinaryoperatorsshorttype-promotion

No sense in using byte, short and char


I'm studying the book for the JAVA OCA certification. I'm in the chapter of binary operator. The book mentions the following rule.

byte, short and char will be promoted to int when they use binary operators.

So, at the moment, if I define and initialize a variable of type short or byte it will be automatically converted to int. In the book, it says = assignment is a binary operator.

Is there any point of using these kind of variables?

How can I print this type of variable in java?


Solution

  • Such variables would not be converted to int whenever you define them. They will be promoted to int when using them together with a binary operator like the & operator. Get class name of an object by using:

    variablename.getClass().getName()