Java has three important editions
- Java Standard Edition/Core Java (J2SE)
- Java Enterprise Edition/Advanced Java(J2EE)
- Java Micro Edition (J2ME)
Note: Java Standard Edition or Core Java is enough for automation testing with Selenium
Java Program Structure
Documentation Section: It includes comments to tell the program’s purpose. It improves the readability of the programs.
Package Statement: It includes that provides a package declaration.
Import Statement: Built-in feature, Import libraries, External features…we import pre defined and user defined libraries using “import” keyword.
e.g. import java.io.console;
Here java= project, io= package, console= class
Interface Section: It includes method’s declaration.
Class Definition(Mandatory): e.g. public class Sample { }
Main Method: java program execution starts from main method, i.e.public static void main (Strings args []) { }
Here public= Access modifier, static= non access modifier, void= return nothing,main= Method name, (Strings args[])= The main() method also accepts some data from the user. It accepts a group of strings, which is called a string array. It is used to hold the command line arguments in the form of string values. (Note: if the main() method is written without String args[]? The program will compile, but not run, because JVM will not recognize the main() method. Remember JVM always looks for the main() method with a string type array as a parameter. )
Declaration Statement: We declare variables and constants. e.g.int a;
a= 100;final int b=100; (Constant)
Normal Statements: c= a+b;
Code Blocks: Condition, loops, method etc..
Object creation statements: We can create object at beginning of the program or anywhere in the program based on our requirement. We have to create object/instance of the class withing main method but we can create object’s outside of the main method also.
Java Sample Program
//It is a sample program to understand java program structure and syntax
package BasicProgram;
public class SampleProgram {
//Create a method with arguments and return a value(Non static method)
public int add (int a, int b) {
int result; //variable declaration
result = a+b; //Initialization
return result;
}
//Create a method without arguments and returns nothing (Non static method)
public void comparison() {
int x=100, y=150; //Declaring multiple variables
if (x>y) {
System.out.println("X is big number");
}
else {
System.out.println("Y is big number");
}
}
//Create a method with program with arguments and return a value (static method)
public static int sub (int a, int b) {
int result = a-b; //Variable declaration with initialization
return result; // Here we can call result variable again because it locally declare within method
}
//Create a method without arguments and returns nothing (static method)
public static void comparison2() {
int a=150, b=100;
if (a>b) {
System.out.println("A is big number");
}
else {
System.out.println("B is big number");
}
}
//Create a main method to execute above methods
public static void main(String[] args) {
//Create object to call non static methods
SampleProgram obj = new SampleProgram();
int res = obj.add(10, 20);
System.out.println(res);
obj.comparision();
//Call static methods
res = SampleProgram.sub(10, 5);
System.out.println(res);
SampleProgram.comparision2();
}
}