Java Program Example as well as Comments inwards Java Programs
i) Java Program Example
ii) Comments inwards Java
--------------------------------------------
i) Java Program Example
//It is a Java Program Example to Understand the Java Syntax as well as Program Structure
package javaExamples;
public degree SampleProgram {
//Create a Method amongst Parameters as well as returning value, Access the method yesteryear invoking the object
public int add(int a, int b){
int effect = a+b;
return result;
}
//Create a Method amongst No Parameters as well as Reruns Nothing, Access the method yesteryear invoking the object
public void comparison(){
int a=100, b=50;
if (a > b){
System.out.println("A is a Big Number");
}
else
{
System.out.println("B is a Big Number");
}
}
//Create a Method amongst Parameters, returning a value as well as access the method without Object
public static int sub(int a, int b){
int effect = a -b;
return result;
}
//Create a Method amongst No Parameters, Returns Nothing as well as access the method without Object
public static void comparison2(){
int a=100, b=500;
if (a > b){
System.out.println("A is a Big Number");
}
else
{
System.out.println("B is a Big Number");
}
}
public static void main(String[] args) {
int a; //Variable Declaration
a=10; //Initialization
int b=20; //Variable Declaration as well as Initialization
double c = 1.234;
int d, e, f; //Declaration of Multiple Variables
int g =30, h=40, k=50;//Declaration of Multiple Variables as well as Initialization
int l = a + b;
System.out.println("Addition of a, b is: "+l);//30
System.out.println(k);//50
k=123;
System.out.println(k);//123
final int abc = 100;//Constant Declaration
System.out.println(abc);//100
if (a > b){
System.out.println("A is a Big Number");
}
else {
System.out.println("B is a Big Number");
}
String mytool = "Selenium Testing";
System.out.println(mytool);
char degree = 'A';
switch (grade){ //Decide amid several Alternates
case 'A':
System.out.println("Excellent");
break;
case 'B':
System.out.println("Good");
break;
case 'C':
System.out.println("Better");
break;
default:
System.out.println("Invalid Grade");
}
for (int i = 1; i <= 10; i++){ //Repetitive Execution
if (i != 7){
System.out.println(i);
}
}
int j=5; //Display i to five Numbers using piece loop
while(j>=1){
System.out.println(j);
j--;
}
int thousand =20;
do
{
System.out.println(m);
m++;
} piece (m<=6);
String [] tools = {"Selenium","UFT", "RFT", "SilkTest"};
System.out.println(tools.length);
for (String xyz: tools){
System.out.println(xyz);
}
//Create Object
SampleProgram obj = novel SampleProgram();
int res = obj.add(123, 321);
System.out.println(res);
System.out.println(obj.add(123, 321));
obj.comparison();
//Access Static methods using Class Name
int res2 = SampleProgram.sub(200, 123);
System.out.println(res2);
System.out.println(SampleProgram.sub(200, 123));
SampleProgram.comparison2();
}
}
------------------------------------------------------
ii) Comments inwards Java
Comments are English linguistic communication words used for Code Documentation.
1) Purpose of Comments
a) To brand the Code Readable
b) To brand the Code disable from Execution.
2) Comments Syntax inwards Java
a) Use // for Single Comment
b) Use /*.............
.....................
.....................
..................*/ for multiple delineate of piece of employment comment
Or
Comment Multiple Lines
(Eclipse IDE)
Select Statements/Steps
Source Menu -> Add Block Comment
Uncomment
(Eclipse IDE)
Select Comment block
Source Menu -> Remove Block Comment
---------------------------------------------------
3) Example:
public degree Sample2 {
public static void main(String[] args) {
int a, b; //Variables Declaration
a =10; b=20;
int c = a+b;
//Display the Addition value of a as well as b
System.out.println("Addition of a, b is: "+c);
System.out.println("");
/*if (a > b) {
System.out.println("A is a Big Number");
}
else
{
System.out.println("B is a Big Number") ;
}*/
}
}
------------------------------------------------
4) Usage of Comments inwards Test Automation
a) To Write Test instance header
b) To Write Method header
c) To Explain Complex Logic
---------------------------------------------
Java for Selenium Part 3 Link
i) Java Program Example
ii) Comments inwards Java
--------------------------------------------
i) Java Program Example
//It is a Java Program Example to Understand the Java Syntax as well as Program Structure
package javaExamples;
public degree SampleProgram {
//Create a Method amongst Parameters as well as returning value, Access the method yesteryear invoking the object
public int add(int a, int b){
int effect = a+b;
return result;
}
//Create a Method amongst No Parameters as well as Reruns Nothing, Access the method yesteryear invoking the object
public void comparison(){
int a=100, b=50;
if (a > b){
System.out.println("A is a Big Number");
}
else
{
System.out.println("B is a Big Number");
}
}
//Create a Method amongst Parameters, returning a value as well as access the method without Object
public static int sub(int a, int b){
int effect = a -b;
return result;
}
//Create a Method amongst No Parameters, Returns Nothing as well as access the method without Object
public static void comparison2(){
int a=100, b=500;
if (a > b){
System.out.println("A is a Big Number");
}
else
{
System.out.println("B is a Big Number");
}
}
public static void main(String[] args) {
int a; //Variable Declaration
a=10; //Initialization
int b=20; //Variable Declaration as well as Initialization
double c = 1.234;
int d, e, f; //Declaration of Multiple Variables
int g =30, h=40, k=50;//Declaration of Multiple Variables as well as Initialization
int l = a + b;
System.out.println("Addition of a, b is: "+l);//30
System.out.println(k);//50
k=123;
System.out.println(k);//123
final int abc = 100;//Constant Declaration
System.out.println(abc);//100
if (a > b){
System.out.println("A is a Big Number");
}
else {
System.out.println("B is a Big Number");
}
String mytool = "Selenium Testing";
System.out.println(mytool);
char degree = 'A';
switch (grade){ //Decide amid several Alternates
case 'A':
System.out.println("Excellent");
break;
case 'B':
System.out.println("Good");
break;
case 'C':
System.out.println("Better");
break;
default:
System.out.println("Invalid Grade");
}
for (int i = 1; i <= 10; i++){ //Repetitive Execution
if (i != 7){
System.out.println(i);
}
}
int j=5; //Display i to five Numbers using piece loop
while(j>=1){
System.out.println(j);
j--;
}
int thousand =20;
do
{
System.out.println(m);
m++;
} piece (m<=6);
String [] tools = {"Selenium","UFT", "RFT", "SilkTest"};
System.out.println(tools.length);
for (String xyz: tools){
System.out.println(xyz);
}
//Create Object
SampleProgram obj = novel SampleProgram();
int res = obj.add(123, 321);
System.out.println(res);
System.out.println(obj.add(123, 321));
obj.comparison();
//Access Static methods using Class Name
int res2 = SampleProgram.sub(200, 123);
System.out.println(res2);
System.out.println(SampleProgram.sub(200, 123));
SampleProgram.comparison2();
}
}
------------------------------------------------------
ii) Comments inwards Java
Comments are English linguistic communication words used for Code Documentation.
1) Purpose of Comments
a) To brand the Code Readable
b) To brand the Code disable from Execution.
2) Comments Syntax inwards Java
a) Use // for Single Comment
b) Use /*.............
.....................
.....................
..................*/ for multiple delineate of piece of employment comment
Or
Comment Multiple Lines
(Eclipse IDE)
Select Statements/Steps
Source Menu -> Add Block Comment
Uncomment
(Eclipse IDE)
Select Comment block
Source Menu -> Remove Block Comment
---------------------------------------------------
3) Example:
public degree Sample2 {
public static void main(String[] args) {
int a, b; //Variables Declaration
a =10; b=20;
int c = a+b;
//Display the Addition value of a as well as b
System.out.println("Addition of a, b is: "+c);
System.out.println("");
/*if (a > b) {
System.out.println("A is a Big Number");
}
else
{
System.out.println("B is a Big Number") ;
}*/
}
}
------------------------------------------------
4) Usage of Comments inwards Test Automation
a) To Write Test instance header
b) To Write Method header
c) To Explain Complex Logic
---------------------------------------------
Java for Selenium Part 3 Link