Data Type and Type Casting

Literals

int age = 17;

The Programming elements like 17 are called literals.
A literal is the source code representation of a fixed value.
Mostly, a value is generated with the same type as a literal.
There are a few examples in Java that do not produce values of the same type as literals.
This will be covered soon.

Variables

int age = 17;

The programming elements like age are called Variables.
A variable is a storage location with a name to save the value.
The size of storage capacity varies depending on the data type.

Data Types

int age = 17;

int is the data type of the variable age. In Java, both literals and variables are used after being determined to a specific data type.

Data types in Java are divided 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 literals. The following example expresses characters, unicode characters, and control characters using single quotation.

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.

long fineGold = 999.9f;

double

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

long fineGold = 999.9;

The rule that determines the data type of the value

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 a int literal.
long l1 = 4;//4 is an int literal.

Mostly, values of the same type as the literal type are created. 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.

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

As a result, an int value is not created and a compilation error occurs.

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

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

Numbers with decimal point or numbers followed by an D or d are double 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 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 john. Like Student, all Java classes are reference data types.

When new Student() is executed, the reference to created student object is assigned 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 to the student object referenced by john as shown below:

john.name = "John Adams";

Type Casting

Type Casting means that converting the data type of value to desired data type. Type Casting is divided into two types. One is called Up-Casting, it happens automatically by the JVM. The Other is called 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 money must be assigned long type value. In this case, JVM converted 300 of int type to 300 of long type before the assignment. Automatic type casting happens to a large datatypes direction from a small data type.
byte --> short --> int --> long --> float --> double)
It is called Up-Casting.

float takes up 4 bytes of memory size and long takes up 8 bytes of memory size, but Up-Casting takes place from long to 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!

Value of 3.5 is created as a double type and value of 3 and 4 are created as int type. In the arithmetic which int type and larger type than int participates in, every value will convert to the largest data type of values as shown below;

int x = 3.0 + 3.5 + 4.0;

The result of 3.0 + 3.5 + 4.0 is dobule 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 large type to small type does not happen automatically.

float f = (float) 1.1;

The above code converts value of the double type to value of float type. 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;

b is assigned 2. When int cast to byte, the first 3 bytes of the int's 4 bytes are lost.

For arithmetic operations involving byte or short values, all values are cast to int type before the operation proceeds.

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

When s1 and s2 participate in the operation, they are all cast as an int type, so the result is an int type value. If you want to avoid compilation errors. cast as shown below:

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

What is value assigned to variable z?

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

z is assigned 2, not 2.5. Since x and y are both int types, the operation result is also of 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, y is automatically cast to the double.

References