Cast of byte type integer

3

I have the following code:

public class Principal {
     public static void main(String[] args){

      int i = 10;
      byte b = 5;
      b = (byte)i;
      b += i;
      i = b;

      System.out.println( i );
      }
}

I know the value returned in b = 20 .

But why? I would like to understand what happens when I put cast(byte) into int .

Question 127 page 4 contest

    
asked by anonymous 18.01.2016 / 19:53

4 answers

4

Before answering the question itself, it is important to mention that this byte b = 5 statement is not changing the result at all. It could have been done directly byte b = (byte)i .

On the result, you need to understand the types involved in the question, especially the type byte .

In Java, the type byte has a signal and has a size of 1 byte, therefore, 8 bits. Using 8 bits it is possible to map 256 characters, in this case, integers. Since the variable has a signal, then the range goes from [-128,127].

That said, if the integer you are casting to byte is in this range, then it will print correctly, if it is not, an overflow will occur in the byte variable and what will be printed will be a number in that range, but not the int for which you are casting.

See this: link

Update

The question has been changed in a meaningful way which made me edit the answer.

The result was not 10 as previously mentioned, but 20 , since a sum of b+=i is made after the cast . However, this does not change the explanation, since 20 is a number that is in the range of byte in Java.

    
18.01.2016 / 20:09
3

Whenever a cast is made of a byte numeral type, understand the bits that are involved, in the case of your example this happens:

00000000 00000000 00000000 00001110 00001110

It will simply discard everything left that is beyond the first byte. It is also worth noting that the Java byte has a signal, so the first bit helps to identify the signal.

And about your code example, run in which version of java it is, the result will always be 10 as well. This causes a very strange behavior, because, for example, 127 in int is 127 in byte, but 128 in int is -128 in byte.

I recommend trying bit shift commands to better understand how the data structure works at the bit level.

    
18.01.2016 / 20:27
3

Note that the original question gives a wrong clue to what was actually asked in the contest. Now it is not very precise either. In anything, but especially in contests, it is essential to be accurate in interpreting or communicating what the problem is. Otherwise errors will occur because of communication deficiency rather than lack of knowledge.

In this case, the statement in question 127 is true. It will print 20.

  int i = 10; // i está valendo 10
  byte b = 5; // b está valendo 5
  b = (byte)i; // agora b passa valer 10, o cast não o afeta, dentro dentro da capacidade do tipo
  b += i; //agora b vale o seu próprio valor mais o de i, então 10 de b e 10 de i = 20
  i = b; // agora i passa ter o mesmo valor de b, ou seja, 20.

cast did not affect the value. It could have affected if the i value was outside the accepted byte type (-128 through 127). The same goes for adding the value of i to b . The value of i that has 4 bytes of storage "fit" within b which has only 1 byte. The type byte allows to represent 256 distinct numbers (2 raised to 8), while type int allows (from -2,147,483,648 to 2,147,483,647 4,294,967,295 - 2 elevated to 32).

    
18.01.2016 / 20:46
0

If we execute the code below:

public class HelloWorld{
    public static void main(String []args){
        int i = 10;
        byte b = 5;
        b = (byte) i;
        System.out.println(b); // esta linha irá imprimir 10
     }
 }

The value "10" will be printed. Because it is a value that fits into a byte-type variable. But if I change the code to:

public class HelloWorld{
    public static void main(String []args){
        int i = 300;
        byte b = 5;
        b = (byte) i;
        System.out.println(b); // esta linha não irá imprimir 10
     }
 }

300 will not be printed. This is a value that does not fit into a variable of this type.

If you look at the Java documentation: link

You will see that byte is an integer type of data, but it is quite limited. It accepts values from -128 to 127. In other words, values outside this margin will be lost.

    
18.01.2016 / 20:21