0% found this document useful (0 votes)
19 views13 pages

Overview of Java Programming Concepts

Java is a high-level, object-oriented programming language known for its platform independence, currently owned by Oracle Corporation. It utilizes the Java Development Kit (JDK) for development and the Java Runtime Environment (JRE) for running applications, with features like portability, security, and robustness. Key concepts include object creation, variable types, OOP principles (inheritance, encapsulation, polymorphism, abstraction), exception handling, and multithreading.

Uploaded by

Suyash Rokade
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)
19 views13 pages

Overview of Java Programming Concepts

Java is a high-level, object-oriented programming language known for its platform independence, currently owned by Oracle Corporation. It utilizes the Java Development Kit (JDK) for development and the Java Runtime Environment (JRE) for running applications, with features like portability, security, and robustness. Key concepts include object creation, variable types, OOP principles (inheritance, encapsulation, polymorphism, abstraction), exception handling, and multithreading.

Uploaded by

Suyash Rokade
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

Java is a high-level object-oriented programming language developed by sun micro-systems.

It is known for its platform independence.

●​ freely downloadable,
●​ open Source,
●​ currently owned by oracle corporation

The JDK (Java Development kit) is used by developers for creating Java applications and
includes the necessary tools, libraries and compiler.

The JRE (Java Runtime Environment) is used by end-users to run java applications and
provides the runtime environment and essential class libraries, but does not include
development tools

How Java is platform independent

1.​ The Bytecode (.class) file will be created in one System os

2.​ That bytecode file will be transported through internet and will be downloaded in other os

3.​ The other OS will have a JVM installed which will help that class file of java code to run
in their System.
4.​ Through this java got platform independent.

These are the benefits of using Java:


●​ Portability: Java code can be run on any platform that has a Java Virtual Machine
(JVM).
●​ Security: Java has a built-in security model that helps to protect users from malicious
code.
●​ Object-oriented: Java is an object-oriented programming language, which makes it
easy to create modular and reusable code.
●​ Robust: Java is a robust language that is designed to be reliable and efficient.
●​ Widely used: Java is a widely used language that has a large community of developers
and support resources.

object creation.

●​ We can use the "new" operator to create an object.


●​ There is no "delete " operator in Java because destruction of useless object is
responsibility of the garbage collector

Types of variable
Based on the behaviour and position of declaration. All variables divided into the following 3
types

1. Instance

2. Static

3. Local.

Instance Variable

●​ The instance variable will be created at the time of object creation and destroyed at the
time of object destruction hence the scope of the instance variable is exactly the same
as the scope of objects.
●​ Instance variables should be declared within the class but outside of any method or
block constructor

●​ Instance variables will be stored on the heap as the part of the object.

Local variable.

●​ The local variable will be created as part of the execution in which it is declared and
destroyed once that block execution complete

●​ Hence the scope of the local variable is exactly the same as the scope of the block in
which we declared.
Array

●​ Array is a collection of fixed number of homogeneous data

●​ Single variable holding multiple values which improves readability of the program.

●​ Every array in java is an object hence. we create using a new operator.

Disadvantages of Array

●​ once we create the size cannot be increased/decreased.

●​ It stores only homogeneous data elements

length vs length()
length: It is property of the Array type class
length(): It is a method of string class.

String

●​ String is a collection of character


●​ String is object in java present in package called [Link]
●​ by default immutable

Mutable String

Once if we create a string, on that String if we try to perform any operation and if those changes
get reflected in the Same object then such strings are called "Mutable string"

Example: StringBuffer, StringBuilder

Immutable String.

Once if we create a string, on that if we try to perform any operation then those changes won't
be reflected in the same object, rather, a new object will be created such type of string is called
"Immutable String"

String class Constructor

●​ String S = new String()

●​ String s = new String (String literals)

●​ String s = new string ( stringBuffer sb)

●​ String s new String (byte[] b)

●​ String s = new String ( char[] ch)

If the content will change frequently then it is not recommended to go for a string object becoz
for every new change a new object will be created.

== operator

It compares the reference of the object().


equals

It compares the content of two object.

Concatenation

The technique of connecting two or more Strings together to create a new string is known
Concatenation

1) + operator
2) [Link]() Method
Static Keyword

●​ Static keyword is mainly used for memory management in java.

●​ Static keyword can be applied with variables, blocks, functions and class.

●​ Static keyword is property of a class rather than an instance of the class

●​ The static keyword is used for constant variable or a method that is the same for every
instance of a class

●​ In case of instance variables for every object a separate copy will be created but In the
case of static variable for the entire class only one copy will be created and shared by
every object of that class

●​ Static variables will be created at the time of class loading and destroyed at the time of
class unloading.

●​ Scope of the static variable is exactly the same as the scope of the .class file.

we can access static variable in 2 way

●​ using className
●​ using a reference variable.

Static method

●​ Inside the static methods we can write only static variables.


OOPs

Object-oriented programming is a way of writing programs that use objects to organise and
structure code.

●​ It is designed to represent real-world entities like cars, employees


●​ It is based on four key principles:inheritance,Encapsulation,polymorphism Abstraction.
●​ oops makes code more modular, reusable and easier to maintain.

Class

A class is a blueprint that defines the properties and behaviors of objects -It encapsulates data
and functions that work on the data.

object

An object is a real world entity with state and behavior.

Eg. Car

State:Name,Color,Price

Behavior:Start,Stop,Drive

Access modifier/specifier.

1. public: member care accessible everywhere.

2. private: Member aire accessible within a same package and in other package as well if called
through inheritance

3. protected:member are accessible with a same package

4. default - Member are accessible with a Same class

Constructor

●​ A constructor in java is a special method used to initialize objects, Class

●​ It has the same name as the rand does not have a return type
●​ Constructors an are called automatically when object is created and are used to set
initial values for object attributes

There are two type of constructor

1. Default (with arguments)

2. Parameterized

Super() Vs this()

1. The first line inside the constructor can be Super()/ this()

2. If we are not writing anything then Compiler coill generate superu

Diff b/w super() / this()

1. This These are constructor calls

2. There are used to invoke Superclass and current class constructor directly

3. we should use only inside the Constructor that to first line otherwise we get compile time
error.

Inheritance

Inheritance allows a class to reuse properties and behaviors of another class

eg.

If you have vehicle class with bas dass can inherit Properties, instead dussede car redefining
everything

Class Vehicle {
int wheels;
}

class cars extends vehicle {


​ public string brand:
}
Types of inheritance

1. Single-level inheritance!

2. Multi-level inheritance

3. Hierarchical inheritance

4. Multiple Inheritance.

Multiple inheritance is not allowed because of ambiguity problems. There are multiple user
Super classes for a single subclass so, super() calls superclass constructor. Because of
ambiguity issue compiler doesn't understand which superclass method to be execute

Advantages

●​ It always speaks about reusability.

●​ using inheritance productivity of the code Can be improved.

●​ Line of code can be reduced in the application

Encapsulation
Binding of data and corresponding methods into a single unit is called "Encapsulation"

●​ To outside world exposed directly the data should not b

●​ In order to provide the controlled access we need to use "Encapsulation"

Encapsulation = Data abstraction +Data Hiding

Advantages

●​ we can achieve Security

●​ Maintainability and modularisation becomes easy

●​ It provides flexibility to the user to use the system very easily.

●​ By using private modifier we can implement "data hiding"

shadow problem
If both local and instance variable have the Same name inside the method then it would result in
name-clash and JVM will always give preference for local variable

eg

class student {

private int age:

private String name;

public void setName(string Name) {

this. Name = "Navin";


}

public void string getName() {

return name;
}

public void show {

[Link](name):
}
}

Abstraction

Abstraction focuses on showing only the essential details and hiding the unnecessary
Complexity

eg

when you drive a car, you just press the accelerator to speed up-you don't need to know how
the engine works internally.

Abstraction is achieved using abstract classes or interfaces.


