Topic 3 - Java Data Types and Variables
Topic 3 - Java Data Types and Variables
This data type defines the operations that can be done on the data,
the meaning of the data, and the way values of that type can be
stored. A data type provides a set of values from which an expression
(i.e. variable, function, etc.) may take its values.
JAVA – DATA TYPES
A. Byte
⮚ The byte data type can store whole numbers from -128 to
127 (8-bit signed two's complement integer).
⮚ If it's certain that the value of a variable will be within -128
to 127, then it is used instead of int to save memory.
⮚ Default size of this data type: 1 byte.
⮚ Default value: 0
PRIMITIVE DATA TYPES
B. Short
⮚ The short data type can store whole numbers from -32768 to
32767 (16-bit signed two's complement integer).
⮚ Short datatype can also be used to save memory as byte data
type. A short is 2 times smaller than an integer
⮚ Default size of this data type: 2 bytes
⮚ Default value: 0
⮚ The byte data type couldn’t hold the value 150 but a short
data type can because it has a wider range.
PRIMITIVE DATA TYPES
C. Int
⮚ The int data type can store whole numbers from
-2147483648 to 2147483647 (32-bit signed two's complement
integer).
⮚ In general, the int data type is the preferred data type when
we create variables with a numeric value unless there is a
concern about memory..
⮚ Default size: 4 byte
⮚ Default value: 0
PRIMITIVE DATA TYPES
D. Long
⮚ The long data type can store whole numbers from
-9223372036854775808 to 9223372036854775807 (64-bit
signed two's complement integer).
⮚ This is used when int is not large enough to store the value.
⮚ Note that you should end the value with an "L".
⮚ Size: 8 bytes
⮚ Default value: 0
PRIMITIVE DATA TYPES
E. Float
⮚ The float data type is a single-precision 32-bit floating-point.
⮚ Sufficient for holding 6 to 7 decimal digits
⮚ Use a float (instead of double) if you need to save memory in
large arrays of floating-point numbers.
⮚ Size: 4 bytes.
⮚ Default Value: 0.0
⮚ Note that you should end the value with an "f".
⮚ It should never be used for precise values such as currency.
PRIMITIVE DATA TYPES
F. Double
⮚ The double data type is a double-precision 64-bit floating-
point.
⮚ The double data type can store fractional numbers from
1.7e−308 to 1.7e+308.
⮚ Sufficient for holding 16 decimal digits.
⮚ Size: 8 bytes
⮚ Default value: 0.0 (0.0d)
⮚ Note that you should end the value with a "d".
PRIMITIVE DATA TYPES
G. Char
⮚ The char datatype is a single 16-bit Unicode character
⮚ The char data type is used to store a single character.
⮚ The character must be surrounded by single quotes, like 'A'
or 'c’.
⮚ Size: 2 bytes
PRIMITIVE DATA TYPES
H. Boolean
⮚ A boolean data type is declared with the boolean keyword
and can only take the values true or false.
⮚ Default value: false.
⮚ They are usually used for true/false conditions.
NON-PRIMITIVE DATA TYPES
Non-primitive data types are called reference types because they refer to
objects.
The main difference between primitive and non-primitive data types are:
⮚ Primitive types are predefined (already defined) in Java. Non-primitive
types are created by the programmer and is not defined by Java (except for
String).
⮚ Non-primitive types can be used to call methods to perform certain
operations, while primitive types cannot.
⮚ A primitive type has always a value, while non-primitive types can be null.
⮚ A primitive type starts with a lowercase letter, while non-primitive types
starts with an uppercase letter.
⮚ The size of a primitive type depends on the data type, while non-primitive
types have all the same size.
⮚ Examples of non-primitive types are Strings, Arrays, Classes, Interface, etc.
NON-PRIMITIVE DATA TYPES
Strings
⮚ Strings are defined as an array of characters. The difference between a
character array and a string in Java is, the string is designed to hold a
sequence of characters in a single variable whereas, a character array
is a collection of separate char type entities.
⮚ The String data type is used to store a sequence of characters (text).
String values must be surrounded by double quotes.
⮚ A String in Java is actually a non-primitive data type, because it refers
to an object. The String object has methods that are used to perform
certain operations on strings.
VARIABLES