Data Type and Type Casting

Literals

int age = 17;

The Programming elements like '17' of the above code are called literals. A literal is the source code representation of a fixed value.

In most cases, JVM generates a value of the same type as a literal's type -- A few exceptions are covered later. --

Variables

int age = 17;

The programming elements like 'age' of the above code are variables. A variable is a storage location with a name to save a value. The size of storage capacity varies depending on data types.

Data Types

int age = 17;

The 'int' is the data type of the variable named age. In Java, both literals and variables are available after JVM determines them to a specific data type.

Java divides data types into two types.

  • Primitive Data Types
  • Reference Data Types

Primitive Data Types

Primitive Data Types are boolean, char, byte, short, int, long, float, and double.

boolean

The boolean data type has only two possible values: true and false.

boolean signIn = true;

char

The char data type is a single 16-bit Unicode character.
Always use single quotes for char literal.
The following example expresses characters, Unicode characters, and control characters using single quotes.

char grade = 'A';
char ch = '\uFFFF';//'\uFFFF' is the character correspond to FFFF in UTF-16
char ech = '\n';//Line feed
char ech = '\b';//Backspace
char ech = '\t';//Tab
char ech = '\\';//Backslash
char ech = '\"';//Double quote
char ech = '\'';//Single quote

byte

The byte data type is an 8-bit integer.

byte weight = 48;

short

The short data type is a 16-bit integer.

short balance = 5000;

int

The int data type is a 32-bit integer.

int balance = 5000;

long

The long data type is a 64-bit integer. Numbers with an L at the end are long-type literals.

long worldPopulation = 7786000000L;

float

The float data type is a 32-bit floating point. Numbers with f at the end are float-type literals.

float fineGold = 999.9f;

double

The double data type is a 64-bit floating point.

double fineGold = 999.9;

There are a few exceptions that JVM does not create a value of the same type as a literal's type.

The following is an explanation of how the data type of the value is determined.

int i1 = 3;//integral number without any additional character is an int literal.
long l1 = 4;//4 is an int literal.

In most cases, JVM generates a value of the same type as a literal's type.
However, an int literal can be a byte or short value.
Consider the following example.

byte b = 1; //1 is an int literal, but it will be a byte value.
short s = 2; //2 is an int literal, but it will be a short value.

Java requires these rules because there are no special literals for bytes and shorts.

Not all literals produce values.

int i2 = 3000000000; //compile error!

3000000000 (3 billion) is an int literal, but out of the int range.

As a result, JVM cannot generate an int value, and a compilation error occurs.

byte	8 bits	-128 ~ 127
short	16 bits	-32,768 ~ 32,767
int	32 bits	-2,147,483,648 ~ 2,147,483,647
long	64 bits	-9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807

An integer number followed by an L is a long literal.

long l2 = 3000000000L; //3000000000L is a long literal.

Numbers with a decimal point or numbers followed by a D or d are double-type literal.

double d1 = 3.14;  //3.14 is a double literal.
double d2 = 3.14D; //3.14D is a double literal.
double d3 = 3.14d; //3.14d is a double literal.

Numbers followed by f are float-type literal.

float f = 3.14f; //3.14f is a float literal.

Reference Data Types

In a Java variable declaration, you must put the data type before the variable.

Student john = new Student();

'Student' is the data type of the variable named john. Like 'Student,' all Java classes are reference data types.

After 'new Student()' is finished, JVM assigns the reference to created student object to the variable john. A reference to an object is the address of the object in memory. It's easy to understand if you think of a reference to an object as the address of an object in memory. You can use the variable john and dot (.) to access the student object referenced by john as shown below:

john.name = "John Adams";

Type Casting

Type Casting means converting the value data type to the desired data type.

Java divides Type Casting into two types. One is Up-Casting; it happens automatically by the JVM. The Other is Down-Casting; if you want it happens, put '(desired data type)' before literals or variables.

Up-Casting

long money = 300;//300 is int literal and value of 300 is created as int type.

Java is strict about data types. The variable named money needs a long type value. Before the assignment, JVM converted 300 of int type to 300 of long type. Automatic Type-Casting happens to a broad data type direction from a narrow data type.

byte --> short --> int --> long --> float --> double

It is called Up-Casting.

There are exceptions here too.
The float type takes up 4 bytes of memory size, and the long type takes up 8 bytes of memory size, but Up-Casting takes place from the long to the float direction.

float x = 10L;//JVM convert 10 of long type to 10 of float type automatically.

Up-Casting in arithmetic operations

int x = 3 + 3.5 + 4; //compile error!

JVM creates a value of 3.5 as a double type and values of 3 and 4 as int type. In the arithmetic in which int type and broader type than int participate, all operands are cast into the broadest type, as shown below:

int x = 3.0 + 3.5 + 4.0;

The result of 3.0 + 3.5 + 4.0 is double and cannot be assigned to an int type variable x, resulting in a compile error.

Down-Casting

float f = 1.1; //compile error!

The above code occurs a compile error because you cannot assign double to float. Type Casting from broad type to narrow type does not happen automatically.

float f = (float) 1.1;

The above code converts the double type value to the float type value. Forcing data types to be changed using parentheses is called Down-Casting. Let's assume the value assigned to b in the following.

byte b = (byte) 258;

The variable b is assigned 2. The first 3 bytes of the int's 4 bytes are lost when casting an int to a byte.

JVM casts all values to an int type at arithmetic operations involving byte or short values before the operation proceeds.

short s1 = 1;
short s2 = 2;
short sum = s1 + s2; //compile error!

The cause of the above code causing a compilation error is that JVM casts both s1 and s2, which are operands, to int type, and the arithmetic operation outputs an int type result. -- The short type variable sum cannot contain an int value--

To avoid compilation errors. Cast as shown below:

short sum = (short) (s1 + s2);

What is the value assigned to variable z?

int x = 10;
int y = 4;
int z = x / y;

The variable z is assigned 2, not 2.5.
Since x and y are both int types, the operation result is also an int type.
If you want z to be assigned 2.5, you must change the type of the variable z to double and cast either x or y to a double.

double z = (double) x / y; 

If you cast x to a double type, JVM automatically cast y to a double type.

References