0% found this document useful (0 votes)
2 views13 pages

Basic Java Review

This document provides a comprehensive review of basic Java programming concepts, covering the differences between machine language, assembly language, and high-level languages, as well as the Java programming environment setup. It discusses key programming principles such as object-oriented programming, encapsulation, inheritance, polymorphism, and abstraction, along with examples and syntax for various Java features. Additionally, it touches on modern development considerations, exception handling, and the use of arrays and collections.

Uploaded by

elhamelhussieny
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)
2 views13 pages

Basic Java Review

This document provides a comprehensive review of basic Java programming concepts, covering the differences between machine language, assembly language, and high-level languages, as well as the Java programming environment setup. It discusses key programming principles such as object-oriented programming, encapsulation, inheritance, polymorphism, and abstraction, along with examples and syntax for various Java features. Additionally, it touches on modern development considerations, exception handling, and the use of arrays and collections.

Uploaded by

elhamelhussieny
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

🚏

Basic Java Review


Strat with the difference between
Machine Language

Assembly Language

High Level Language

Machine Language

It's the lowest level of software programming.

Consists of binary code (0s and 1s) that computers can directly understand.

Directly executable by the computer's CPU.

ASCCI Table [Link]

Example of Machine Language:

10110000 01100001
11000011 10100010

Characteristics:

Very difficult for humans to read and write — Evolution

No need for translation — No Intermediate Channel

Fastest execution

Direct hardware manipulation

10110000 01100001 (Binary instructions)

Assembly Language

Second-generation programming language.

Uses mnemonics (symbolic names) instead of binary.

Requires an assembler to convert to machine code.

Still hardware-specific.

Example of Assembly Language:

MOV AL, 61h ; Move value 61h into AL register


ADD AL, 5 ; Add 5 to AL
MOV [1000h], AL; Store AL at memory location 1000h

Characteristics:

More readable than machine language

One-to-one correspondence with machine instructions

Requires understanding of device architecture

Used in system programming and embedded systems

High-Level Languages More abstract and human-readable languages that are independent of computer hardware.

a) Procedural / Structural Languages: Example in C:

#include <stdio.h>int main() {


int num1 = 5;
int num2 = 10;
int sum = num1 + num2;
printf("Sum: %d", sum);

Basic Java Review 1


return 0;
}

b) Object-Oriented Languages: Example in Java:

public class Calculator {


public static void main(String[] args) {
int num1 = 5;
int num2 = 10;
int sum = num1 + num2;
[Link]("Sum: " + sum);
}
}

Program Translation Process:


Source Code (High-Level Language)


Compiler/Interpreter

Assembly Language

Assembler

Machine Language


Execution
——————————————————————————————————————
More details about Java execution and application compilation process

[Link]

Popular High-Level Languages:


a) Java:

public class Example {


public static void main(String[] args) {
[Link]("Hello, World!");
}
}

b) Python:

print("Hello, World!")

c) JavaScript:
Created by Brandon Ich
Development finished on 14 Day

[Link]("Hello, World!");

Language Paradigms:
a) Procedural/Structural Programming:

Focus on procedures/methods

Sequential execution

Examples: C, FORTRAN

b) Object-Oriented Programming: OOP

Focus on objects and classes

Data encapsulation

Basic Java Review 2


Examples: Java, C++, Python

c) Functional Programming:

Focus on functions and immutability

Examples: Haskell, Scala , java script

Modern Development Considerations:

IDEs (Integrated Development Environments) — > intelJ

Version control systems — Git —- > GitHub

Build tools and package managers.

Testing frameworks.

Documentation tools.

Overview about basic Java programming concepts and topics :


Basics and Environment Setup

Installing JDK (Java Development Kit)

Setting up IDE (Eclipse, IntelliJ, or NetBeans)

Understanding Java Virtual Machine (JVM) — compilation process

Basic program structure Example:

public class HelloWorld {


public static void main(String[] args) {
[Link]("Hello, World!");
}
}

Basic Input/Output

// Reading input
Scanner scanner = new Scanner([Link]);
[Link]("Enter your name: ");
String name = [Link]();
String name = [Link]();
String name = [Link]();

// Printing output
[Link]("Hello, " + name);

Variables and Data Types

what is the Data Types in java ?


what is the data type ranges in java ?
what is the data type size ?
Full information about data types check the following link