Class Base {

abstract void show():

class derived extends Base {

@override

void show (){

[Link]("Derived class");
}

Abstract class and Abstract methods.

●​ In java applications, if we declare any abstract class with abstract method there it is
convention to implement all the abstract methods by taking sub classes, reference
variable

●​ If we declare a subclass then we are able to access both abstract class members and
subclass members.

final Keyword

●​ If a variable is marked as final then those variable are treated as compile time Constants
and we should not change the value of those variable.

●​ If a method is declared as fined then those methods we can't override

●​ If a class is marked as final then the class coon't participate in inheritance

Polymorphism

●​ Polymorphism means many forms


●​ It allows same function to behave differently based on the object calling it

eg

A person can be a parent at home and an employee at work employee are different roles of the
same person.

polymorphism achieved using method overloading and method overriding


1) Compile time polymorphism achieved by using method overloading

2) Runtime polymorphism achieved by using method overriding.

Method overloading

The process of extending the existing method with some new functionality is called method
overloading is called ove Method overloading

Method overriding

The process of replacing existing method functionality with some new functionality is Called
Method overriding.

●​ To perform on method overriding we must have inheritance relationship classes.

●​ In case of method overriding, the same method prototypes must be provided to the
method

class person {

void role{

[Link]("I am a person");
}

Class father extends person {

@override
void role{

[Link]("I am a fresher");
}

Why use oop?

1. Modularity and Reusability: oops promotes modularity through classes and objects allowing
for code reusability.

2. Data Encapsulation: oops encapsulates data within objects enhancing data security and data
integrity.
Why do we need oops?

→ object oriented programming is useful for breaking down complex systems into Smaller, more
manageable components. This makes it easier to develop, maintain and modify software. OOPs
also help to ensure code reusability and data security.

Exception handling

An exception is an event which occurs during the execution of a program that disrupts the
normal flow of the program's instructions.

Exception handling is a mechanism used in oop to handle runtime error or exception

It allows a program to continue executing by catching error and providing alternate Solutions,

try {

​ //code that may throw a exception

} catch (ExceptionType e){

// code to handle the exception

finally{
// cleanup code
}

The try block contains code that might throw an exception.

The catch block handles the exception if it occurs

Finally, Block is used to execute important code regardless of whether an exception occurs or
not.

Type of Exception

Checked-Exception.
Checked Exceptions are called compile-time exceptions because these exceptions are Checked
at compile time by the Compiler

Eg. ​ Class Not found Exception.


SQLException

Unchecked Exceptions.

●​ The compiler will not check these exceptions at compile time.


●​ if program throws an unchecked exception and even if we didn't handle of declare it, the
program would not give a Compilation error

eg.
NullpointerException
Array Index out of Bounds Exception.

The throw and throws keyword are used to handle exception

●​ The throw keyword is used to explicitly throw an exception


●​ The throws keyword is used to declare that a method Can throw an exception

Multithreading

Multithreading is a java features that allows Concurrent execution of two or more parts of a
program for maximum utilization of CPU

Threads are light-weight processes within a process.

Threads can be created by using two mechanisms

1. Extending the thread class.

2. Implementing the Runnable interface

Thread scheduler

If multiple threads are waiting to execute then which thread will execute ist is decided by Thread
scheduler which is part of JVM

The run() method is available in the class Constructed using a Separate Runnable object.
Synchronized keyword

Synchronized keyword is used to make class or method thread, safe which only one thread can
have tock of Synchronized method and use it, other threads have to wait till the lock releases
and anyone of them acquire that lock

-It is important to use if our program is running in a multi-threaded environment where two more
threads execute simultaneously. But sometimes it also causes a problem which is called

Deadlock

A deadlock is a Situation in which two or more processes are unable to proceed because each
is waiting for the other to release a resource.

●​ It occurs when four conditions are met mutual exclusion, hold and wait, no preemption
and circular wait

●​ Deadlock can be prevented by avoiding these conditions or resolved using techniques


like resource allocation graphs or timeout.

You might also like