Values and Data types-SM - 2
Values and Data types-SM - 2
Data types:
Data types are used to identify the type of value that will be stored in an identifier.
Data types in Java:
1. Primitive data types: These are the fundamental built in data types that are independent of any other data
type.
Example: int , char , double , boolean etc.
2. Non-primitive: These are data types are derived data types which are dependent on primitive data types.
Example: class , array , String etc.
Type conversion
Implicit Explicit
Implicit conversion /Coercion /Widening: is done automatically by the compiler when the data types are
compatible with each other and the smaller data type is promoted into higher data type. This is called as Type
promotion. But no widening conversion is supported from numeric type to char or boolean because the
numeric type is incompatible with char or boolean. Also char and boolean are incompatible with each other.
The following conversion takes place implicitly:
byte ->short -> int ->long ->float ->double
char ->int ->long ->float ->double
Example: char c=’A’;
int x= c;
Explicit conversion/Type casting: is done forcefully by the user using (type) operator where the higher data
type is converted to smaller data type.
General form : (typename)value
SAMPLE QUESTIONS: