OOP in Java Unit3
OOP in Java Unit3
Whitespace: These are the spaces that are used to separate tokens in the
source code such as spaces, tabs and newlines. Proper use of whitespace
improves the readability of the code.
Examples of whitespace:
• Space: Used to separate keywords, variables, operators etc.
int a = 5; //spaces between keyword ‘int’, variable ‘a’, operator ‘=’ and
literal ‘5’.
• Tab: Used for indentation and make the code more readable.
• Newlines: Used to separate statements and make the code readable.
Separators: These are the symbols that are used to separate different parts of
the code and define the structure. Some of the commonly used separators are
as follows:
• Parentheses ( )
• Braces { }
• Brackets [ ]
• Semicolon ;
• Comma ,
• Period .
Comments: These the statements that are not executed by the compiler and
are used to provide information about the lines of code. They make the
program easier to read and understand. They are widely used for documenting
code.
1
By: Manoj Sapkota, Nepal Polytechnic Institute
(b) multi-line comments: These comments start with /* and ends with */.
Any text between /* and */ will be ignored by the compiler. Multiple lines
of comments can be placed inside them.
For example:
/* This is a multiple line comment.
It will print Hello World. */
System.out.println(“Hello World.”);
Identifiers: These are the names that are used to identify variables, methods,
classes, packages, and other entities in the code. They must follow specific
rules:
• An identifier must start with a letter (A-Z or a-z), currency character ($),
or an underscore (_).
• After the first character, identifiers can contain letters, digits (0-9),
currency characters, and underscores.
• Identifiers are case-sensitive.
• Identifiers cannot be Java keywords.
Keywords: These are the reserved words that have a predefined meaning in the
language syntax. Their meaning cannot be changed and cannot be used as
identifiers.
Examples of keywords: int, double, if, else, while, for, switch, public, private,
protected, return, void, static, final, class, interface, extends, implements etc.
2
By: Manoj Sapkota, Nepal Polytechnic Institute
Literals: These are the data that are used for representing fixed values. These
are the constant values that are assigned to the variables.
Data types: It is a classification that specifies the type of data that can be
stored in variables. All the variables must be declared with their data types
before they can be used.
Categories of Data types: There are two categories of data types. They are:
(a) Primitive data types
(b) Non-primitive data types
3
By: Manoj Sapkota, Nepal Polytechnic Institute
(a)Primitive data types: These are the basic data types provided by Java. They
represent only single values and are stored directly in memory.
1. byte type:
• Range: -128 to 127 (8-bit signed integer)
• It is used instead of ‘int’ to save memory, if it is certain that the value
of a variable falls within its range.
• Default value: 0
• Example: byte marks =70;
2. short type:
• Range: -32768 to 32767 (16-bit signed integer)
• It is used instead of other types such as ‘int’ or ‘long’, if it is certain
that the value of a variable falls within its range.
• Default value: 0
• Example: short speed = 150;
3. int type:
• Range: -231 to 231-1 (32-bit signed integer)
• It is generally used to store integers (whole numbers).
• Default value: 0
• Example: int balance = 150000;
4
By: Manoj Sapkota, Nepal Polytechnic Institute
4. long type:
• Range: -263 to 263-1 (64-bit signed integer)
• It is generally used to store integers (whole numbers) of long size.
• Default value: 0
• Example: long phone = 9855078422;
5. float type:
• It is used to store floating-point numbers.
• It is a single-precision 32-bit floating-point.
• Default value: 0.0 (0.0f)
• Example: float kg = 2.5;
6. double type:
• It is used to store floating-point numbers with high precision.
• It is a double-precision 64-bit floating-point.
• Default value: 0.0 (0.0d)
• Example: double price = 2.25;
7. char type:
• It is a 16-bit unicode character.
• Default value: ‘\u0000’
• It is used to store a single character, such as ‘A’ or ‘1’.
• Example: char c = ‘A’;
8. boolean type:
• It has two possible values, either true or false.
• Default value: false
• It is generally used for true/false conditions.
• Example: boolean flag = true;
(b) Non-primitive data types: These are also known as reference types. These
are types that are not predefined in the Java language. They are created by
programmers. Non-primitive data types can store multiple values or complex
objects and include arrays, classes, interfaces etc.
5
By: Manoj Sapkota, Nepal Polytechnic Institute
Data type conversion: It is the process of converting one data type into
another. There are mainly two types of data type conversion in Java. They are:
1. Automatic (Implicit) type conversion
2. Manual (Explicit) type conversion
The automatic data type conversion takes place from left side to right side
types as shown in figure above.
Operators: These are the special symbols that perform specific operations on
operands and then provide a certain result. They allow to perform arithmetic
and logical operations, compare values, manipulate bits etc.
Examples: int sum = a + b; /// Here, ‘=’ and ‘+’ are operators
boolean r = (a>b); /// Here, ‘>’ is an operator
6
By: Manoj Sapkota, Nepal Polytechnic Institute
Types of operators:
1. Arithmetic operators: These operators are used to perform basic
arithmetic operators. These operators are: +, - , *, / , %
Examples:
int sum= a+b;
int result= a*b;
7
By: Manoj Sapkota, Nepal Polytechnic Institute
Strings: These are the objects that represent sequences of characters. In Java,
the String class is used to create and manipulate strings.
2. Using the new Keyword: Strings can be created by using the new
keyword.
Example: String str2 = new String("Hello, Java!");
Control Structures: These are the statements that control the flow of
execution in a program. They allow the program to make decisions, repeat
actions, and control the order in which statements are executed. The main
types of control structures in Java are:
1. Decision-Making Structures
2. Looping Structures
3. Branching Structures
8
By: Manoj Sapkota, Nepal Polytechnic Institute
9
By: Manoj Sapkota, Nepal Polytechnic Institute
a) for loop: It is used for iterating a part of the program several times.
Example: for (int i = 1; i < =5; i++) {
System.out.print(" " + i);
}
Output: 1 2 3 4 5
10
By: Manoj Sapkota, Nepal Polytechnic Institute
Output: 1 2 3 4 5
c) do-while Loop: The do-while loop is similar to the while loop, but it checks
the condition after executing the statements. It executes the code at least once
even if the condition is false.
Example: int i = 1;
do {
System.out.print(" " + i);
i++;
} while (i < 5);
Output: 1 2 3 4 5
3. Branching Structures: These are the statements that are used to alter the
control and jump from one part of code to another. These are as follows:
a) break statement
b) continue statement
11
By: Manoj Sapkota, Nepal Polytechnic Institute
c) return Statement: It exits from the current method and may return a value.
Example:
public int add(int a, int b) {
return a + b;
}
Arrays: Arrays are objects that store multiple values of the same type. They
group and manage a collection of related data items.
array[0] = 1;
array[1] = 3;
array[2] = 5;
array[3] = 7;
array[4] = 9;
12
By: Manoj Sapkota, Nepal Polytechnic Institute
Example program: Write a Java program to find the greatest number among 5
input numbers using array.
Solution:
13