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

m1 Notes

Uploaded by

ketif41959
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

m1 Notes

Uploaded by

ketif41959
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Object-Oriented Programming (OOP)

Object-Oriented Programming (OOP) is a programming paradigm that uses "objects" to design


software. It emphasizes the organization of software around data, or objects, rather than functions
and logic. Here are the principles of OOP:

1. Encapsulation:

● Encapsulation is the bundling of data (attributes) and methods (functions) that


operate on the data within a single unit (class).

● It restricts direct access to some of an object's components, which is a means of


preventing unintended interference and misuse of the methods and data.

2. Inheritance:

● Inheritance is a mechanism that allows one class (the child or subclass) to inherit the
attributes and methods of another class (the parent or superclass).

● This promotes code reusability and establishes a hierarchical relationship between


classes.

3. Polymorphism:

● Polymorphism allows objects to be treated as instances of their parent class, even if


they are actually instances of a child class.

● It enables a single interface to represent different underlying forms (data types). The
two main types of polymorphism are:

● Compile-time polymorphism (method overloading)

● Run-time polymorphism (method overriding)

4. Abstraction:

● Abstraction is the concept of hiding the complex implementation details and


showing only the essential features of the object.

● It allows focusing on what an object does instead of how it does it, simplifying the
interaction with complex systems.

Advantages of OOP

● Modularity: Code is organized into discrete objects, making it easier to manage and
understand.

● Reusability: Classes can be reused across programs, reducing redundancy.

● Maintainability: Changes in one part of the code can be made with minimal impact on other
parts.

● Flexibility: OOP allows for the creation of flexible systems that can evolve over time.

Java Data Types and Variables


Java has two main categories of data types: primitive data types and reference data types.

1. Primitive Data Types

Primitive data types are the basic types built into Java. There are eight primitive data types:

Data
Type Size Description

byte 8 bits Integer type, range: -128 to 127

16
short bits Integer type, range: -32,768 to 32,767

32 Integer type, range: -2,147,483,648 to


int bits 2,147,483,647

64
long bits Integer type, range: -2^63 to 2^63-1

32
float bits Floating-point type, for decimal values

64
double bits Floating-point type, for double precision

16
char bits Single 16-bit Unicode character

boolean 1 bit Represents true or false

2. Reference Data Types

Reference data types refer to objects and arrays. They store references to the actual data
rather than the data itself.

● String: A sequence of characters.

● Arrays: A collection of similar data types.

Variables in Java

A variable is a named storage location in memory that holds data. Variables must be
declared with a specific data type before use.

1. Declaration: Specifies the variable's name and data type.

2. Initialization: Assigns a value to the variable.


Control Statements in Java

Control statements in Java allow you to dictate the flow of execution in your program. The
most commonly used control statements are if, switch, and loops like while. Below is an
explanation of each, along with examples.

1. if Statement

The if statement is used to execute a block of code conditionally. If the condition evaluates
to true, the code inside the if block is executed.

Syntax:

java

VerifyOpen In EditorEditCopy code

1if (condition) {

2 // Code to execute if condition is true

3}

Example:

java

VerifyOpen In EditorEditCopy code

public class IfExample {

public static void main(String[] args) {

int number = 10;

if (number > 0) {

System.out.println("The number is positive.");

} else {

System.out.println("The number is not positive.");

}}}

2. switch Statement

The switch statement is used to execute one block of code among multiple options based on
the value of a variable. It is often used as an alternative to multiple if statements.

Syntax:

java

VerifyOpen In EditorEditCopy code

switch (expression) {

case value1;
break;

case value2:

break;

default: }

3. while Loop

The while loop is used to execute a block of code repeatedly as long as a specified condition
is true. It checks the condition before each iteration.

Syntax:

java

VerifyOpen In EditorEditCopy code

1while (condition) {

2 // Code to execute repeatedly

Example:

java

VerifyOpen In EditorEditCopy code

1public class WhileLoopExample {

2 public static void main(String[] args) {

3 int count = 1;

5 while (count <= 5) {

6 System.out.println("Count: " + count);

count++;

}}}

Summary

● if Statement: Used for conditional execution of code blocks based on boolean expressions.

● switch Statement: Used for multi-way branching based on the value of a variable.

● while Loop: Used for repeated execution of a block of code as long as a condition is true.

You might also like