Data Types available in Java :
The main purpose of data types are that, what kind of value we can stored into the variable. Java data types are classified into three categories :
- Fundamental / Predefined / Primary / Primitive / Core / Scalar / Built-in data types
- Derived / Reference data types
- User defined / Secondary / Custom / Programmer defined data types
Fundamental data types :-
Fundamental data types are those data types which are developed by language developer and can store single value. The fundamental data types are :
Boolean data type :
To represent logical values.
- boolean - Size - Not applicable (Virtual Machine dependent) (either true or false)
Character data type :
To represent character values.
- char - 2 bytes Unicode UTF-16 character (unsigned)
Numeric data type :
To represent number values :-
Integer data type :
To represent integral values.
- byte - 1 byte (signed)
- short - 2 bytes (signed)
- int - 4 bytes (signed)
- long - 8 bytes (signed)
Floating point data type :
To represent real values.
- float - 4 bytes (IEEE 754)
- double - 8 bytes (IEEE 754)
Derived data types :-
Derived data types are those data types which can store multiple values of same type. It includes Arrays.
User defined data types :-
User defined data types are those data types whose variables allows us to store multiple values either of same type or different types. The user defined data types are :
Type Conversion :-
The process of converting one predefined type into another is called type conversion. There are two types of type conversion :
- Implicit type conversion / Implicit type casting
- Explicit type conversion / Explicit type casting
Implicit type conversion :-
It is performed by the compiler automatically. Java compiler converts all operands upto the type of largest operand. It is also called type promotion. This type casting is required whenever we are assigning smaller data type value into bigger data type variable. Hence, it is also known as widening or up-casting. There is no loss of information in this type conversion.
double d = 10; // Compiler automatically converts integer value into double System.out.println(d); // Output : 10.0 int i = 'a'; // Compiler automatically converts character value into int System.out.println(i); // Output : 97
Explicit type conversion :-
Programmer is responsible to perform this type conversion. This type casting is required whenever we are assigning bigger data type value into smaller data type variable. Hence, it is also known as narrowing or down-casting. There may be loss of information in this type conversion.
byte b = 130; System.out.println(b); /* Compile time error : possible loss of precision required : byte found : int byte b = (byte) 130; System.out.println(b); // Output : -126 /* 130 => 000000.....010000010 (binary form having 32 bits i.e 4 bytes) 130 => 10000010 (binary form having 8 bits i.e 1 byte) Most significant bit represent sign bit (1 => negative (-) and 0 => positive (+)). Positive number will be represented directly in memory whereas negative number will be represented in 2's complement : 10000010 (binary form having 8 bits i.e 1 byte) 01111101 (1's complement) +1 (Adding 1 to 1's complement => 2's complement) 11111110 (2's complement : 126 in decimal form represented as -126 for negative value) */
MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic.