Selenium – Basics of Java Part 2

Language and Syntax Basics

Variables

The Java programming language is statically-typed, which means that all variables must first declared before they can be used.

Example – int gear=1;

Data Type Default Value
Byte 0
Short 0
Int 0
Long 0L
Float 0.0f
Double 0.0d
Char \u0000′
String Null
Boolean FALSE

The Java programming language defines the following kinds of variable:

  • Instance Variables (Non-Static Fields)  – Variable declared without the status keyword called non-static fields and also known as instance variables. Example – object
  • Class Variables (Static Fields)A class variable is any field declared with the static modifier.
  • Local Variables – Similar to how an object stores its state in fields, a method will often store its temporary state in local variables.
  • Parameters – A parameter is a variable that is passed to a method when the method is called.

Arrays

An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.

Example

Class Array {
public static void main(String[] args) {
//declares an array of integers
int[] intArray;
//allocates memory for 5 integers
intArray = new int[5];
//Initialize first element
intArray[0] = 50;
//Initialize second element
intArray[0] = 100;
//etc
SYstem.out.println(“Element at index 0:” + intArray[0]);
}
}

Similarly, you can declare arrays of other types:

byte[] arrayOfBytes;

short[]  arrayOfShorts;

long[]  arrayOfLongs;

String[]  arrayOfStrings;

//etc  (arrayVariable.length will return the size of array)

Operators

Operators are special symbols that perform specific operations on one, two or three operands, and then return a result. The following is a reference for operators supported by the java programming language.

Simple Assignment Operator

=  Simple assignment operator

Arithmetic Operators

+  Addition operator

–  Subtraction operator

*  Multiplication operator

/  Division operator

%  Remainder operator

Unary Operators

+    Unary plus operator

–      Unary minus operator

++  Increment operator

—    Decrement operator

!     Logical complement operator

Equality and Relational Operators

==  Equal to

!=   Not Equal to

>    Greater than

>=  Greater than or equal to

<     Less than

<=   Less than or equal to

Conditional Operators

&&  Conditional AND

||   Conditional OR

?     Ternary

Type Comparison Operator

instanceof    Compares an object to a specified type

Control Flow Statements

This section describes the decision-making statements (if, if-else, switch), the looping statements (for, while, do-while) and the branching statements (break, continue, return) supported by the Java programming language.

The If-elseif- else Statement 

if (condition A) { //do this }

elseif (condition B) { //do this }

else { //do this }

The control will go to one of the block of statements depending upon the given condition.

public class FlowControl {
public static void main(String[] args) {
int age = 14;
System.out.println(“Peter is ” + age + ” years old”);

if (age < 4) {
System.out.println(“Peter is a baby”);
} else if (age >= 4 && age < 14) {
System.out.println(“Peter is a child”);
} else if (age >= 14 && age < 18) {
System.out.println(“Peter is a teenager”);
} else if (age >= 18 && age < 68) {
System.out.println(“Peter is adult”);
} else {
System.out.println(“Peter is an old men”);
}
}
}

The switch Statement

In some cases you can avoid using multiple if-s in your code and make your code look better. For this you can use the switch statement. Look at the following java switch example

public class SwitchExample {
public static void main(String[] args) {
int numOfAngles = 3;

switch (numOfAngles) {
case 3:
System.out.println(“triangle”);
break;
case 4:
System.out.println(“rectangle”);
break;
case 5:
System.out.println(“pentagon”);
break;
default:
System.out.println(“Unknown shape”);
}
}
}

For Loop

for loop is very powerful and yet simple to learn. It is mostly used in searching or sorting algorithms as well in all cases where you want to iterate over collections of data. Here is a simple example of a “for” loop:

public class ForLoopExample {
public static void main(String[] args) {
for(int i=0; i<5; i++) {
System.out.println(“Iteration # ” + i);
}
}
}

while Loop

The general form of the while loop can be expressed as follows:

while(condition) {
// execute code here
}

Condition is Boolean. This means until the condition is true the while loop will be executed. I will recreate our first example this time using while loop instead of for loop.

public class WhileLoopExample {
public static void main(String[] args) {
int i=0;
while(i<5) {
System.out.println(“Iteration # ” + i);
i++;
}
}
}

Do While Loop

The Java Programming language also provides a do-while statements, which can be expressed as follows:

do {

statement(s)

} while (expression);

The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once, as shown in the following DoWhileLoopExample program.

public class DoWhileLoopExample {
public static void main(String[] args) {
int i=1;
do {
System.out.println(“Iteration # ” + i);
i++;
}while(i<5);
}
}

The Return Statement

The return statement exits from the current method, and control flow returns to where the method was invoked. The return statement has two forms: one that returns a value, and one that doesn’t. To return a value, simply put the value after the return keyword.

Example – return count;

The data type of the returned value must match the type of the method’s declared return value. When a method is declared void, return doesn’t return a value or doesn’t have a return statement at all.

Java Keywords

The following list shows the reserved words in java. These reserved words may not be used as a constant or variable or any other identifier name.

abstract continue for new
switch assert default goto
package synchronized  boolean do
if private this break
double implements protected throw
byte else import public
throws case enum instanceof
return transient catch extends
int short try char
final interface static void
class finally long strictfp
volatile const float native
super while

Leave a Reply Cancel reply