Data Type and Type Casting

Literal and Variable

int year = 2019;

Programming elements like 2019 called Literal. 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 is covered later.--

The programming elements as the year called Variable. A variable is a storage location with a name in order to save the value. The size of storage capacity varies depending on the Data Type.

Data Type

Data type in Java is divided into two types.

  • Primitive Data Types
  • Reference Data Types

Primitive Data Types are a Data Type for normal data. Reference Data Types are a Data Type for reference data.

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 marks ('').

char grade = 'A';
char ch = '\uFFFF';
('\uFFFF' is the character correspond to FFFF (Hex) in UTF-16)
char ech = '\n';//line feed
char ech = '\b';//backspace
char ech = '\t';//tab
char ech = '\\';//backspace
char ech = '\"';//double quote
char ech = '\'';//single quote

byte

The byte data type is an 8-bit integer.

byte weight = 71;

short

The short data type is a 16-bit integer.

short bill = 30000;

int

The int data type is a 32-bit integer.

int balance = 56219618;

long

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

long balance = 56219618L;

float

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

long rate = 6.195f;

double

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

long rate = 6.195;

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-form value. Consider the following example.

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

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

Not all literals are a value.

int i2 = 3000000000; //compile error

3000000000 (3 billion) is an int literal, but out of the int range. 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.
float f1 = 3.14;   //compile error. A double type value can not be assigned to a float type variable.

Numbers followed by f are float literal.

float f2 = 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 and Student is not primitive data type. therefore, Student is a reference data type. When new Student() is executed, the reference to created student object is assigned to the varible john. A reference to an object is the address of the object in memory. Now, if you use the varibale john and .(dot), you can access to the student object which john is referring to as 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 on the data type applies. So, variable money shall be assigned a value of long type. 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)
So, it is called Up-Casting. Even so float occupies 4 bytes of memory size and long occupies 8 bytes of memory size, but long are automatically cast to float.

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

Up-Casting in arithmetic

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 converted to the largest data type of values as below;

int x = 3.0 + 3.5 + 4.0;

The result of 3.0 + 3.5 + 4.0 is dobule and can not 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 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;

2 is assigned to b. When int converts to byte, front 3 bytes of 4 bytes is 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 an 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);

다음 예에서 z에 할당되는 값을 예상해 보자.

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

We expect 2.5, but z is assigned 2. Since x and y are both int types, the operation result is also of type int. If you want z to be assigned a value of 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