ICTQB Notes

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Java Packages?

A java package is a group of similar types of classes, interfaces and sub-packages.


Package in java can be categorized in two form, built-in package and user-defined
package. There are many built-in packages such as java, lang, awt, javax, swing, net,
io, util, sql etc. It provides access protection. It removes naming collision.

There are three ways to access the package from outside the package:

1. import package.*; // classes and interfaces will be accessible but not


subpackages.
2. import package.classname; //only declared class of this package will be
accessible.
3. fully qualified name. //only declared class of this package will be accessible
and there is no need to import

// 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.

Sequence that needs to be followed:

1. first declare package


2. then import the package
3. then declare your class
System is available in java lang package.

In java, we use a specific format for defining the package names. Standard->
domain.company.package Example-> com.youtube.shashwat

Note: lang package is imported by default.

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.

4. Public: The access level of a public modifier is everywhere. It can be accessed


from within the class, outside the class, within the package and outside the
package.

Non Access Specifiers


In java, we have 7 non-access modifiers. They are used with classes, methods,
variables, constructors, etc to provide information about their behavior to JVM.

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.

int a=2; int b=0;

// static block static { System.out.println("Static block initialized."); b = a * 7 + 2; }

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:

1. They can only directly call other static methods.


2. They can only directly access static data.
3. They cannot refer to "this" or "super" in any way.

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.

4. synchronized - Multi-threaded programs may often come to a situation where


multiple threads try to access the same resources and finally produce
erroneous and unforeseen results. If we want only one thread can access the
resource at a given point in time then we use synchronized keyword.

5. transient - transient is a variables modifier used in serialization. At the time of


serialization, if we don’t want to save value of a particular variable in a file,
then we use transient keyword. When JVM comes across transient keyword, it
ignores original value of the variable and save default value of that variable
data type. Practically we serialized only those fields which represent a state of
instance, after all serialization is all about to save state of an object to a file. It
is good habit to use transient keyword with private confidential fields of a
class during serialization.

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.

Java provides three types of control flow statements.

1. Decision Making statements

decision-making statements decide which statement to execute and when. It


evaluates the Boolean expression. There are two types of decision-making
statements in Java, i.e., If statement and switch statement.

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

The if-else-if statement contains the if-statement followed by multiple else-if


statements. It is the chain of if-else statements that create a decision tree where the
program may enter in the block of code where the condition is true. We can also
define an else statement at the end of the chain.

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

The if statement can contain a if or if-else statement inside another if or else-if


statement.

if(condition 1) {

statement 1; //executes when condition 1 is true

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?
}

Rules of using a switch case

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.

Example is traversing a linked list (size is unknown) Syntax -> while(condition){


}

c) for loop

It enables us to initialize the loop variable, check the condition, and


increment/decrement in a single line of code. We use the for loop only when we
exactly know the number of times, we want to execute the block of code.

example is iteration of an array, where size can be determined. Syntax ->


for(initialization, condition, increment/decrement) {
}

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.

Syntax -> for(data_type var : array_name){


}

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.

(Methods) Functions in Java


Method in Java or Java Method is a block of code that perform some specific task
and it may or may not return the result to the caller. Method in java should be part of
some class.

Method syntax: <access_modifier> <return_type>


<method_name>(list_of_parameters) { //function body }

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.

Main advantage is "Code Reusability".

The Exception list will be covered in Exception Handling.

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:

The method written by the user or programmer is known as a user-defined method.


These methods are modified according to the requirement.
Method Signature

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.

A method needs to be called..

It returns to the code that invoked (called) it when:

1. It completes all the statements in the method


2. It reaches a return statement
3. Throws an exception

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.

Method Overloading in Java

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

You might also like