Professional Documents
Culture Documents
In programming, a data type is a classification that specifies which type of value a variable can
hold, what operations can be performed on that variable, and how the data is stored. It defines a
set of values and the allowable operations on those values. Data types are fundamental to
programming languages and play a crucial role in ensuring that data is stored and processed
appropriately.
Primitive Data Types: Primitive data types are the basic building blocks of data manipulation in
programming languages. They are predefined by the language and represent simple values.
Reference Data Types: Reference data types, also known as non-primitive data types, do not
store the actual data but a reference (memory address) to the location where the data is stored.
They are more complex than primitive data types and are often used to represent objects and
structures. In Java, reference data types include:
// Example of a class
class Car {
String model;
int year;
// Constructor
public Car(String model, int year) {
this.model = model;
this.year = year;
}
// Method
public void startEngine() {
System.out.println("The " + year + " " + model + " is starting the
engine.");
}
}
// Creating an object (instance) of the class
myCar.startEngine();
// Example of an interface
interface Printable {
void print();
}
// Class implementing the interface
class Printer implements Printable {
@Override
public void print() {
System.out.println("Printing a document.");
}
}
// Creating an object of the class implementing the interface
Printable myPrinter = new Printer();
myPrinter.print();
Array Types: Objects that represent arrays.
Arrays are objects in Java that represent a collection of elements of the same
data type. They provide a way to store and manipulate multiple values under a
single name.
// Example of an array
int[] numbers = {1, 2, 3, 4, 5};
// Example of an enumeration
enum Days {
In each example, the creation of an object involves using the new keyword to instantiate
a class, implement an interface, create an array, or use an enumerated constant. Objects
are instances of these types and can be manipulated based on the defined structure and
behavior.
Reference data types allow for more complex data structures and enable the creation of
user-defined types through classes and interfaces. They provide a way to represent and
manipulate data in a more flexible and extensible manner compared to primitive data
types.
Variables: Variables are containers for storing data values. In Java, variables must be declared
with a specific data type before they can be used.
Initialization
Descriptive Names:
// Good example
int studentAge;
Camel Case:
Avoid Keywords:
Types of scope:
Type Casting
Type casting is the conversion of a variable from one data type to another.
Implicit Casting (Widening): Automatically done by the compiler when there is no loss
of data (e.g., int to double).
Explicit Casting (Narrowing): Requires manual intervention and may result in data loss
(e.g., double to int).
Syntax (Java):
These concepts are fundamental to understanding how variables are declared, initialized, named,
scoped, and casted in Java programming. Proper usage and understanding of these concepts
contribute to writing clear, efficient, and error-free code.
1.2. Arrays
Definition and Purpose: An array is a data structure that allows you to store a collection of
elements of the same data type under a single name. The purpose of arrays is to efficiently
manage and manipulate a set of related data items.
Initialization:
Multi-dimensional Arrays:
System.out.println(numbers[i]);
Decision statements, also known as conditional statements, allow a program to make decisions
and choose different paths of execution based on certain conditions.
Example:
if (condition) {
// Code to execute if the condition is true
}
else-if Statement: Provides an alternative condition if the preceding 'if' condition is
false.
Example:
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
}
else Statement: Executes if none of the preceding conditions are true.
Example:
if (condition1) {
// Code to execute if condition1 is true
} else {
// Code to execute if none of the conditions are true
}
Switch Statement: Evaluates an expression against multiple possible case values.
Example:
Repetition statements, or loops, enable the execution of a block of code repeatedly as long as a
specified condition is true.
for Loop: Executes a block of code repeatedly for a specified number of times.
Example:
Example:
while (condition) {
// Code to execute as long as the condition is true
}
Prepared By: MSc. Misgana Belete Wachemo university Java Programming 10
do-while Loop: Similar to 'while' loop, but guarantees at least one execution.
Example:
do {
// Code to execute at least once
} while (condition);
break and continue Statements:
Break: Exits the loop prematurely.
Example: break;
Continue: Skips the rest of the loop's code and proceeds to the next iteration.
Example: continue;
1. Checked Exceptions
Checked at compile-time
Must be handled or declared.
2. Unchecked Exceptions (Runtime Exceptions):
Not checked at compile-time
It can be handled.
3. Exception Hierarchy
All exceptions are subclasses of the `Throwable` class.
Two main categories: `Error` (critical errors) and
`Exception` (recoverable).
try-catch Blocks:
Try: Encloses the code where an exception might occur.
Catch: Catches and handles the exception.
Syntax:
try {
// Code that might throw an exception
} catch (ExceptionType e) {
// Code to handle the exception
}
finally Block:
Executes code regardless of whether an exception occurs or not.
Syntax:
try {
// Code that might throw an exception
} catch (ExceptionType e) {
// Code to handle the exception
} finally {
// Code that always executes
}
Multiple catch Blocks:
Handles different types of exceptions in separate catch blocks.
Syntax:
try {
// Code that might throw an exception
} catch (ExceptionType1 e) {
// Code for ExceptionType1
} catch (ExceptionType2 e) {
// Code for ExceptionType2
}
Syntax: