0% found this document useful (0 votes)
7 views5 pages

Notes

Java is a high-level, object-oriented programming language known for its platform independence and is used for various applications. Key concepts of Java include classes and objects, encapsulation, inheritance, and polymorphism, which enhance code reusability and maintainability. The document also covers variables, operators, and control statements like if and if-else, providing foundational knowledge for Java programming.

Uploaded by

afsahhaseeb9
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)
7 views5 pages

Notes

Java is a high-level, object-oriented programming language known for its platform independence and is used for various applications. Key concepts of Java include classes and objects, encapsulation, inheritance, and polymorphism, which enhance code reusability and maintainability. The document also covers variables, operators, and control statements like if and if-else, providing foundational knowledge for Java programming.

Uploaded by

afsahhaseeb9
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/ 5

Java is a high-level, object-oriented programming language developed by Sun Microsystems (now

owned by Oracle). It is known for its platform independence, meaning Java programs can run on any
device that has a Java Virtual Machine (JVM) installed. Java is used to develop mobile apps,
web apps, desktop apps, games and much more.

a class is a template for objects, and an object is an instance of a class.

When the individual objects are created, they inherit all the variables and
methods from the class.

Java - What is OOP?


OOP stands for Object-Oriented Programming.

Procedural programming is about writing procedures or methods that perform


operations on the data, while object-oriented programming is about creating
objects that contain both data and methods.

Object-oriented programming has several advantages over procedural


programming:

 OOP is faster and easier to execute


 OOP provides a clear structure for the programs
 OOP helps to keep the Java code DRY "Don't Repeat Yourself", and makes
the code easier to maintain, modify and debug
 OOP makes it possible to create full reusable applications with less code
and shorter development time
Tip: The "Don't Repeat Yourself" (DRY) principle is about reducing the repetition
of code. You should extract out the codes that are common for the application,
and place them at a single place and reuse them instead of repeating it.

Java - What are Classes and Objects?


Classes and objects are the two main aspects of object-oriented programming.

Java Classes/Objects
Java is an object-oriented programming language.

Everything in Java is associated with classes and objects, along with its
attributes and methods. For example: in real life, a car is an object. The car
has attributes, such as weight and color, and methods, such as drive and
brake.

A Class is like an object constructor, or a "blueprint" for creating objects.

Encapsulation
The meaning of Encapsulation, is to make sure that "sensitive" data is hidden
from users. Encapsulation is about keeping data and methods together in one place, called a
class. This way, the details inside the class are hidden from the outside, so other parts of the
program can’t directly change the data.

To control what can be accessed:

 Access modifiers like public, private, and protected are used.


o Private means only the class itself can use the data or method.
o Public means other parts of the program can use it.

Inheritance: Inheritance is a mechanism where a child class (subclass) is derived from an


existing class/ parent class (superclass). The subclass inherits properties and behaviors (methods)
of the superclass. It can also have its own additional properties and behaviors. In Java,
inheritance is achieved using the extends keyword.

Polymorphism means one action can work in different ways depending on the context.

In Java, it has two main types:


1. Compile-time polymorphism (method overloading): This happens when a class has
multiple methods with the same name but different parameters. Java picks the right
one based on the arguments used.
o Example: You might have two print methods: one that prints a number and one
that prints text.
2. Runtime polymorphism (method overriding): This happens when a subclass changes
how a method works that was already defined in its parent class.
o Example: A Bird class has a makeSound() method. The Parrot subclass can
override makeSound() to provide its own sound.

With polymorphism, we can treat different classes as if they’re the same, but each class can
still have its own specific behavior.

ESCAPE SEQUENCE IN JAVA: A character with a backslash (\) just before it is an escape
sequence or escape character. We use escape characters to perform some specific task. The total
number of escape sequences or escape characters in Java is 8. Each escape character is a valid
character literal. The list of Java escape sequences:

VARIABLES

Variables are containers for storing data values. Variables in Java language are the names used to
refer to data stored in the memory. One Java variable can hold only a single type of data. It is a
combination of "vary + able" which means its value can be changed.

TYPES OF VARIABLES IN JAVA 1. Local Variables 2. Instance Variables 3. Static Variables

1) Local Variable

A variable declared inside the body of the method is called local variable. You can use this
variable only within that method and the other methods in the class aren't even aware that the
variable exists.

A local variable cannot be defined with "static" keyword.


2) Instance Variable

A variable declared inside the class but outside the body of the method, is called an instance
variable. It is not declared as static.

It is called an instance variable because its value is instance-specific and is not shared among
instances.

public class Student { String name; // Instance variable public void setName(String newName) {
name = newName; } }

3) Static variable

A variable that is declared as static is called a static variable. It cannot be local. You can create a
single copy of the static variable and share it among all the instances of the class. Memory
allocation for static variables happens only once when the class is loaded in the memory.

public class Counter { static int count = 0; // Static variable public Counter() { count++; } }

Introduction to operators In Java, operators are symbols used to perform operations on variables and
values. They are categorized into different types based on the operations they perform, such as
arithmetic, relational, logical, bitwise, assignment, and more.

Basic Arithmetic Operators

These operators are used to perform basic arithmetic operations like addition, subtraction,
multiplication, division, and modulus.  + (Addition)  - (Subtraction)  * (Multiplication)  / (Division) 
% (Modulus)

Assignment Operators

These operators are used to assign values to variables.  = (Simple assignment)  +=, -=, *=, /=, %=
(Compound assignment)

Relational Operators
These operators are used to compare values and determine the relationship between them. They return
a Boolean value (true or false).  == (Equal to)  != (Not equal to)  > (Greater than)  < (Less than)  >=
(Greater than or equal to)  <= (Less than or equal to)

Logical Operators

These operators are used to perform logical operations on Boolean values.  && (Logical AND)  ||
(Logical OR)  ! (Logical NOT)

Bitwise Operators

These operators perform bitwise operations on integer operands.  & (Bitwise AND)  | (Bitwise OR)  ^
(Bitwise XOR)  ~ (Bitwise Complement)  << (Left shift)  >> (Signed right shift)

If statement

If statement is the simplest decision making statement. It is used to decide whether a certain statement
or block of statements will be executed or not.

public class Main { public static void main(String[] args) { int x = 10; if (x > 5)
{ System.out.println("x is greater than 5"); } } }

If-else Statement

If statement alone tells us that if a condition is true it will execute a block of statements and if the
condition is false it won’t. But what if we want to do something else if the condition is false. Here comes
the else statement.

If-else Statement

If statement alone tells us that if a condition is true it will execute a block of statements and if the
condition is false it won’t. But what if we want to do something else if the condition is false. Here comes
the else statement.

A nested if is an if statement that is the target of another if or else. Nested if statements mean an if
statement inside an if statement. Yes, java allows us to nest if statements within if statements.

You might also like