0% found this document useful (0 votes)
21 views10 pages

Java Programming Basics Guide

The document provides an introduction to Java, highlighting its key features such as platform independence, object-oriented design, security, and multi-threading. It covers Java architecture components including JVM, JRE, and JDK, as well as basic syntax, data types, control flow statements, and object-oriented programming principles. Additionally, it discusses exception handling and the Collections Framework for managing groups of objects.

Uploaded by

naazkakria1001
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)
21 views10 pages

Java Programming Basics Guide

The document provides an introduction to Java, highlighting its key features such as platform independence, object-oriented design, security, and multi-threading. It covers Java architecture components including JVM, JRE, and JDK, as well as basic syntax, data types, control flow statements, and object-oriented programming principles. Additionally, it discusses exception handling and the Collections Framework for managing groups of objects.

Uploaded by

naazkakria1001
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

Page 1: Introduction to Java

Java is a high-level, class-based, object-


oriented programming language designed
to have as few implementation
dependencies as possible.
Key Features
• Platform Independent: Compiled
Java code (Bytecode) runs on any
machine with a Java Virtual Machine
(JVM).
• Object-Oriented: Everything in Java
is an Object, which allows for modular
and reusable code.
• Secure: Java runs inside a "sandbox"
to prevent unauthorized access to
system resources.
• Multi-threaded: Allows programs to
perform multiple tasks simultaneously.
The "WORA" Principle
Java follows the "Write Once, Run
Anywhere" philosophy. You write your
code once, and it can run on Windows,
Mac, Linux, or mobile devices without
modification.

Page 2: Java Architecture (JDK, JRE,


JVM)
Understanding how Java runs is crucial
for any developer.
Components Breakdown
1. JVM (Java Virtual Machine): The
engine that executes Java Bytecode. It
is platform-dependent.
2. JRE (Java Runtime
Environment): Includes the JVM plus
the standard libraries required to run
Java applications.
3. JDK (Java Development Kit): The
full toolkit for developing Java apps. It
includes the JRE and tools like the
compiler (javac) and debugger.
Execution Flow:
Source Code (.java) → Compiler →
Bytecode (.class) → JVM → Machine
Code

Page 3: Basic Syntax & Your First


Program
Every Java application must have at least
one class and a main method.
The Hello World Program
Java
public class Main {
public static void main(String[] args) {
[Link]("Hello, Java!");
}
}
Key Syntax Rules
• Case Sensitivity: Main and main are
different.
• File Name: Must match the public
class name (e.g., [Link]).
• Main Method: The entry point of any
Java program.
• Semicolons: Every statement must
end with a ;.

Page 4: Data Types and Variables


Java is a statically-typed language,
meaning all variables must be declared
before use.
Primitive Data Types
Type Size Description
4
int Stores whole numbers.
bytes
8 Stores fractional
double
bytes numbers.
2 Stores a single
char
bytes character/ASCII.
boolean 1 bit Stores true or false.
Non-Primitive Types
• String: A sequence of characters
(e.g., "Hello").
• Arrays: A collection of similar types.
• Classes: User-defined types.
Page 5: Control Flow Statements
Control flow determines the order in which
code is executed based on conditions.
1. Decision Making
• if-else: Executes a block if a condition
is true.
• switch: Selects one of many code
blocks to be executed.
2. Looping
• for loop: Used when the number of
iterations is known.
• while loop: Repeats as long as a
condition is true.
• do-while: Similar to while, but
executes the block at least once.
Java
for(int i=0; i<5; i++) {
[Link](i);
}
Page 6: Object-Oriented Programming
(OOP) - Part 1
OOP is the heart of Java. It mimics real-
world entities using Classes and Objects.
Key Concepts
• Class: A blueprint or template for
creating objects (e.g., a "Car"
blueprint).
• Object: An instance of a class (e.g., a
specific "Red Tesla").
• Constructor: A special method used
to initialize objects.
Java
class Car {
String model;
Car(String m) { // Constructor
model = m;
}
}

Page 7: OOP Part 2 - Inheritance &


Polymorphism
These concepts allow for code reusability
and flexibility.
Inheritance
A class (subclass) inherits attributes and
methods from another class (superclass)
using the extends keyword.
Polymorphism
The ability of an object to take on many
forms.
1. Method Overloading: Multiple
methods with the same name but
different parameters (Compile-time).
2. Method Overriding: A subclass
provides a specific implementation of a
method already defined in its
superclass (Runtime).

Page 8: OOP Part 3 - Abstraction &


Encapsulation
These principles focus on security and
complexity management.
Encapsulation
Wrapping data (variables) and code
(methods) together as a single unit. Data
is hidden using private modifiers and
accessed via Getters and Setters.
Abstraction
Hiding internal details and showing only
functionality.
• Abstract Class: Cannot be
instantiated; can have abstract
methods.
• Interface: A "contract" that classes
must follow. Uses the implements
keyword.

Page 9: Exception Handling


Exceptions are events that disrupt the
normal flow of the program.
The Try-Catch Block
To prevent a program from crashing, we
use exception handling:
• try: Code that might throw an error.
• catch: Code that handles the error.
• finally: Code that always executes
(e.g., closing a file).
• throw/throws: Used to manually
signal an error.

Page 10: The Collections Framework


The Collections Framework provides an
architecture to store and manipulate
groups of objects.
Common Interfaces & Classes
1. List: An ordered collection that
allows duplicates (e.g., ArrayList,
LinkedList).
2. Set: A collection that does NOT
allow duplicates (e.g., HashSet).
3. Map: Stores data in Key-Value
pairs (e.g., HashMap).
Example: ArrayList
Java
import [Link];
ArrayList<String> fruits = new
ArrayList<>();
[Link]("Apple");
[Link]("Banana");
[Link](fruits);

You might also like