[Link]

a) Primitive Data Types: (Value Data type)

int

tiny Int

long

double (decimal numbers)

float

decimal

boolean (true/false)

char (single characters) Example:

Basic Java Review 3


int age = 25;
long age =50;
decimal age =50.00000;
double salary = 50000.50;
boolean isStudent = true;
char grade = 'A';

b) Reference Data Types:

String

Arrays

String name = "John Doe";


int[] numbers = {1, 2, 3, 4, 5};

Operators

Arithmetic (+, -, *, /, %)

Relational (==, !=, >, <, >=, <=)

Logical (&&, ||, !) Example:

int a = 10;
int b = 5;
int sum = a + b; // 15
boolean isGreater = a > b; // true

Control Flow Statements

a) Conditional Statements:

// if-else
if (age >= 18) {
[Link]("Adult");
} else {
[Link]("Minor");
}

// switch
switch(grade) {
case 'A':
[Link]("Excellent");
break;
case 'B':
[Link]("Good");
break;
default:
[Link]("Need improvement");
}

b) Loops:

// for loop
for(int i = 0; i < 5; i++) {
[Link](i);
}

// while loop
int count = 0;
while(count < 5) {
[Link](count);
count++;
}

Basic Java Review 4


// do-while loop
do {
[Link](count);
count++;
} while(count < 5);

//foreach(int x of list of XX)

Methods

Method declaration

Method Definition

Method Calling —add(2,5); -- Method call

Parameters and return types

Method Example:

public void add(int a, int b) {


return a + b;
}

public double add(double a, double b) {


return a + b;
}

private int driveCar(String model){


//Logic
}

Basic OOP Concepts Introduction

Class : template for objects

Encapsulation: Bundling data and methods that operate on that data within a single unit

Inheritance: Creating new classes that are built upon existing classes

Polymorphism: Ability of objects to take on multiple forms

Abstraction: Hiding complex implementation details and showing only necessary features

public string Transfter (int transferto,int transferfrom,double amount)

Class Structure and Encapsulation

// Base class using encapsulation


public class Player {
// Private fields (encapsulation)
private String name;
private int jerseyNumber;
private String position;
protected int speed; // Protected for inheritance

// Constructor

public Player(String name, int jerseyNumber, String position) {


[Link] = name;
[Link] = jerseyNumber;
[Link] = position;
[Link] = 0;
}

// Getters and Setters (encapsulation)


public String getName() {
return name;
}

Basic Java Review 5


public void setName(String name) {
[Link] = name;
}

// Method
public void train() {
[Link](name + " is training");
}
}
--------------------------------------------------------------------------------
Player x; ---> Allocation for memeory
x=new player(name, jerseyNumber, "Goalkeeper"); --> actual creation for object

Encapsulation with Properties:

public class PlayerStats {


private int matches;
private int goals;
private int assists;

// Encapsulated access through methods


public void updateStats(int matches, int goals, int assists) {
if (matches >= 0 && goals >= 0 && assists >= 0) {
[Link] = matches;
[Link] = goals;
[Link] = assists;
}
}

public int getGoalsPerMatch() {


//ternarry operators
return matches > 0 ? (double)goals/matches : 0;
}
}

Access Modifiers Example:

public class FootballClub {


// Public: accessible from anywhere
public String clubName;

// Private: only accessible within this class


private double budget;

// Protected: accessible in same package and subclasses


protected String stadium;

// Default -- public
String league;
}

Inheritance

Java Support multi level inheritance

grand father
father extends grand father
mother extends grand father
son extends father

// Striker inherits from Player


public class Striker extends Player {
private int goalsScored ;

Basic Java Review 6


public Striker(String name, int jerseyNumber) {
super(name, jerseyNumber, "Striker");
[Link] = 0;
}

public void scoreGoal() {


goalsScored++;
[Link](getName() + " scored! Total goals: " + goalsScored);
}
}

// Goalkeeper inherits from Player


public class Goalkeeper extends Player {
private int savesMade;

public Goalkeeper(String name, int jerseyNumber) {


super(name, jerseyNumber, "Goalkeeper");
[Link] = 0;
}

public void makeSave() {


savesMade++;
[Link](getName() + " made a save! Total saves: " + savesMade);
}
}

Abstraction
Abstract class Example

