0% found this document useful (0 votes)
22 views

Java Basics Pdf2

Automated testing can help ensure a web application or website is fully functional and user-friendly by testing functionality repeatedly in an automated way, saving time compared to manual testing. It is more reliable than manual testing as it reduces errors by consistently testing with tools and scripts. In comparison to manual testing, automated testing requires fewer resources and enables load, performance, stress and reliability testing.

Uploaded by

Ponvel Selvam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Java Basics Pdf2

Automated testing can help ensure a web application or website is fully functional and user-friendly by testing functionality repeatedly in an automated way, saving time compared to manual testing. It is more reliable than manual testing as it reduces errors by consistently testing with tools and scripts. In comparison to manual testing, automated testing requires fewer resources and enables load, performance, stress and reliability testing.

Uploaded by

Ponvel Selvam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Automated Testing:

 Automated testing can help you ensure that your web application or website is fully

functional and user-friendly.

 In addition, automated testing saves you time because you don’t need to test the same

functionality manually repeatedly.

 It is more reliable, as it reduces the occurrence of errors. It is reliable because it tests the

application with the help of tools and test scripts .

 In comparison to manual testing, automation testing requires fewer resources.

 It makes load and performance testing, stress testing, and reliability testing possible.
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.

Create a Class
To create a class, use the keyword class:

Main.java
Create a class named "Main" with a variable x:

public class Main {

int x = 5;

}
Create an Object
In Java, an object is created from a class. We have already created the class named Main, so now we can use
this to create objects.

To create an object of Main, specify the class name, followed by the object name, and use the keyword new:

Example
Create an object called "myObj" and print the value of x:

public class Main {

int x = 5;

public static void main(String[] args) {

Main myObj = new Main();

System.out.println(myObj.x);

}
Java Data Types
 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

Data Type Size Description

char 2 bytes Stores a single character/letter or ASCII values

int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647

long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to


9,223,372,036,854,775,807

float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits

double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits

boolean 1 bit Stores true or false values


Java Constructors
A constructor in Java is a special method that is used to initialize objects. The constructor is called when an
object of a class is created. It can be used to set initial values for object attributes:

Example
Create a constructor:

// Create a Main class

public class Main {

int x; // Create a class attribute

// Create a class constructor for the Main class

public Main() {

x = 5; // Set the initial value for the class attribute x

public static void main(String[] args) {

Main myObj = new Main(); // Create an object of class Main (This will call the constructor)

System.out.println(myObj.x); // Print the value of x

// Outputs 5
Constructor Parameters
Constructors can also take parameters, which is used to initialize attributes.

The following example adds an int y parameter to the constructor. Inside the constructor we set x to y (x=y).
When we call the constructor, we pass a parameter to the constructor (5), which will set the value of x to 5:

Example
public class Main {

int x;

public Main(int y) {

x = y;

public static void main(String[] args) {

Main myObj = new Main(5);

System.out.println(myObj.x);

// Outputs 5
Polymorphism

Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by
inheritance

In Java polymorphism is mainly divided into two types:


 Compile-time Polymorphism
 Runtime Polymorphism

Method overloading is a compile-time polymorphism.


Method overriding is a run-time polymorphism.

Method Overriding in Java


If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in
Java.

//Java Program to demonstrate why we need method overriding


//Here, we are calling the method of parent class with child
//class object.
//Creating a parent class
class Vehicle{
void run(){System.out.println("Vehicle is running");}
}
//Creating a child class
class Bike extends Vehicle{
public static void main(String args[]){
//creating an instance of child class
Bike obj = new Bike();
//calling the method with child class instance
obj.run();
}
}
OUTPUT:

Vehicle is running

Method Overloading in Java

If a class has multiple methods having same name but different in parameters, it is known as Method Overloading
Example of Method Overloading:
import java.io.*;

class MethodOverloadingEx {

static int add(int a, int b)


{
return a + b;
}

static int add(int a, int b, int c)


{
return a + b + c;
}
public static void main(String args[])
{
System.out.println("add() with 2 parameters");
System.out.println(add(4, 6));

System.out.println("add() with 3 parameters");


System.out.println(add(4, 6, 7));
}
}

Output
add() with 2 parameters
10
add() with 3 parameters
17

Inheritance in Java

In Java, inheritance means creating new classes based on existing ones.

How to use inheritance in Java?


The extends keyword is used for inheritance in java. Using the extends keyword indicates you are derived from
an existing class. In other words, “extends” refers to increased functionality.
The syntax of Java Inheritance

class Subclass-name extends Superclass-name


{
//methods and fields
}

Multilevel Inheritance Example


When there is a chain of inheritance, it is known as multilevel inheritance. As you can see in the example given below,
BabyDog class inherits the Dog class which again inherits the Animal class, so there is a multilevel inheritance.

class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class BabyDog extends Dog{
void weep(){System.out.println("weeping...");}
}
class TestInheritance2{
public static void main(String args[]){
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}}

Output:

weeping...
barking...
eating...

Java Control Statements


Java provides three types of control flow statements.

1. Decision Making statements


o if statements
o switch statement
2. Loop statements
o do while loop
o while loop
o for loop
Decision-Making statements:

1) If Statement:
In Java, the "if" statement is used to evaluate a condition. The control of the program is diverted depending upon the specific
condition. The condition of the If statement gives a Boolean value, either true or false. In Java, there are four types of if-
statements given below.

1. Simple if statement
2. if-else statement
3. if-else-if ladder
4. Nested if-statement

Let's understand the if-statements one by one.

(1) Simple if statement:

It is the most basic statement among all control flow statements in Java. It evaluates a Boolean expression and enables the
program to enter a block of code if the expression evaluates to true.

Syntax of if statement is given below.

if(condition) {
statement 1; //executes when condition is true
}
(2) if-else statement

The if-else statement is an extension to the if-statement, which uses another block of code, i.e., else block. The else block is
executed if the condition of the if-block is evaluated as false.

Syntax:

if(condition) {
statement 1; //executes when condition is true
}
else{
statement 2; //executes when condition is false
}

(3) if-else-if ladder:

The if-else-if statement contains the if-statement followed by multiple else-if statements. In other words, we can say that it
is the chain of if-else statements that create a decision tree where the program may enter in the block of code where the
condition is true. We can also define an else statement at the end of the chain.
Syntax of if-else-if statement is given below.

if(condition 1) {
statement 1; //executes when condition 1 is true
}
else if(condition 2) {
statement 2; //executes when condition 2 is true
}
else {
statement 2; //executes when all the conditions are false
}

(4). Nested if-statement

In nested if-statements, the if statement can contain a if or if-else statement inside another if or else-if statement.

Syntax of Nested if-statement is given below.

if(condition 1) {
statement 1; //executes when condition 1 is true
if(condition 2) {
statement 2; //executes when condition 2 is true
}
else{
statement 2; //executes when condition 2 is false
} }
Switch Statement:
The switch statement contains multiple blocks of code called cases and a single case is executed based on the variable which is being
switched.

Consider the following example to understand the flow of the switch statement.

public class Student implements Cloneable {


public static void main(String[] args) {
int num = 2;
switch (num){
case 0:
System.out.println("number is 0");
break;
case 1:
System.out.println("number is 1");
break;
default:
System.out.println(num);
}
}
}

Output:

2
Loop Statements

1. for loop
2. while loop
3. do-while loop

for loop
In Java, for loop is similar to C and C++. It enables us to initialize the loop variable, check the condition, and
increment/decrement in a single line of code. We use the for loop only when we exactly know the number of times, we want
to execute the block of code.

The syntax of the For loop is given below.

for(initialization, condition, increment/decrement) {


//block of statements
}
Consider the following example to understand the proper functioning of the for loop in java.

public class Calculattion {


public static void main(String[] args) {
// TODO Auto-generated method stub
int sum = 0;
for(int j = 1; j<=10; j++) {
sum = sum + j;
}
System.out.println("The sum of first 10 natural numbers is " + sum);
}
}

Output:

The sum of first 10 natural numbers is 55

while loop
The while loop is also used to iterate over the number of statements multiple times. However, if we don't know the number
of iterations in advance, it is recommended to use a while loop. Unlike for loop, the initialization and increment/decrement
doesn't take place inside the loop statement in while loop.
The syntax of the while loop is given below.

while(condition){
//looping statements
}

Consider the following example.

public class Calculation {


public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 0;
System.out.println("Printing the list of first 10 even numbers \n");
while(i<=10) {
System.out.println(i);
i = i + 2;
}
}
}

Output:

Printing the list of first 10 even numbers


0
2
4
6
8
10
do-while loop
The do-while loop checks the condition at the end of the loop after executing the loop statements. When the number of
iteration is not known and we have to execute the loop at least once, we can use do-while loop.

It is also known as the exit-controlled loop since the condition is not checked in advance. The syntax of the do-while loop
is given below.

do
{
//statements
} while (condition);

Access Modifiers in Java


There are four types of Java access modifiers:

1. Private: The access level of a private modifier is only within the class. It cannot be accessed from outside the class.
2. Default: The access level of a default modifier is only within the package. It cannot be accessed from outside the
package. If you do not specify any access level, it will be the default.
3. Protected: The access level of a protected modifier is within the package and outside the package through child
class. If you do not make the child class, it cannot be accessed from outside the package.
4. Public: The access level of a public modifier is everywhere. It can be accessed from within the class, outside the class,
within the package and outside the package.
Understanding Java Access Modifiers
Let's understand the access modifiers in Java by a simple table.

Access within within outside package by outside


Modifier class package subclass only package

Private Y N N N

Default Y Y N N

Protected Y Y Y N

Public Y Y Y Y

You might also like