Java
A. Java
Classes and Objects
Figure 1.0 - Java Classes and 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.
● A Class is like an object constructor, or a "blueprint" for creating objects.
● 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:
public class Main {
int x = 5;
public static void main(String[] args) {
Main myObj = new Main();
System.out.println(myObj.x);
}
public class Main {
int x = 5;
public static void main(String[] args) {
Main myObj1 = new Main(); // Object 1
Main myObj2 = new Main(); // Object 2
System.out.println(myObj1.x);
System.out.println(myObj2.x);
}
Data Types
Figure 1.0 - Data Types
Figure 1.1 - Primitive Data Types
Figure 1.2 - Primitive vs Non-primitive Data Types
● Ref: https://docs.oracle.com/en/java/javase/14/docs/api/java.base/module-summary.html
String Literal v.s. String Object
Figure 1.0 - Equals() Method
Figure 1.1 - String Literal vs. String Object
● The equals() method compares two strings, and returns true if the strings are equal, and
false if not.
Variables
Figure 1.0 - Variables
● Plus (+) Operator used to concatenate variables in Java.
Operators
Figure 1.0 - Arithmetic Operators
Figure 1.1 - Precedence and Associativity of Operators
● Precedence and associative rules are used when dealing with hybrid equations involving
more than one type of operator. In such cases, these rules determine which part of the
equation to consider first, as there can be many different valuations for the same
equation. The below table depicts the precedence of operators in decreasing order as
magnitude, with the top representing the highest precedence and the bottom showing
the lowest precedence. Ref: https://www.geeksforgeeks.org/operators-in-java/
IF ELSE
Figure 1.0 - If Else
Figure 1.1 - Multiple Else If
Ternary Operator
Figure 1.0 - Ternary Operator
Switch Case
Figure 1.0 - Switch Case
Figure 1.1 - Switch Case with Break
Figure 1.2 - Switch Case with Default
● Note: If the default statement is used as the last statement in a switch block, it does not
need a break.
● When Java reaches a break keyword, it breaks out of the switch block.
● This will stop the execution of more code and case testing inside the block.
● When a match is found, and the job is done, it's time for a break. There is no need for
more testing.
● A break can save a lot of execution time because it "ignores" the execution of all the rest
of the code in the switch block.
While Loop
Figure 1.0 - While Loop
Figure 1.1 - While Loop Sample
Do While Loop
Figure 1.0 - Do While Loop
● The Java do-while loop is used to iterate a part of the program repeatedly, until the
specified condition is true. If the number of iterations is not fixed and you must have to
execute the loop at least once, it is recommended to use a do-while loop.
● Java do-while loop is called an exit control loop. Therefore, unlike while loop and for
loop, the do-while checks the condition at the end of loop body. The Java do-while loop
is executed at least once because the condition is checked after the loop body.
Figure 1.1 - Do While Loop Sample 1
Figure 1.2 - Do While Loop Sample 2
For Loop
Figure 1.0 - For Loop Sample
Break and Continue
● You have already seen the break statement used in an earlier chapter of this tutorial. It
was used to "jump out" of a switch statement.
● The break statement can also be used to jump out of a loop.
Figure 1.0 - Break in Loop
● The continue statement breaks one iteration (in the loop), if a specified condition occurs,
and continues with the next iteration in the loop.
Figure 1.1 - Continue in Loop
Nested Loops
● It is also possible to place a loop inside another loop. This is called a nested loop.
● The "inner loop" will be executed one time for each iteration of the "outer loop":
Figure 1.0 - Nested Loop
Single Dimension Array
Figure 1.0 - Single Dimension Array
Multidimensional Array
● A multidimensional array is an array of arrays.
● Multidimensional arrays are useful when you want to store data as a tabular form, like a
table with rows and columns.
● To create a two-dimensional array, add each array within its own set of curly braces:
Figure 1.0 - Multidimensional Array
● myNumbers is now an array with two arrays as its elements.
Figure 1.1 - Multidimensional Array 2
Figure 1.2 - Loop through Multidimensional Array
Methods
● A method is a block of code which only runs when it is called.
● You can pass data, known as parameters, into a method.
● Methods are used to perform certain actions, and they are also known as functions.
● Why use methods? To reuse code: define the code once, and use it many times.
● To call a method in Java, write the method's name followed by two parentheses () and a
semicolon;
● In the following example, myMethod() is used to print a text (the action), when it is called:
Figure 1.0 - Method
Parameters and Arguments
● Information can be passed to methods as parameters. Parameters act as variables
inside the method.
● Parameters are specified after the method name, inside the parentheses. You can add
as many parameters as you want, just separate them with a comma.
● The following example has a method that takes a String called fname as parameter.
When the method is called, we pass along a first name, which is used inside the method
to print the full name:
Figure 1.0 - Parameters
Packages
● Ref: www.selenium.dev/selenium/docs/api/java/index.html
Figure 1.0 - Packages
Figure 1.1 - Package Doc Selenium
What is POJO?
Figure 1.0 - What is Pojo
Figure 1.1 - Example of POJO
What is JavaBean?
Figure 1.0 - What is JavaBean
Figure 1.1 - Example of JavaBean
● Serializable is just a label for java that tells java that this class can be written to things
like databases and files.
B. Command-Line Arguments
● Reference:
https://web.mit.edu/6.031/www/fa17/projects/fb1/commandline.html#:~:text=%2Dcp%20s
pecifies%20the%20classpath%20where,jar%20and%20lib%2Fphysics.
● https://web.mit.edu/6.031/www/fa17/