// Abstract class
public abstract class Player {
protected String name;
protected int playerNumber;

// Constructor
public Player(String name, int playerNumber) {
[Link] = name;
[Link]= playerNumber;
}

// Abstract method (must be implemented by subclasses)


public abstract void play();

// Concrete method
public void introduce() {
[Link]("I am " + name + ", wearing number " + playerNumber);
}
}

// Striker class
public class Striker extends Player {
public Striker(String name, int playerNumber) {
super(name, playerNumber);
}

@Override
public void play() {
[Link](name + " is trying to score goals!");
}
}

// Goalkeeper class
public class Goalkeeper extends Player {
public Goalkeeper(String name, int playerNumber) {

Basic Java Review 7


super(name, playerNumber);
}

@Override
public void play() {
[Link](name + " is protecting the goal!");
}
}

public class FootballGame {


public static void main(String[] args) {
// Create players
Striker striker = new Striker("Ronaldo", 7);
Goalkeeper goalkeeper = new Goalkeeper("Buffon", 1);

// Using the methods


[Link]("=== Player Introductions ===");
[Link](); // From abstract class
[Link](); // Implemented method

[Link]("\n=== Second Player ===");


[Link](); // From abstract class
[Link](); // Implemented method
}
}

Interface Example
reason behind why we prefer the interface than abstract to achieve the following concept

SOLID principles —-interface segregation

can implement more than one interface so can be consider solution for multi inheritance

// Interface definition
public interface ITrainable {
void performTraining();
void showProgress();
}

// Class implementing interface


public class FieldPlayer extends Player implements Trainable {
private int trainingHours;

public FieldPlayer(String name, int jerseyNumber, String position) {


super(name, jerseyNumber, position);
[Link] = 0;
}

@Override
public void play() {
[Link](name + " on the sky!");
}

@Override
public void performTraining() {
trainingHours++;
[Link](getName() + " completed training session");
}

@Override
public void showProgress() {
[Link]("Training hours: " + trainingHours);
}
}

Basic Java Review 8


Polymorphism

1. Polymorphism Example:
overloading —- in the same class
overriding —— between the parent class and its child classes

more details about overloading and overriding check the following link

[Link]

public class Team {


private ArrayList<Player> players = new ArrayList<>();

// Polymorphic method
public void addPlayer(Player player) {
[Link](player);
}

// Method showing polymorphism in action


public void teamTraining() {
for(Player player : players) {
[Link](); // Different implementations based on player type
}
}
}

// Usage example
public class FootballGame {
public static void main(String[] args) {
Team team = new Team();

// Polymorphic objects
Player striker = new Striker("Ronaldo", 7);
Player goalkeeper = new Goalkeeper("Buffon", 1);

[Link](striker);
[Link](goalkeeper);

[Link]();
}
}

Usage of OOP concepts


Complete Example

public class FootballMatch {


public static void main(String[] args) {
// Create team
Team homeTeam = new Team();

// Create players
Striker striker = new Striker("Messi", 10);
Goalkeeper keeper = new Goalkeeper("Neuer", 1);
FieldPlayer midfielder = new FieldPlayer("Modric", 8, "Midfielder");

// Add players to team


[Link](striker);
[Link](keeper);
[Link](midfielder);

// Simulate match actions


[Link]();
[Link]();
[Link]();

Basic Java Review 9


// Team training
[Link]();
}
}

This example demonstrates:

Encapsulation through private fields and public methods

Inheritance hierarchy of players

Polymorphism in team management

Abstraction through abstract classes and interfaces

Access control using modifiers

Proper object-oriented design principles

Key Points:

1. Use private fields to enforce encapsulation

2. Create meaningful inheritance hierarchies

3. Implement interfaces for common behaviors

4. Use abstract classes for shared functionality

5. Apply polymorphism for flexible code

6. Choose appropriate access modifiers

7. Follow SOLID principles — Bounce Point

Exception Handling
More details about Exception check the following link

[Link]

plus knowledge —→
[Link]

try {
int result = 10 / 0;
} catch (Exception e) {
[Link]("Cannot divide by zero");
} finally {
[Link]("This always executes");
//most of the time used with GC -- Garbge Collector
}

Arrays and Collections


Basic Syntax and Creation:
reference type —- every new array is an object

