ACHIEVERS UNIVERSITY, OWO
COLLEGE OF NATURAL AND APPLIED SCIENCES
DEPARTMENT OF COMPUTER SCIENCE
COS 201 – COMPUTER PROGRAMMING I – 3 UNITS
LECTURER IN CHARGE - MR. ADEPOJU, S. E.
LECTURE 5
JAVA DATA TYPES
Introduction
A variable in Java must be a specified data type. Variables are containers for storing data values.
Data types are divided into two groups:
● Primitive data types - includes byte, short, int, long, float, double, boolean and char
● Non-primitive data types - such as String, Arrays and Classes
Primitive Data Types
A primitive data type specifies the type of a variable and the kind of values it can hold. There are
eight primitive data types in Java:
● byte - stores whole numbers from -128 to 127
● short - stores whole numbers from -32,768 to 32,767
● int - stores integers (whole numbers), without decimals, from -2,147,483,648 to
2,147,483,647
● long - stores whole numbers from -9,223,372,036,854,775,808 to
nOfBits nOfBits - 1
9,223,372,036,854,775,807 [-2 , (2 ) - 1]
● float - stores floating point numbers (with decimals), sufficient for storing 6 to 7 decimal
digits
● double - stores fractional numbers, sufficient for storing 15 to 16 decimal digits
● boolean - stores values with two states: true or false
● char - stores a single character/letter or ASCII values, such as 'a' or 'B'. Char values are
surrounded by single quotes
Example 1
1 byte myPetite = 3; // Whole number within the byte range
2 short aShortNum = 17; // Whole number within the short range
3 int myNumber = 29; // Integer (whole number)
4 long soTall = 584L; // Long (highest form of integer)
5 float myFloatNum = 5.99f; // Floating point number
6 double longDecimal = 0.95732d; // Double (highest decimal digits)
7 boolean myBool = true; // Boolean
8 char favoriteLetter = 'S'; // Character
Non-Primitive Data Types
Non-primitive data types are called reference types because they refer to objects.
● String - stores text, such as "Hello". String values are surrounded by double quotes
Example
1 String myText = "Hello"; // String
Primitive vs Non-primitive data types
The main differences between primitive and non-primitive data types are:
● Primitive types in Java are predefined and built into the language, while non-primitive
types are created by the programmer (except for String).
● Primitive types start with a lowercase letter (like int), while non-primitive types typically
starts with an uppercase letter (like String).
● Primitive types always hold a value, whereas non-primitive types can be null.
● Non-primitive types can be used to call methods to perform certain operations, whereas
primitive types cannot.
TYPE CONVERSION
byte -> short -> int -> long
● A long can store an int, a short and a byte
● An int can store a short and a byte but it cannot store a long
● A short can store a byte but it cannot store an int or a long
● A byte can only store a byte
Example 2
1 byte b = 8;
2 short s = 19;
3 int i = 25;
4 long l = 103L;
5
6 l = b + s + i; // Ok, long = int
7 i = s + b; // Ok, int = short
8 s = b; // Ok, short = byte
9
10 i = l; // Not Ok, int = long
11 s = i; // Not Ok, short = int
12 b = s; // Not Ok, byte = short
TYPE CASTING
Type casting is when you assign a value of one primitive data type to another type. In Java, there
are two types of casting:
● Widening Type Casting (automatically) - Widening type casting is done automatically
when passing a smaller size type to a larger size type.
byte -> short -> char -> int -> long -> float -> double
Example 3(a)
Main.java
1 public class Main {
2 public static void main(String[] args) {
3 int myAge = 9;
4 double myDouble = myAge; // Automatic casting: int to double
5
6 System.out.println(myAge); // Outputs 9
7 System.out.println(myDouble); // Outputs 9.0
8 }
9 }
● Narrowing Type Casting (manually) - Narrowing type casting must be done manually
converting a larger type to a smaller size type, by placing the type in parentheses ( ) in
front of the value.
double -> float -> long -> int -> char -> short -> byte
Example 3(b)
Main.java
1 public class Main {
2 public static void main(String[] args) {
3 double myHeight = 5.78d;
4 int myHeightRange = (int) myHeight; // Manual casting: double to int
5
6 System.out.println(myHeight); // Outputs 4.78
7 System.out.println(myHeightRange); // Outputs 4
8 }
9 }
Example 3(c)
Here's a real-life example of type casting where we create a program to calculate the percentage
of students that passed in relation to the total number in class.
We use type casting to make sure that the result is a floating-point value, rather than an integer:
Main.java
1 public class Main {
2 public static void main(String[] args) {
3 int totalStudents = 72; // Total number of students in the class
4 int passedStudents = 58; // number of students that passed
5
6 /* In order to calculate the percentage of students that passed in the class,
7 convert passedStudents to float to make sure that the division is accurate */
8
9 float percentagePassed = (float) passedStudents / totalStudents * 100.0f;
10
11 System.out.println("Percentage of Passed Students is " + percentagePassed);
12 }
13 }
// Output: 80.55556
ASSIGNMENT
1) What is the syntax for declaring and defining a variable in Java?
2) List the primitive data types in Java. Using your own defined variable name in each of the
primitive data types, write a simple Java program that displays the value each variable has
been assigned.
3) A variable said to be declared final means what? Discuss using an example.
(Hint: final int age = 17; )