Object-Oriented Programming Principles and Data
Types in Java
1. OOPs Principles
OOPs (Object-Oriented Programming System) organizes code into objects (real-world entities like
Student, Car, etc.). It makes programs modular, reusable, and easy to maintain. The main four
principles are:
Encapsulation: Wrapping of data (variables) and methods into a single unit (class). Helps protect
data from being accessed directly.
Inheritance: Acquiring properties and behaviors of one class into another using the 'extends'
keyword.
Polymorphism: One name, many forms. A function or object behaves differently based on context.
Abstraction: Showing only essential features and hiding internal details using abstract classes or
interfaces.
2. Integer Data Types
Data Type Size Range
byte 1 byte -128 to 127
short 2 bytes -32,768 to 32,767
int 4 bytes -2,147,483,648 to 2,147,483,647
long 8 bytes Very large range
Example Program:
public class IntegerTypes { public static void main(String[] args) { byte b = 100; short s = 2000; int i
= 50000; long l = 15000000000L; [Link]("Byte value: " + b); [Link]("Short
value: " + s); [Link]("Int value: " + i); [Link]("Long value: " + l); } }
3. Floating Point Data Types
Data Type Size Example
float 4 bytes 3.14f
double 8 bytes 19.12345
Example Program:
public class FloatTypes { public static void main(String[] args) { float f = 5.75f; double d =
19.87654321; [Link]("Float value: " + f); [Link]("Double value: " + d); } }
4. Type Conversion and Type Casting
Type Conversion (Implicit/Widening): Automatically converts smaller type to larger type.
Example: public class ConversionExample { public static void main(String[] args) { int a = 10;
double b = a; // automatic conversion [Link]("Int: " + a); [Link]("Converted
to double: " + b); } }
Type Casting (Explicit/Narrowing): Manually converts larger type to smaller type.
Example: public class CastingExample { public static void main(String[] args) { double x = 9.78; int y
= (int) x; // manual casting [Link]("Double: " + x); [Link]("After casting to int:
" + y); } }