Data types, variables and constants Flashcards
Data types
• Data that can be handled by a computer can
be of many types such as Character, Numeric,
String etc.
• Java classifies data types as follows:
– Primitive or in-built data type
– Reference data type
Primitive Data Types
Primitive Data Types • Primitive data types (also known as the fundamental data types) are the inbuilt data types offered by Java. • Java provides 8 primitive data types which are categorized as follows: – Integral • byte, short, int, long – Fractional • float, double – Character • char – Boolean • boolean
Sizes of integral data types
byte: 1 byte
short: 2 bytes
int: 4 bytes
long: 8 bytes
Ranges of integral data types
byte: -128 to +127
short: -32768 to +32767
int: -2 billion to +2 billion i.e -2^31 to 2^31 -1
long: -2^63 to +2^63 - 1
Sizes of fractional data types
float: 4 bytes
double: 8 bytes
Ranges of fractional data types
float: -3.4E+38 to +3.4E+38
double: -1.7E + 308 to 1.7E + 308
Precision of fractional data types
float: 6 digit single precision
double: 15 digit double precision
Size and range of char data type
size: 2 bytes
range: 0 to 65535
Size or range of boolean data type
size: 8 bits, uses 1 bit
range: true or false
Reference Types
Inbuilt reference types
– Arrays
– Strings
• User-defined reference types (also known as
user-defined objects or user-defined data
types)
Variables
• A variable is a named storage location in the
computer’s memory.
• It can hold only one value of a particular data
type.
• The value in a variable can be manipulated
during the execution of the program.
Constants
• A constant is a variable whose value cannot
be changed once it has been assigned a
value. If the value is changed subsequently in
the program, the compiler gives an error.
• The keyword final makes a variable as
constant.
Advantages of Constants
• Indicates to the programmer that the value in
that constant (variable) cannot be changed and
hence makes it easier to read and check for
correctness. Even if the programmer accidentally
changes the value of that constant in the code,
the compiler gives an error.
• If the value in the constant needs to be changed,
only the declaration needs to be modified. On
the other hand if a literal is used, one has to
search through the entire program for every
occurrence of that specific value and then
change all the occurrences.
Suffixes
• Suffixes are used to explicitly specify the data type of a literal data value.
• In some situations it is required, because the compiler
gives an error, when the value that is assigned to a
variable is beyond the capacity of the variable on the left hand side.