0% found this document useful (0 votes)
4 views9 pages

OCA Java Exam Mini Course

This mini-course covers essential topics for the OCA Java exam, including Java basics, control flow, object-oriented programming, exceptions, date/time API, and miscellaneous features. It provides detailed explanations, examples, and tables to aid understanding of concepts such as variables, control structures, access modifiers, exception handling, and more. The course aims to help learners master the key concepts necessary for the exam.

Uploaded by

abdo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

OCA Java Exam Mini Course

This mini-course covers essential topics for the OCA Java exam, including Java basics, control flow, object-oriented programming, exceptions, date/time API, and miscellaneous features. It provides detailed explanations, examples, and tables to aid understanding of concepts such as variables, control structures, access modifiers, exception handling, and more. The course aims to help learners master the key concepts necessary for the exam.

Uploaded by

abdo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

OCA Java Exam M.

Mlml
Ml
Lm
mlini-Course
Introduction
This mini-course covers the key topics for the OCA Java exam. Each section provides
detailed explanations, examples, and tables to help you understand and master the
concepts.

1. Java Basics

Variables
Variables are used to store data. They are declared with types such as int, double, boolean,
etc., and initialized with values.

Example:
int x = 10;
double y = 20.5;
boolean isTrue = true;

Casting
Casting is used to convert between types. Implicit casting (widening) happens
automatically, while explicit casting (narrowing) requires a cast operator.

Example:
int x = 10;
double y = x; // Implicit casting
int z = (int) y; // Explicit casting
Class Structure
A Java class includes fields, methods, constructors, and access modifiers. It defines the
blueprint for objects.

Example:
public class MyClass {
private int field;
public MyClass(int field) {
this.field = field;
}
public void method() {
// method body
}
}

Object vs Primitive Variables


Primitive variables store values directly, while object variables store references to the
memory location where the object is stored.

Example:
int x = 10; // Primitive
String str = new String("Hello"); // Object

Object Lifecycle
Objects are created using the new keyword, dereferenced by reassignment, and collected by
the garbage collector when no longer in use.

Example:
MyClass obj = new MyClass(10);
obj = null; // Dereferenced
// Garbage collector will collect obj

Packages
Packages are used to group related classes. Use the import statement to access classes from
other packages.

Example:
import java.util.ArrayList;
public class MyClass {
ArrayList<String> list = new ArrayList<>();
}
2. Control Flow

if/else
Conditional branching using if and else statements.

Example:
if (x > 0) {
// code
} else {
// code
}

Ternary
Short form of if/else using the ternary operator.

Example:
int y = (x > 0) ? 1 : -1;

while
Pre-check loop that executes as long as the condition is true.

Example:
while (x < 10) {
// code
}

do/while
Post-check loop that executes at least once and then as long as the condition is true.

Example:
do {
// code
} while (x < 10);

for
Classic loop that executes a set number of times.

Example:
for (int i = 0; i < 10; i++) {
// code
}

Enhanced for
Loop for iterating over arrays or collections.
Example:
for (int n : array) {
// code
}

break/continue
break exits the loop, continue skips to the next iteration.

Example:
break;
continue;

3. Object-Oriented Programming

Access Modifiers
Access modifiers control the visibility of class members. They include public, private,
protected, and default (package-private).

Example:
public class MyClass {
private int field;
protected void method() {
// code
}
}

Static Keyword
The static keyword indicates that a member belongs to the class, not instances of the class.
It is used for utility methods and fields.

Example:
public class MyClass {
public static int staticField;
public static void staticMethod() {
// code
}
}

Constructors
Constructors are special methods used to initialize objects. Overloading allows multiple
forms of constructors.

Example:
public class MyClass {
public MyClass() {
// default constructor
}
public MyClass(int field) {
this.field = field;
}
}

Default Constructor
A default constructor is provided if no constructor is defined. It is removed if any
constructor is defined.

Example:
public class MyClass {
// default constructor is provided
}
public class MyClass {
public MyClass(int field) {
this.field = field;
}
// default constructor is removed
}

Method Overloading
Method overloading allows multiple methods with the same name but different parameters.

Example:
public class MyClass {
public void method() {
// code
}
public void method(int param) {
// code
}
}

Scope
Scope defines the visibility and lifetime of variables. Local scope is inside a method, instance
scope is fields, and class scope is static fields.

Example:
public class MyClass {
private int instanceField;
public static int classField;
public void method() {
int localVar = 10;
}
}

Abstract Classes/Interfaces
Abstract classes provide partial implementation, while interfaces define a contract with no
implementation (until Java 8+).

Example:
public abstract class MyAbstractClass {
public abstract void abstractMethod();
}
public interface MyInterface {
void method();
}

4. Exceptions

Checked Exceptions
Checked exceptions must be handled or declared. Examples include IOException and
SQLException.

Example:
try {
// code
} catch (IOException e) {
// handle exception
}

Unchecked Exceptions
Unchecked exceptions are runtime errors that are not required to be handled. Examples
include NullPointerException and ArithmeticException.

Example:
int x = 10 / 0; // ArithmeticException

Errors
Errors are serious problems that are not meant to be caught. Examples include
OutOfMemoryError.

Example:
throw new OutOfMemoryError();
Common Exception Classes
Common exception classes include NullPointerException,
ArrayIndexOutOfBoundsException, and ClassCastException.

Example:
String str = null;
str.length(); // NullPointerException

Advantages of Exception Handling


Exception handling separates error-handling code from regular code, improving program
reliability and readability.

Example:
try {
// code
} catch (Exception e) {
// handle exception
}

5. Date/Time API

LocalDate
LocalDate represents a date without time.

Example:
LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.of(1990, 6, 18);

LocalTime
LocalTime represents a time without date.

Example:
LocalTime now = LocalTime.now();
LocalTime noon = LocalTime.of(12, 0);

LocalDateTime
LocalDateTime represents a date and time.

Example:
LocalDateTime now = LocalDateTime.now();
LocalDateTime meeting = LocalDateTime.of(2023, 6, 18, 14, 30);

DateTimeFormatter
DateTimeFormatter is used to format and parse date-time objects.
Example:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
String formattedDate = now.format(formatter);

Period
Period represents the difference between dates.

Example:
Period age = Period.between(birthday, today);

6. Miscellaneous

Java Features
Java features include platform independence (JVM), object-oriented programming,
encapsulation, etc.

Example:
Java code runs on any platform with a JVM.
Java supports encapsulation through access modifiers.

Main Method
The main method is the entry point of a Java application.

Example:
public static void main(String[] args) {
System.out.println("Hello, world!");
}

Console Output
Console output is done using System.out.println().

Example:
System.out.println("Hello, world!");

Strings
Strings are immutable. Use .equals() for comparison.

Example:
String str1 = "Hello";
String str2 = "Hello";
boolean isEqual = str1.equals(str2);

Casting
Casting is needed when narrowing types (e.g., int to byte).
Example:
int x = 10;
byte y = (byte) x;

Equality
Use == to compare references, .equals() to compare values.

Example:
String str1 = "Hello";
String str2 = new String("Hello");
boolean isEqual = str1.equals(str2);

Operators
Use parentheses to control operator precedence.

Example:
int x = (10 + 2) * 3;

Field Access
Use dot notation to access fields and methods of an object.

Example:
MyClass obj = new MyClass();
obj.field = 10;
obj.method();

You might also like