Java OOPs Concepts - Notes for Quick Revision
---
Type Conversion in Java
1. Widening Conversion (Implicit):
- Converts a smaller data type to a larger one.
- Example: int to long, float to double
int x = 10;
long y = x; // Implicit conversion
2. Narrowing Conversion (Explicit):
- Converts a larger data type to a smaller one using casting.
- Example: double to int
double a = 9.7;
int b = (int) a; // Explicit casting
---
Basic Java Program Structure
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
- public class Main: Declares a public class named Main.
- public static void main(String[] args): Main method (entry point).
- System.out.println(): Prints text to the console.
String[] args:
- Accepts input from the command line.
public class Example {
public static void main(String[] args) {
System.out.println("Argument: " + args[0]);
---
String Allocation
String name = "Java"; // Allocated in String pool
String another = new String("Java"); // Allocated in heap memory
- String Pool: Saves memory by storing one copy of identical string literals.
Example:
String a = "Hello";
String b = "Hello";
System.out.println(a == b); // true (same reference from pool)
String c = new String("Hello");
System.out.println(a == c); // false (different object in heap)
---
Common String Methods
String s = "Hello";
s.length(); // 5
s.charAt(1); // 'e'
s.contains("ell"); // true
s.indexOf("l"); // 2
s.substring(1, 4); // "ell"
---
Bitwise Operators
Operator | Meaning | Example
-------- | ----------- | ---------------
& | AND |5&3=1
| | OR |5|3=7
^ | XOR |5^3=6
~ | NOT | ~5 = -6
<< | Left shift | 5 << 1 = 10
>> | Right shift | 5 >> 1 = 2
---
Conditional Statements
if (condition) {
// code
} else if (otherCondition) {
// code
} else {
// code
// switch
switch (value) {
case 1: break;
case 2: break;
default: break;
// ternary
String result = (age > 18) ? "Adult" : "Minor";
---
Arrays in Java
- Used to store multiple values of the same type.
int[] arr = new int[5];
arr[0] = 10;
int[] arr2 = {1, 2, 3, 4};
- Stored in heap memory.
Example:
for (int i = 0; i < arr2.length; i++) {
System.out.println(arr2[i]);
---
Encapsulation
- Binding data and methods into a single unit (class).
- Achieved by:
- Making variables private
- Providing public getters and setters
Example:
class Person {
private String name;
public String getName() {
return name;
public void setName(String name) {
this.name = name;
---
Access Modifiers
Modifier | Class | Package | Subclass | World
---------- | ----- | ------- | -------- | ------
private | Yes | No | No | No
default | Yes | Yes | No | No
protected | Yes | Yes | Yes | No
public | Yes | Yes | Yes | Yes
---
Polymorphism
1. Method Overloading (Compile-time):
class Calculator {
int add(int a, int b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; }
2. Method Overriding (Runtime):
class Animal {
void sound() { System.out.println("Animal sound"); }
class Dog extends Animal {
void sound() { System.out.println("Dog barks"); }
---
Abstraction
1. Abstract Class:
abstract class Shape {
abstract void draw();
void display() { System.out.println("Displaying shape"); }
2. Interface:
interface Drawable {
void draw();
interface Paintable {
void paint();
class Circle implements Drawable, Paintable {
public void draw() { System.out.println("Drawing Circle"); }
public void paint() { System.out.println("Painting Circle"); }
---
These notes cover core Java OOP topics.