JAVA
DATA TYPES
DEFINITION OF A DATA TYPE
• A data type in java represents the size and different values that can be
stored in a variable.
CLASSIFICATION OF DATA TYPES
• Data types are classified into two;
-Primitive data types
-Non-primitive data types
PRIMITIVE DATA TYPES
• Primitive data types are a set of basic data types from which all other
data types are constructed.
• A primitive data type specifies the size and type of variable values, and
it has no additional methods.
• byte
DATA TYPE SIZE DESCRIPTION
• Stores whole numbers from -128 to
1 byte
127
• Stores whole numbers from -32,768 to
• short 2 bytes
32,767
• Stores whole numbers from -
• int 4 bytes 2,147,483,648 to 2,147,483,64
• Stores whole numbers from
• long 8 bytes 9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
• Stores fractional numbers. Sufficient
• float 4 bytes
DATA TYPE
SIZE DESCRIPTION
• double 8 bytes • Stores fractional numbers.
Sufficient for storing 15 decimal
digits
• boolean 1 bit
• Stores true or false values
• char 2 bytes
• Stores a single character
• The char [characters] are enclosed using single quotes while strings are
enclosed using double qutations
Example
• char myGrade = ‘B’
• String greeting =“Hello World”
NON-PRIMITIVE DATA TYPES
• Non-primitive data types are called reference types because they
refer to objects
• Examples of non-primitive data types include Strings, Arrays,
Classes and Interface
STRINGS
• Are used for storing texts
• A String variable contains a collection of characters surrounded by
double quotes
ARRAYS
• Arrays are used to store multiple values in a single variable , instead of
declaring separate variables for each value.
• To declare an array , define the variable type with square brackets
Example
Array of strings
String [ ] cars;
String [ ] cars ={ “Volvo”, “BMW”, “Mazda” “Ford”} ;
Array of integers
Int [ ] myNum = { 10, 20 ,30 }
CLASSES
• A class is like an object constructor , or a “blueprint” for creating
objects
• For example a car is an object , the car has attributes such as weight
and color
DIFFERENCES BETWEEN PRIMITIVE AND NON-
PRIMITIVE DATA TYPES
PRIMITIVE DATA TYPE NON-PRIMITIVE DATA TYPE
• Are predefined [already defined] in java • Are created by the programmer and is not
• Cannot be used to call methods to defined by java [except for String]
perform certain operations
• Can be used to call methods to perform
• A primitive type always has a value
certain operations
• Non primitive types can be null
• A primitive type starts with a lower
case letter
• Non primitive type starts with an upper
case letter