ICTQB Notes
ICTQB Notes
ICTQB Notes
There are three ways to access the package from outside the package:
// java.util and java.sql packages contain Date class, so it is better to use fully
qualified names. // Note: Subpackages are needed to be imported explicitly, as they
won't be part of parent package import.
In java, we use a specific format for defining the package names. Standard->
domain.company.package Example-> com.youtube.shashwat
Modifiers in Java
There are two types of modifiers in Java: access modifiers and non-access modifiers.
Access Specifiers
1. Private: The access level of a private modifier is only within the class. It cannot
be accessed from outside the class.
2. Default: The access level of a default modifier is only within the package. It
cannot be accessed from outside the package. If you do not specify any access
level, it will be the default. The default access modifier is also called package-
private.
3. Protected: The access level of a protected modifier is within the package and
outside the package through child class. If you do not make the child class, it
cannot be accessed from outside the package.
1. static - The static keyword in Java is mainly used for memory management.
The static keyword in Java is used to share the same variable or method of a
given class. The users can apply static keywords with variables, methods,
blocks, and nested classes. When a member is declared static, it can be
accessed before any objects of its class are created, and without reference to
any object.
A. Static Block - If you need to do the computation in order to initialize your static
variables, you can declare a static block that gets executed exactly once, when the
class is first loaded.
B. Static Variable - When a variable is declared as static, then a single copy of the
variable is created and shared among all objects at the class level. We can create
static variables at the class level only static block and static variables are executed in
the order they are present in a program.
C. Static Method - When a method is declared with the static keyword, it is known as
the static method. The most common example of a static method is the main()
method. Methods declared as static have several restrictions:
D. Static Classes - A class can be made static only if it is a nested class. We cannot
declare a top-level class with a static modifier but can declare nested classes as static.
Such types of classes are called Nested static classes. Nested static class doesn’t need
a reference of Outer class. In this case, a static class cannot access non-static
members of the Outer class.
2. final - final keyword is used in different contexts. First of all, final is a non-
access modifier applicable only to a variable, a method, or a class. Final
Variable - To create a constant variable. Final Methods - To prevent method
Overloading. Final Class - To prevent Inheritance
3. abstract - The abstract keyword is a non-access modifier, used for classes and
methods. An abstract class is a restricted class that cannot be used to create
objects (to access it, it must be inherited from another class). An abstract
method can only be used in an abstract class, and it does not have a body.
6. volatile - Using volatile is yet another way (like synchronized, atomic wrapper)
of making class thread-safe. Thread-safe means that a method or class
instance can be used by multiple threads at the same time without any
problem. volatile keyword here makes sure that the changes made in one
thread are immediately reflect in other thread
7. native - The native keyword in Java is applied to a method to indicate that the
method is implemented in native code. The methods which are implemented
in C, C++ are called native methods or foreign methods. native keyword can
only be applied to those methods.
control-flow
It is one of the fundamental features of Java that can be used to control the flow of
Java code.
a) if statements
The control of the program is diverted depending upon the specific condition.
1. Simple If
if(condition) {
statement 1; //executes when condition is true
}
2. If - Else
The else block is executed if the condition of the if-block is evaluated as false.
if(condition) {
statement 1; //executes when condition is true
}
else{
statement 2; //executes when condition is false
}
3. if-else-if ladder
if(condition 1) {
statement 1; //executes when condition 1 is true
}
else if(condition 2) {
statement 2; //executes when condition 2 is true
}
else {
statement 2; //executes when all the conditions are false
}
4. Nested if-statement
if(condition 1) {
if(condition 2)
{
statement 2; //executes when condition 2 is true
}
else
{
statement 2; //executes when condition 2 is false
}
}
b) switch statement
The switch statement contains multiple blocks of code called cases and a single case
is executed based on the variable which is being switched.
switch (expression){
case value1:
statement1;
break;
case value2:
statement2;
break;
default:
default statement; // yha per break laga kr kya faida bhai?
}
1. The case variables can be int, short, byte, char, String or enumeration.
2. Cases cannot be duplicate.
3. Default statement is executed when any of the case doesn't match the value of
expression. It is optional.
4. Break statement terminates the switch block when the condition is satisfied. It is
optional, if not used, next case is executed.
5. While using switch statements, we must notice that the case expression will be of the
same type as the variable. However, it will also be a constant value.
2. Loop statements
Sometimes we need to execute the block of code repeatedly while some condition
evaluates to true.
a) do while loop
The do-while loop checks the condition at the end of the loop after executing the
loop statements. When the number of iteration is not known and we have to execute
the loop at least once, we can use do-while loop.
It is also known as the exit-controlled loop since the condition is not checked in
advance. Syntax -> do
{
//statements
} while (condition);
b) while loop
The while loop is also used to iterate over the number of statements multiple times.
If we don't know the number of iterations in advance, it is recommended to use a
while loop. Unlike for loop, the initialization and increment/decrement doesn't take
place inside the loop statement in while loop.
It is also known as the entry-controlled loop since the condition is checked at the
start of the loop. If the condition is true, then the loop body will be executed.
c) for loop
d) for-each loop
Java provides an enhanced for loop to traverse the data structures like array or
collection. In the for-each loop, we don't need to update the loop variable.
3. Jump statements
Jump statements are used to transfer the control of the program to the specific
statements. In other words, jump statements transfer the execution control to the
other part of the program.
a) break statement
It is used to break the current flow of the program and transfer the control to the
next statement outside a loop or switch statement. Note that it breaks only the loop
where it is written.
The break statement cannot be used independently in the Java program, i.e., it can
only be written inside the loop or switch statement.
b) continue statement
Unlike break statement, the continue statement doesn't break the loop, whereas, it
skips the specific part of the loop and jumps to the next iteration of the loop
immediately.
The parameters that appear in the function definition are called formal parameters
and, the parameters that appear in the function call statement are called actual
parameters.
1. Predefined Method:
Predefined methods are the method that is already defined in the Java class libraries
is known as predefined methods. It is also known as the standard library method or
built-in method. We can directly use these methods just by calling them in the
program at any point.
2. User-defined Method:
It consists of the method name and a parameter list (number of parameters, type of
the parameters, and order of the parameters). Example
calculate(int a, int b)
1. While defining a method, remember that the method name must be a verb and start
with a lowercase letter.
2. If the method name has more than two words, the first name must be a verb followed
by an adjective or noun.
3. In the multi-word method name, the first letter of each word must be in uppercase
except the first word. For example, findSum, computeMax, setX and getX.
Methods calls are implemented through a stack. Whenever a method is called a stack
frame is created within the stack area and after that, the arguments passed to and
the local variables and value to be returned by this called method are stored in this
stack frame and when execution of the called method is finished, the allocated stack
frame would be deleted. There is a stack pointer register that tracks the top of the
stack which is adjusted accordingly.
In Java, all primitives like int, char, etc are pass by value. but all non-primitives (or
objects of any class) are always references.
Java creates a copy of references and pass it to method, but they still point to same
memory reference.
When there are two or more than two methods in a class that have the same name
but different parameters, it is known as method overloading. Java allows a function
to have the same name if it can distinguish them by their number and type of
arguments.
Types of instance methods (non-static):
1. Accessor Method
2. Mutator Method
The method(s) that reads the instance variable(s) is known as the accessor method.
They are also known as getter. The method(s) read the instance variable(s) and also
modify the values are known as the mutator methods. They are also known as setter