// Array Declaration and Initialization


public class ArrayExample {
public static void main(String[] args) {
// Fixed-size array
int[] numberArray = new int[5]; // Empty array
String[] namesArray = {"John", "Mary", "Bob"}; // Array with values

// ArrayList Declaration and Initialization


ArrayList<Integer> numberList = new ArrayList<>(); // Empty ArrayList
ArrayList<String> namesList = new ArrayList<>([Link]("John", "Mary", "Bob"));
}
}

Key Differences with Examples:

Basic Java Review 10


public class ArrayVsArrayList {
public static void main(String[] args) {
// 1. Size
// Array - Fixed size
int[] array = new int[3];
// array = new int[4]; // Need to create new array to change size

// ArrayList - Dynamic size


ArrayList<Integer> arrayList = new ArrayList<>();
[Link](1); // Automatically grows
[Link](2);
[Link](3);
[Link](4); // No size limit

// 2. Type
// Array - Can hold primitives data types and objects
int[] primitiveArray = {1, 2, 3};
String[] objectArray = {"a", "b", "c"};

// ArrayList - Can only hold objects


// ArrayList<int> invalid; // Won't work
ArrayList<Integer> validList = new ArrayList<>();// Must use wrapper classes

// 3. Size/Length
[Link]("Array length: " + [Link]); // Using length
[Link]("ArrayList size: " + [Link]()); // Using size()
}
}

1. Common Operations Comparison:

public class ArrayOperations {


public static void main(String[] args) {
// Array Operations
int[] numbers = new int[3];

// Adding elements
numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;

// Accessing elements
[Link]("Array element: " + numbers[1]);

// Modifying elements
numbers[1] = 20;

// Iterating
[Link]("Array elements:");
for (int number : numbers) {
[Link](number);
}
}
}

public class ArrayListOperations {


public static void main(String[] args) {
// ArrayList Operations
ArrayList<Integer> numbers = new ArrayList<>();

// Adding elements
[Link](1);
[Link](2);

Basic Java Review 11


[Link](3);

// Accessing elements
[Link]("ArrayList element: " + [Link](1));

// Modifying elements
[Link](1, 20);

// Adding/Removing elements
[Link](4); // Add at end
[Link](0, 0); // Add at index
[Link](1); // Remove by index

// Iterating
[Link]("ArrayList elements:");
for (Integer number : numbers) {
[Link](number);
}
}
}

Key Differences Summary:

1. Size:

Array: Fixed size

ArrayList: Dynamic size

2. Type Support:

Array: Primitives and objects

ArrayList: Only objects

3. Syntax:

Array: length property

ArrayList: size() method

4. Functionality:

Array: Basic operations

ArrayList: Rich set of utility methods

5. Performance:

Array: Better for fixed-size operations

ArrayList: Better for dynamic operations

When to Use Which:

Use Array when:

Size is fixed

Primitive types are needed

Performance is critical

Use ArrayList when:

Dynamic size is needed

Frequent insertions/deletions

More utility methods are needed

Working with collections framework

File Handling

Byte Streams
Java byte streams are used to perform input and output of 8-bit bytes. Though there are many classes related to byte streams
but the most frequently used classes are, FileInputStream and FileOutputStream. Following is an example which makes use of

Basic Java Review 12


these two classes to copy an input file into an output file −
More details about FileInputStream check the following link

[Link]

import [Link];
import [Link];
import [Link];

public class CopyFile {

public static void main(String args[]) throws IOException {


FileInputStream in = null;
FileOutputStream out = null;

try {
in = new FileInputStream("[Link]");
out = new FileOutputStream("[Link]");

int c;
while ((c = [Link]()) != -1) {
[Link](c);
}
}finally {
if (in != null) {
[Link]();
}
if (out != null) {
[Link]();
}
}
}
}

To Have more context and knowledge about File I/O on java check the following link

[Link]

Additional Important Topics:


Access modifiers (public, private, protected)

Static keywords and methods

Interfaces and abstract classes

Packages and imports

String manipulation

Remember to:
Follow Java naming conventions —- camel case

Write comments for code documentation

Handle exceptions appropriately

Test your code thoroughly

These concepts form the foundation of Java programming. Master these basics before moving on to advanced topics like
multithreading, networking, and advanced data structures.

Basic Java Review 13

You might also like