Java Notes 1
Java Notes 1
A. Java
● 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.
● 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:
int x = 5;
System.out.println(myObj1.x);
System.out.println(myObj2.x);
}
Data Types
● Ref: https://docs.oracle.com/en/java/javase/14/docs/api/java.base/module-summary.html
String Literal v.s. String Object
● The equals() method compares two strings, and returns true if the strings are equal, and
false if not.
Variables
IF ELSE
Ternary Operator
● 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
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
● 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 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":
Multidimensional Array
● 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
● 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:
● 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:
● Ref: www.selenium.dev/selenium/docs/api/java/index.html
What is POJO?
What is 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/