Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Java data types

1) Why is the following assignment not allowed:

byte b = 0b11111111; // 8 bits or 1 byte

but this assignment is allowed:

int i = 0b11111111111111111111111111111111; //32 bits or 4 bytes

Both types are signed, and I would expect b and i were -1.

2) Why doesn't the Integer MIN_VALUE have a sign?

public static final int   MIN_VALUE = 0x80000000;

but the Byte MIN_VALUE does have a sign?

public static final byte   MIN_VALUE = -128;
like image 342
rvillablanca Avatar asked Feb 11 '14 21:02

rvillablanca


People also ask

What are the data types in Java explain?

There are two types of data types in Java: Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and double. Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays.

How do I know what data type to use in Java?

For checking variable type in Java, there is a feature called the “instanceOf” operator, which is used to check the type of a variable or object. It gives the boolean value to tell whether the variable belongs to the specified type or not.

What are the 5 basic data types?

Most modern computer languages recognize five basic categories of data types: Integral, Floating Point, Character, Character String, and composite types, with various specific subtypes defined within each broad category.

How do you understand data types?

A data type is an attribute associated with a piece of data that tells a computer system how to interpret its value. Understanding data types ensures that data is collected in the preferred format and the value of each property is as expected.


2 Answers

Question 1)

This is because 0b11111111 is an int literal, whose value is 255. This value doesn't fit into a byte. See http://docs.oracle.com/javase/7/docs/technotes/guides/language/binary-literals.html for more details on this.

Question 2)

When we write binary or hexadecimal literals, we never put a sign. The literal 0x80000000 is actually a negative value, even though we don't write it as such.

There's no really good reason why the makers of the JDK chose to use a decimal literal for -128 but a hexadecimal literal for 0x80000000; except that in each case, it's probably a whole lot clearer that way what is intended.

like image 70
Dawood ibn Kareem Avatar answered Sep 22 '22 11:09

Dawood ibn Kareem


All integer literals have type int (unless suffixed by an L or l). Thus, in the first case, you're storing an int into a byte. A narrowing conversion like this is not allowed without a cast, except that if the right side is a constant, it's allowed if the value is in range, which is -128 to 127. 0b11111111 is 255, though, which is not in range.

As for why int i = 0b11111111111111111111111111111111 is allowed: it's pretty much "because the JLS says so". In fact, that specific example appears in JLS 3.10.1. There's a rule that decimal literals of type int cannot exceed 214743647 (except in the specific case -2147483648), but there's no rule about binary literals except that they have to fit into 32 bits.

As I mentioned in a comment, the second question is really a question about the style preference of the programmers who wrote the code, and it's impossible to answer.

like image 23
ajb Avatar answered Sep 22 '22 11:09

ajb