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

CORE JAVA MATERIALdthsedhserkrsrtsrgfnggr

The document provides an overview of core Java concepts, focusing on Object-Oriented Programming (OOP) principles such as encapsulation, inheritance, polymorphism, and abstraction. It also covers Java's String handling, exception handling mechanisms, looping statements, and the use of static and final modifiers. Each concept is illustrated with code examples to enhance understanding.
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)
11 views10 pages

CORE JAVA MATERIALdthsedhserkrsrtsrgfnggr

The document provides an overview of core Java concepts, focusing on Object-Oriented Programming (OOP) principles such as encapsulation, inheritance, polymorphism, and abstraction. It also covers Java's String handling, exception handling mechanisms, looping statements, and the use of static and final modifiers. Each concept is illustrated with code examples to enhance understanding.
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/ 10

CORE JAVA MATERIAL

OOPs (Object-Oriented Programming System) is a programming paradigm that organizes


software design around objects, rather than functions and logic. OOP helps in organizing
complex programs, improving reusability, scalability, and maintainability.
Four Core OOPs Concepts

1. Encapsulation
 Definition: Wrapping data (variables) and code (methods) together as a single unit.
 Goal: Hide the internal details (data hiding).
 Access: Controlled using access modifiers (private, public, protected).

class Student {
private int age;

public void setAge(int age) {


this.age = age;

public int getAge() {


return age;
}

}
2. Inheritance
 Definition: One class can inherit properties and methods from another.
 Goal: Promote code reusability.
 Keyword: extends (Java)

 Example:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}

class Dog extends Animal {


void bark() {
System.out.println("Dog barks");
}
}

3. Polymorphism
 Definition: One thing can take many forms.
 Types:

o Compile-time (Method Overloading):


Example:
class MathOps {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }

}
o Run-time (Method Overriding):
Example:
class Animal {
void sound() {

System.out.println("Animal sound");
}
}
class Cat extends Animal {

@Override
void sound() {
System.out.println("Meow");
}
}

4. Abstraction
 Definition: Hiding implementation details and showing only functionality.
 Achieved using:

o Abstract classes
o Interfaces
 Example:
abstract class Shape {
abstract void draw();

class Circle extends Shape {


void draw() {
System.out.println("Drawing Circle");

}
}
String:
In Java, String is a class in the java.lang package, used to represent a sequence of characters.
Strings are immutable, which means once created, they cannot be changed.
1. String DeclarationString s1 = "Hello"; // String literal

String s2 = new String("Hello"); // Using constructor

2. String Immutability
Once a String is created, its content cannot be changed.
String s = "Hello";

s.concat(" World");
System.out.println(s); // Output: Hello (not Hello World)

String Buffer:
StringBuffer is a mutable class in Java used to handle changeable strings. It is part of
java.lang package and is thread-safe, meaning it is synchronized and can be used safely in
multi-threaded environments.
Example:

StringBuffer sb = new StringBuffer(); // Empty buffer


StringBuffer sb2 = new StringBuffer("Hello"); // With initial content
StringBuffer sb3 = new StringBuffer(50); // With initial capacity

StringBuilder in Java
StringBuilder is a mutable class in Java used to create and manipulate dynamic strings
efficiently. It is not thread-safe, which makes it faster than StringBuffer in single-threaded
environments.

StringBuilder sb1 = new StringBuilder(); // Empty builder


StringBuilder sb2 = new StringBuilder("Hello"); // With initial value
StringBuilder sb3 = new StringBuilder(50); // With specified capacity
Exception Handling:
Exception Handling in Java is a mechanism to handle runtime errors (exceptions) so the
program can continue its normal flow even after an error occurs.
What is an Exception?

An exception is an unexpected event that disrupts the normal flow of a program during
execution.
All exceptions are objects of classes that inherit from the Throwable class.

Object
└── Throwable

├── Error // Serious issues (OutOfMemoryError, etc.)

└── Exception // Handleable problems

├── RuntimeException (Unchecked)

└── Other Exceptions (Checked)

try {
// risky code
} catch (ExceptionType e) {
// exception handler

Example:
public class Example {
public static void main(String[] args) {

try {
int a = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero!");
}
}

Summary:

Comments
Keyword/Usage

Try block Code that might cause error


Catch block Code to handle exception
Finally Executes after try/catch
Throw Manually throw an exception
Throws Declare possible exceptions

Looping Statements in Java

Loops in Java are used to execute a block of code repeatedly as long as a specified
condition is true.

for Loop Syntax in Java

The for loop is used when you know in advance how many times you want to execute a
statement or block of statements.

for (initialization; condition; update) {


// block of code to be executed
}

for (int i = 1; i <= 5; i++) {


System.out.println(i);
}

while Loop Syntax in Java

The while loop is used when you want to repeat a block of code as long as a condition is
true — especially when the number of iterations is unknown beforehand.

while (condition) {
// code to be executed repeatedly
}
for-each Loop Syntax in Java

The for-each loop (also known as the enhanced for loop) is used to iterate over elements
in arrays or collections without using an index.

for (type variable : arrayOrCollection) {


// code to use each element
}

Example:

int[] numbers = {10, 20, 30, 40, 50};

for (int num : numbers) {


System.out.println(num);
}

if Statement Syntax in Java

The if statement is a conditional control statement that allows you to execute code only
when a specific condition is true.

if (condition) {
// code to execute if condition is true
}

Example:

int age = 20;

if (age >= 18) {


System.out.println("You are eligible to vote.");
}

if-else Statement

if (condition) {
// true block
} else {
// false block
}
Example:

int num = 5;

if (num % 2 == 0) {
System.out.println("Even");
} else {
System.out.println("Odd");
}

if-else-if Ladder

if (condition1) {
// block 1
} else if (condition2) {
// block 2
} else {
// default block
}

Example:

int marks = 85;

if (marks >= 90) {


System.out.println("Grade A");
} else if (marks >= 75) {
System.out.println("Grade B");
} else {
System.out.println("Grade C");
}

static and final in Java

In Java, static and final are modifiers, not access specifiers (like public, private, protected).
They define how members (variables/methods) behave in relation to classes and objects.

1. static Modifier
 Belongs to the class, not objects.
 Shared among all instances.
 Can be accessed without creating an object.

Example:
class MyClass {
static int count = 0;

MyClass() {
count++;
}
}

public class Test {


public static void main(String[] args) {
MyClass obj1 = new MyClass();
MyClass obj2 = new MyClass();
System.out.println(MyClass.count); // Output: 2
}
}

2. final Modifier
 Used to declare constants, prevent method overriding, and class inheritance.
 Once assigned, cannot be changed.

Example:

final int x = 10;


// x = 20; ❌ Error: cannot assign a value to final variable

class A {
final void display() {
System.out.println("A");
}
}

class B extends A {
// void display() ❌ Error: Cannot override final method
}

final class A {}

// class B extends A ❌ Error: Cannot inherit from final class

Reference:

Overview (Java SE 24 & JDK 24)

You might also like