0% found this document useful (0 votes)
14 views22 pages

Java

Java is an object-oriented programming language used for various applications, including mobile, desktop, and web. It operates on the principle of 'Write once, run anywhere' through the Java Virtual Machine (JVM), which allows Java programs to run on any device. Key features of Java include simplicity, security, robustness, and support for multithreading and distributed computing.

Uploaded by

chugliguhaat
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)
14 views22 pages

Java

Java is an object-oriented programming language used for various applications, including mobile, desktop, and web. It operates on the principle of 'Write once, run anywhere' through the Java Virtual Machine (JVM), which allows Java programs to run on any device. Key features of Java include simplicity, security, robustness, and support for multithreading and distributed computing.

Uploaded by

chugliguhaat
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 (JAMES GOSLIN)

Java program is an object-oriented programming language, that means


java is the collection of objects, and these objects communicate through
method calls to each other to work together.

It is used for:

● Mobile applications (specially Android apps)


● Desktop applications
● Web applications
● Web servers and application servers
● Games
● Database connection

JVM

The Java virtual machine (JVM) is a virtual machine that enables a


computer to run Java programs.

The JVM has two primary functions, which are:

To allow Java programs to run on any device or operating system


(this is also known as the "Write once, run anywhere" principle).

And, to manage and optimize program memory.


FEATURES OF JAVA

1. Simple
2. Object-Oriented

3. Platform independent
4. Portable
5. Secure

6. Robust
7. Architecture neutrel
8. Dynamic
9. High Performance
10. Multithreading
11. Distribute
1. Simple
Java is very easy to learn, and its syntax is simple, clean and easy to
understand. Java syntax is based on C++ (so it is easier for
programmers to learn it after C++).

2. Object-oriented

Java is an object-oriented programming [Link] supports every


OOP concept, such as Abstraction, Encapsulation, Inheritance, and
Polymorphism. Java programs are developed using classes and
objects.

3. Platform Independent

Java is a programming language that can run on different types of


computers and operating systems.
It did this by using something called a Java Virtual Machine (JVM).
When you write a Java program, It's first turned into a special code
(bytecode), which the JVM can understand. Then, the JVM on your
computer converts this bytecode into instructions that your
computer's operating system can execute.
This way, you can write a Java program once, and it can work on
Windows, Mac, or Linux without needing to change the code. This
makes Java a "write once, run anywhere" language and is why it's
considered platform-independent.
JVM is platform dependent
Every operating system has its own version of JVM, which is capable
of reading and converting bytecode to an equivalent machine’s native
language.

4. Portable
Java works on all sorts of computers without needing changes
because it uses a special program called the Java Virtual
[Link] WORA (Write Once Run Anywhere) concept and
platform-independent feature make Java portable.

5. Secure

Java provides a strong Exception Handling mechanism which makes it


more secure as it does not allow you to run the program until you rectify the
error. Java provides access modifiers/access specifiers like private,
protected, public and default. It helps in controlling the access of any
member.
6. Robust

Java is like a reliable guard for computer programs. It keeps things


organized, makes sure there are no security risks, cleans up unused
memory, and handles mistakes without causing problems. This makes
Java a strong and trustworthy choice for building software.

● It uses strong memory management.


● Java provides automatic garbage collection which runs on the Java
Virtual Machine to get rid of objects which are not being used by a
Java application anymore.
● There are exception handling and the type checking mechanism in
Java. All these points make Java robust.

7. Architecture Neutral

Java is architecture neutral because you can write Java code on one
platform, such as Windows, and run it on another platform, such as Linux,
without needing to recompile the code.

8. Dynamic

you can load classes only when you need them, which helps save memory
and resources. Additionally, Java can use functions from languages like C
and C++. Java supports the dynamic loading of classes. It means classes
are loaded on demand.
9. High Performance

Java is faster than many other interpreted languages because it compiles


code into bytecode, which is like machine code, and uses a Just-In-Time
compiler to execute only the needed parts, making it faster and efficient.

10. Multithreading

Multithreading is a Java feature that allows concurrent execution of two or


more parts of a program for maximum utilization of CPU.

Thread is a lightweight and independent subprocess of a running program


(i.e, process) that shares resources. And when multiple threads run
simultaneously is called multithreading. In many applications, you have
seen multiple tasks running simultaneously, for example, Google Docs
where while typing text, the spell check and autocorrect tasks are running.

11. Distributed

Java is said to be "distributed" because it enables developers to build


