Java Basics Pdf2
Java Basics Pdf2
Automated testing can help you ensure that your web application or website is fully
In addition, automated testing saves you time because you don’t need to test the same
It is more reliable, as it reduces the occurrence of errors. It is reliable because it tests the
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.
Create a Class
To create a class, use the keyword class:
Main.java
Create a class named "Main" with a variable x:
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:
int x = 5;
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
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
Example
Create a constructor:
public Main() {
Main myObj = new Main(); // Create an object of class Main (This will call the constructor)
// 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;
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
Vehicle is running
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 {
Output
add() with 2 parameters
10
add() with 3 parameters
17
Inheritance in Java
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...
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
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.
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
}
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
}
In nested if-statements, the if statement can contain a if or if-else statement inside another if or else-if statement.
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.
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.
Output:
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
}
Output:
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);
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.
Private Y N N N
Default Y Y N N
Protected Y Y Y N
Public Y Y Y Y