G C Reddy Technologies ([Link].
com)
Selenium Class 6 - Java Program
Structure
i) Java Program Structure
ii) Java Sample Program
----------------------------------------
i) Java Program Structure
1) Documentation Section
> It includes the comments to tell the program's purpose, it improves the
readability of the program
2) Package Statement
> It includes statement that provides a package declaration
3) Import Statement/s
We import predefined and user defined libraries using "import" keyword
Ex:
import [Link];
java - Project
io - Package
Console - Class
G C Reddy Technologies ([Link])
G C Reddy Technologies ([Link])
import [Link].*;
java - Project
io - Package
io.* - import all classes from io package
--------------------------------------------
Predefined/Built-in,
All libraries in Java are predefined, but a few libraries only automatically
loaded in every Java program.
4) Class Definition
Ex:
public class Sample{
.
}
5) Interface Section
It includes method declaration
6) main Method (java program execution starts from main method)
public static void main (String [] args){
....
}
public - Access Modifier
static - Non Access Modifier (use main Method without invoking any Object)
void - Returns nothing
G C Reddy Technologies ([Link])
G C Reddy Technologies ([Link])
main - Method name
(String [] args) -?
------------------------------------------
7) Declaration Statement/s
We declare Variables and Constants
int a;
a=100;
int b=200;
b=300;
c=400;
final int y=1000; (Constant)
y=2000; //Incorrect
-----------------------------------------
Variables vs. Constants
int a;//Correct
a=10;
a=30;
int b=200;
b=400;
-------------
final int x; //Incorrect
G C Reddy Technologies ([Link])
G C Reddy Technologies ([Link])
final int y=300; //Correct
y=300; //Incorrect
----------------------------
8) Normal Statements
c=a+b;
[Link]("Hello");
System - Predefined Class
out - Object
println - Method
"Hello" - Message
----------------------------
9) Code Blocks
Conditions,
Loops,
Methods, etc...
--------------------------------
10) Object Creation Statement
Note 1: We can create Object at beginning of the program or middle of the
program or end of the program
Note 2: Usually we create Object/Instance of the Class within main method,
but we can also create Objects outside of the main method
Syntax:
ClassName objectName= new ClassClassName();
----------------------------------------
G C Reddy Technologies ([Link])
G C Reddy Technologies ([Link])
ii) Java Sample Program
//It is a Sample Program to Understand the Java program Structure and
Syntax.
package abcd;
public class Sample {
//Create a Method with Arguments and return a value (Non Static method)
public int add(int a, int b){
int result;
result=a+b;
return result;
}
//Create a method without Arguments and returns nothing (Non Static
method)
public void comparison(){
int x=100, y=20;
if (x>y){
[Link]("X is a Big Number");
}
else{
[Link]("Y is a Big Number");
}
}
//Create a Method with Arguments and return a value (Static method)
public static int sub(int a, int b){
G C Reddy Technologies ([Link])
G C Reddy Technologies ([Link])
int result=a-b;
return result;
}
//Create a Method without and returns nothing (Static method)
public static void comparision2(){
int a=100, b=200;
if (a>b){
[Link]("A is a Big Number");
}
else{
[Link]("B is a Big Number");
}
}
public static void main (String [] args){
//Create Object to call Non Static methods
Sample obj = new Sample();
int res = [Link](100, 200);
[Link](res);//300
//Or
[Link]([Link](100, 200));//300
[Link]();//X is a Big Number
//Call Static Methods using Class name
res = [Link](100, 50);
[Link](res);//50
G C Reddy Technologies ([Link])
G C Reddy Technologies ([Link])
//Or
[Link]([Link](200, 100));//100
Sample.comparision2();//B is a Big Number
//Call Static Methods without using Class name
int x= sub(10, 5);
[Link](x);//5
[Link](sub(20,10));//10
comparision2();//B is a Big Number
int a;//Variable Declaration
a=100; //Initialization
int b=200; //Variable Declaration with Initialization
int c, d, e; //Declare multiple variables
int f=40, g=50, h=60; //Declare multiple variables with initialization
double l=123.45678;
char m='*';
boolean p=true;
String q="Selenium Testing";
[Link](q);//Selenium Testing
[Link](l);//123.45678
[Link]("Hello Java");
G C Reddy Technologies ([Link])
G C Reddy Technologies ([Link])
final int price =100;
[Link](price);
if (a>b){
[Link]("A is a Big Number");
}
else
{
[Link]("B is a Big Number");
}
char grade ='U';
switch (grade){
case 'A':
[Link]("Excellent");
break;
case 'B':
[Link]("Good");
break;
case 'C':
[Link]("Better");
break;
default:
[Link]("Invalid Grade");
}
G C Reddy Technologies ([Link])
G C Reddy Technologies ([Link])
//Print 1 to 5 Numbers except 4 using for loop
for (int i=1; i<=5; i++){
if (i != 4) {
[Link](i);
}
}
//Print 1 to 5 numbers using while loop
int j=10;
while (j<=15){
[Link](j);
j++;
}
//do while loop
int k=100;
do
{
[Link](k);
k++;
} while (k<=8);
//Enhanced for loop
String [] tools ={"Selenium", "UFT", "RFT", "SilkTest"};
for (String mytool: tools){
[Link](mytool);
G C Reddy Technologies ([Link])
G C Reddy Technologies ([Link])
}
}
}
---------------------------------------------------------
G C Reddy Technologies ([Link])