applications that can be accessed and used from different computers
connected via the internet.
Class (Logical entity)
A class is a blueprint which includes all data. It defines the structure,
behavior, and properties that its objects will have.
Example: Animal, Human, Fruit, Vehicle

Object (Physical entity)


An object is an instance of a class. an object is like a real-world
[Link] have their own unique data and can perform actions defined
by the class.
Example: Lion,Men/Women, Apple, Car

Object has state and behavior


State refers to the properties of an object. Behavior refers to actions that
the object can take.
Example:
Dog- State: colour,breed
Behavior: bark,walk

Method
A method is a block of code which only runs when it is [Link] can pass
data, known as parameters, into a [Link] are used to perform
certain actions, and they are also known as [Link] using methods
you only have to define the code once, and then you can use it over and
over again whenever you need it.
public class Greeting {

// Method to display a greeting message

public void sayHello() {

[Link]("Hello, World!");

public static void main(String[] args) {

//classname objectname = new classname (Used to Create an object of the Greeting class)

Greeting obj= new Greeting();

// Call the sayHello method to display the greeting message

[Link]();

OUTPUT

Hello, World!
Variables in Java

Variables are containers for storing data values. In Java, there are
different types of variables,

Instance Variables: variables which are declared inside the class but
outside the method. They have class-wide visibility, which means they are
accessible throughout the entire class and can be accessed by any method
within the class.

Local Variables: variables which are declared inside the [Link]


have limited scope and are only accessible within the method in which they
are declared.

Static Variables: Static variables are declared within a class but outside of
any method, and they are marked with the static [Link] have class-
wide visibility, similar to instance variables. They are accessible throughout
the entire class and can be accessed by any method within the class.
Example
public class test
{
// data type variable name= value;
int a=10; // instance variable
private static int data=19; //static variable
public void m1()
{
int b=20; // local variable
[Link](b);
// print 20
}

public void m2()


{
[Link](a);
// print 10 because ‘a’ is an instance variable, it can be used anywhere in the class.
[Link](b);
// compile time error message should be displayed, because b is a local variable only it
can be used in m1() method.
}
}

Datatypes in Java

● String - stores text, such as "Hello". String values are surrounded by


double quotes
● int - stores integers (whole numbers), without decimals, such as 123
or -123
● float - stores floating point numbers, with decimals, such as 19.99 or
-19.99
● char - stores single characters, such as 'a' or 'B'. Char values are
surrounded by single quotes
● boolean - stores values with two states: true or false

JAVA INSTALLATION
ECLIPSE download
GOOGLE - Eclipse for java developers - DOWNLOAD ECLIPSE IDE
[Link]
[Link]

DOWNLOAD JAVA JDK 17-> Windows x64 Installer


[Link]

Download selenium webdriver for java


GOOGLE - selenium webdriver - CHOOSE JAVA STABLE VERSION - INSTALL

[Link]

Chromedriver download
[Link]
OOPS CONCEPT

Object-Oriented Programming is a methodology or paradigm to design a


program using classes and objects

● Object

● Class

● Abstraction

● Encapsulation

● Inheritance

● Polymorphism
1. ABSTRACTION

Abstraction is a process which displays only the information needed


and hides the unnecessary information. We can say that the main
purpose of abstraction is data hiding.

Class is an abstract data type, datas in class are private. To access


these data we make some public functions. Through these functions
we can access data inside the class.

EXAMPLE

Imagine you have a TV remote. You know that when you press the
power button, the TV turns on, and when you press the volume button,
the sound changes. You don't need to understand how pressing the
button on the remote actually makes these things happen inside the TV.
2. ENCAPSULATION

Wrapping up of data members and its member functions into a single


unit is called encapsulation. Encapsulation can be used to hide both
data members and data functions or methods.
Information hiding or data hiding in programming is about protecting
data or information from any inadvertent change throughout the
program.

To achieve this, you must:


● Declare class variables/attributes as private.
● Provide public get and set methods to access and update the value of
a private variable.
3. INHERITANCE
It is the mechanism in Java by which one class is allowed to inherit
the features(fields and methods) of another class.A class that inherits
from another class can reuse the methods and fields of that class.
Subclass (child) -A class that is derived from another class is called a
subclass (also a derived class, extended class, or child class).
Superclass (parent) - The class from which the subclass is derived is called
a superclass (also a base class or a parent class), and extends keyword is used
for inheritance.
4. POLYMORPHISM

Ability to take more than one form. Polymorphism refers to an entity


in code such as a variable, function or object that can have more than
one form. It performs a single action in multiple forms.
Real-life Illustration of Polymorphism in Java: A person at the same
time can have different characteristics. Like a man at the same time
is a father, a husband, and an employee. So the same person
possesses different behaviors in different situations. This is called
polymorphism.
METHOD OVERLOADING

It is a feature in JAVA where multiple functions can have the same name
but with different parameter lists. The compiler will decide which function to
call based on the number and types of arguments passed to the function.

public class Calculator {


// Method to add two integers
int add(int a, int b) {
return a + b;
}
// Method to add three integers
int add(int a, int b, int c) {
return a + b + c;
}
int add(int a, double b) {
return a + b;
}
public static void main(String[] args) {
Calculator calc = new Calculator();
// Using the first add method (integers)
[Link]("Sum is: " + [Link](5, 10));
// Using the second add method (three integers)
[Link]("Sum is: " +[Link](5, 10, 15));
[Link]("Sum is: " +[Link](5, 10.5));
}
}
Output
Sum is: 15
Sum is: 30
Sum is: 15.5
METHOD OVERRIDING

A derived (child) class has a definition for one of the member functions of
the base (parent) [Link] method is defined in both the parent class
and the child class, then the method of the child class overrides the method
of the parent class. This is known as method overriding.

// Parent class
public class First
{
void makeSound()
{
[Link]("Generic animal sound");
}
public static void main(String[] args) {
First anim = new First();
[Link]();
Dog myDog = new Dog();
[Link]();
}
}
// child class
class Dog extends First
{
void makeSound()
{
[Link]("Woof! Woof!");
}

Output
Generic animal sound
Woof! Woof!

Conditional Statements

Conditional Statements
[Link]
Iterative Statements
[Link]

IF-ELSE

The Java if-else statement also tests the condition. It executes the if block if the
condition is true otherwise the else block is executed.

if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
Example

//It is a program of odd and even number.


public class IfElseExample {
public static void main(String[] args) {
//defining a variable
int number=13;
//Check if the number is divisible by 2 or not
if(number%2==0){
[Link]("even number");
}else{
[Link]("odd number");
} } }

OUTPUT : odd number


Iterative Statements

While loop
The while statement is used to execute a single statement or block of
statements repeatedly as long as the given condition is TRUE. The while
statement is also known as Entry control looping statement.
syntax
while (test_expression)
{
// statements
update_expression;
}

Example
class whileLoopDemo {

public static void main(String args[])

{
// initialization expression

int i = 1;

// test expression

while (i < 6) {

[Link]("Hello World");

// update expression

i++;
}
}}
Do while loop
The do-while statement is used to execute a single statement or block of
statements repeatedly as long as the given condition is TRUE. The do-
while statement is also known as the Exit control looping statement.
do...while loop executes at least once i.e. the first iteration runs without
checking the condition. The condition is checked only after the first iteration
has been executed.

syntax
do
{
// Loop Body
Update_expression
}
// Condition check
while (test_expression);
Example

class GFG {
// Main driver method
public static void main(String[] args)
{
int i = 1;
do {
// Body of loop that will execute minimum 1 time for sure no matter what
//condition is true or false
[Link]("Print statement");
i++;
}
// Checking condition
while (i < 0);
}
}

OUTPUT
Print statement
For Loop
The for statement is used to execute a single statement or a block of
statements repeatedly as long as the given condition is TRUE. A "For"
Loop is used to repeat a specific block of code a known number of times.

Syntax
for (initialization; condition; increment/decrement) {
// code to be executed
}

Example
class forLoopDemo {

public static void main(String args[])

// Writing a for loop


// to print Hello World 5 times

for (int i = 1; i <= 5; i++)

[Link]("Hello World"); }
}
Java User Input
The Scanner class is used to get user input, and it is found in the [Link]
package.

Example 1
public static void main(String[] args) {
Scanner myObj = new Scanner([Link]); // Create a Scanner object
[Link]("Enter username");
String userName = [Link](); // Read user input
[Link]("Username is: " + userName); // Output user input
}

Example 2
public static void main(String[] args) {
Scanner sc= new Scanner([Link]);
[Link]("Enter your age");
int age= [Link]();
[Link]("Age is "+age);
[Link]("\n");
[Link]("Enter your name");
String name=[Link]();
[Link]("Name is "+name);
}

REFERENCES
[Link]

You might also like