Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type cast vs literal assignment

Tags:

java

casting

Consider the following:

class TypeCast {
    public static void main(String[] args) {
        byte by = 4;   // Compiler casts int literal to byte
        int in = 4;
        byte byt = in; // Compilation Error: compiler can not cast automatically. WHY?
    }
}

I know compiler can do the narrowing in case of literal assignment. But it can't do the same when assignment involves variable instead of literal. Why?

EDIT: I think most people could not understand what I was trying to ask. It is not about assigning 'out-of-range' value it is about assigning 'in-range' value to byte and let compiler take care of the narrowing. It is quite obvious 'byte' won't be able to handle out of range value and explicit conversion would require(and that is not I want to know).

Given value falls in byte range, what is the difference between int literal assignment to byte and int type variable assignment to byte ?

like image 843
maximus335 Avatar asked Feb 10 '15 12:02

maximus335


People also ask

What is an example of typecasting?

An example of typecasting is converting an integer to a string. This might be done in order to compare two numbers, when one number is saved as a string and the other is an integer. For example, a mail program might compare the first part of a street address with an integer.

What do you mean by typecasting?

Type casting is when you assign a value of one primitive data type to another type. In Java, there are two types of casting: Widening Casting (automatically) - converting a smaller type to a larger type size. byte -> short -> char -> int -> long -> float -> double.

What is purpose of type typecasting?

Type casting is a way of converting data from one data type to another data type. This process of data conversion is also known as type conversion or type coercion. In Java, we can cast both reference and primitive data types. By using casting, data can not be changed but only the data type is changed.

Can a short be assigned to an int without a cast?

Assigning a short value to an int without a cast is allowed because all of the values that can be represented by a short can also be represented by int. However, assigning an int value to a short is not allowed without a cast because it involves going from a 32-bit signed quantity to a 16-bit signed quantity.


2 Answers

Because in your code you can change the value of a variable. That's why it is not allowed to assign an int variable to a byte, but if you declare your int variable as final it will allow try this:

 public class Test {
    public static void main(String[] args) {
       final int i = 10;
       byte by = i;
    }
 }

It means 10 is in range of byte, so all is fine, but if you write

public class Test {
   public static void main(String[] args) {
      final int i = 10000;
      byte by = i;
   }
 }

it will give you an error, because 10000 is not in the range of a byte.

like image 55
Keval Avatar answered Oct 02 '22 11:10

Keval


The main reason, IMHO, is that a code like that (which actually doesn't compile)

  int in = ...   // what if in == 1234?;
  byte byt = in; // then byt == -46. Can you expect this?

is dangerous, because of potential overflow (int 1234 becomes -46 byte). However,

  byte byt = 4; 

is safe since illegal (overflow) code like

  byte byt = 1234; // doesn't compile   

will cause a compile time error. You can insist, however:

  // I'm warned about the overflow, but do as I command...
  byte byt = (byte) 1234; // -46

or

  int in = ...
  byte byt = (byte) in; // I know what I'm doing! Cast it, please 
like image 43
Dmitry Bychenko Avatar answered Oct 02 '22 09:10

Dmitry Bychenko