Java Notes
Java Notes
What is java?
Java runs on 3 billion devices worldwide, which makes Java one of the most popular
programming languages
• JDK
• JRE
• JVM
What is JDK?
What is JRE?
JRE (Java Runtime Environment) is a software package that provides Java class
libraries, Java Virtual Machine (JVM), and other components that are required to run
Java applications.
Page 1
IISem BCA OOPS USING JAVA
What is JVM?
JVM (Java Virtual Machine) is an abstract machine that enables your computer to run
a Java program.
When you run the Java program, Java compiler first compiles your Java code to
bytecode. Then, the JVM translates bytecode into native machine code
Features of Java
o Simple: Java is a simple language because its syntax is simple, clean, and easy to
understand. Complex and ambiguous concepts of C++ are either eliminated or
re-implemented in Java. For example, pointer and operator overloading are not
used in Java.
o Object-Oriented: In Java, everything is in the form of the object. It means it has
some data and behavior. A program must have at least one class and object.
o Robust: Java makes an effort to check error at run time and compile time. It uses
a strong memory management system called garbage collector. Exception
handling and garbage collection features make it strong.
o Secure: Java is a secure programming language because it has no explicit pointer
and programs runs in the virtual machine. Java contains a security manager that
defines the access of Java classes.
o Platform-Independent: Java provides a guarantee that code writes once and run
anywhere. This byte code is platform-independent and can be run on any
machine.
Page 2
IISem BCA OOPS USING JAVA
class HelloWorld
System.out.println("Hello, World");
1. Class definition
This line uses the keyword class to declare that a new class is being defined.
class HelloWorld {
//
//Statements
Page 3
IISem BCA OOPS USING JAVA
2. HelloWorld
It is an identifier that is the name of the class. The entire class definition, including all
of its members, will be between the opening curly brace “{” and the closing curly
brace “}“.
3. main method:
In the Java programming language, every application must contain a main method.
The main function(method) is the entry point of your Java application, and it’s
mandatory in a Java program. whose signature in Java is:
static: The main method is to be called without an object. The modifiers public and
static can be written in either order.
main(): Name configured in the JVM. The main method must be inside the class
definition. The compiler executes the codes starting always from the main function.
String[]: The main method accepts a single argument, i.e., an array of elements of
type String.
Page 4
IISem BCA OOPS USING JAVA
COMMENTS IN JAVA
In Java there are three types of comments:
Single-line comments.
//Comments here( Text in this line only is considered as comment )
Multi-line comments.
/*Comment starts
continues
continues
.
.
.
Comment ends*/
DATA TYPES
Data types specify the different sizes and values that can be stored in the variable. There
are two types of data types in Java:
1. Primitive data types: The primitive data types include boolean, char, byte, short,
int, long, float and double.
2. Non-primitive data types: The non-primitive data types
include Classes, Interfaces, and Arrays.
Page 5
IISem BCA OOPS USING JAVA
In Java language, primitive data types are the building blocks of data manipulation.
These are the most basic data types available in Java language.There are eight primitive
data types in Java
Page 6
IISem BCA OOPS USING JAVA
The term non-primitive data type means that these types contain “a memory address of
the variable”.
In contrast to primitive data types, which are defined by Java, non-primitive data types
are not defined or created by Java but they are created by the programmers.
They are also called Reference data types because they cannot store the value of a
variable directly in memory.
VARIABLES
A variable in Java is a container that holds the value during the execution of Java
program. In other words, Variable is the name of the memory location reserved for
storing value.
Each variable in Java has a specific type that determines the size of the memory.
Page 7
IISem BCA OOPS USING JAVA
1. As per Java coding standard, the variable name should start with a lower case
letter.
If you have lengthy variables such as more than one words then you can declare
the first word small and second word with the capital letter
• int age;//valid
• int smallNumber; // valid.
• String collegeName; // valid.
• int num ber = 100; is invalid because there is a blank space between num and
ber.
• String $name; // valid.
• String _nSchool; // valid.
• int @num; // invalid.
Page 8
IISem BCA OOPS USING JAVA
1) Local Variable
1. A variable that is declared and used inside the body of methods, constructors, or
blocks is called local variable in java. It is called so because local variables are not
available for use from outside.
2. We must assign a local variable with a value at the time of creating. If you use a local
variable without initializing a value, you will get a compile-time error like “variable x not
have been initialized”.
}
3. We cannot use access modifiers with local variables.
4. The local variables are visible only within the declared constructors, methods, or
blocks.
5. A local variable is not equivalent to an instance variable.
6. A local variable cannot be static.
2) Instance Variable
1. A variable that is declared inside the class but outside the body of the methods,
constructors, or any blocks is called instance variable in java.
Page 9
IISem BCA OOPS USING JAVA
They are available for the entire class methods, constructors, and blocks. It is also called
non-static variable because it is not declared as static.
2. Instance variables are created when an object is created using the keyword ‘new’ and
destroyed when the object is destroyed.
3. We can also use access modifiers with instance variables. If we do not specify any
modifiers, the default access modifiers will be used which can be accessed in the same
package only.
4. It is not necessary to initialize the instance variable.
3) Static variable
1 A variable which is declared with a static keyword is called static variable in java.
A static variable is also called class variable because it is associated with the class.
2. Static variables are always declared inside the class but outside of any methods,
constructors, or blocks.
OPERATORS
Operators are used to perform operations on variables and values.
Java provides many types of operators which are classified based on the functionality
they provide.
1 Arithmetic Operators
2 Unary Operators
3 Assignment Operator
4 Relational Operators
5 Logical Operators
6 Ternary Operator
7 Bitwise Operators
8 Shift Operators
9 instance of operator
Arithmetic Operators
Page 10
IISem BCA OOPS USING JAVA
They are used to perform simple arithmetic operations on primitive data types.
2 Unary Operators
Unary operators need only one operand. They are used to increment, decrement or
negate a value.
Page 11
IISem BCA OOPS USING JAVA
3 Assignment Operator
Assignment operator is used to assign a value to any variable.
4 Relational Operators
These operators are used to check for relations like equality, greater than, less
than.
Page 12
IISem BCA OOPS USING JAVA
5 Logical Operators
Logical operators are used to determine the logic between variables or values
6 Ternary Operator
A ternary operator evaluates the test condition and executes a block of code
based on the result of the condition.
Syntax
condition ? expression1 : expression2;
Here, condition is evaluated and
• if condition is true, expression1 is executed.
• And, if condition is false, expression2 is executed.
Page 13
IISem BCA OOPS USING JAVA
Page 14
IISem BCA OOPS USING JAVA
if (hasVoterCard)
if-else statement: An if-else statement, there are two blocks one is if block and
another is else block. If a certain condition is true, then if block executes otherwise else
block executes.
Page 15
IISem BCA OOPS USING JAVA
int a = 10;
int b = 50;
if (a > b)
else
Page 16
IISem BCA OOPS USING JAVA
int a = 10;
Page 17
IISem BCA OOPS USING JAVA
switch(a)
case 1:
System.out.println("Value of a: 1");
break;
case 5:
System.out.println("Value of a: 5");
break;
case 10:
System.out.println("Value of a: 10");
break;
default:
System.out.println("else block");
break;
Loop statements
Statements that execute a block of code repeatedly until a specified condition is met are
known as looping statements. Java provides the user with three types of loops
Page 18
IISem BCA OOPS USING JAVA
While
Known as the most common loop, the while loop evaluates a certain condition. If the
condition is true, the code is executed. This process is continued until the specified
condition turns out to be false.
The condition to be specified in the while loop must be a Boolean expression.
Syntax:
while (condition)
statementOne;
int i = 5;
System.out.println(i);
i = i+2;
Page 19
IISem BCA OOPS USING JAVA
Do..while
The do-while loop is similar to the while loop, the only difference being that the
condition in the do-while loop is evaluated after the execution of the loop body. This
guarantees that the loop is executed at least once
Syntax
do{
//code to be executed
}while(condition);
Example
int i = 20;
do
System.out.println(i);
i = i+1;
Page 20
IISem BCA OOPS USING JAVA
For
The for loop in java is used to iterate and evaluate a code multiple times. When the
number of iterations is known by the user, it is recommended to use the for loop.
Syntax
statement;
Example
System.out.println(i);
Method in Java
Page 21
IISem BCA OOPS USING JAVA
code. We write a method once and use it many times. We do not require to write code
again and again. It also provides the easy modification and readability of code,
Method Declaration
The method declaration provides information about method attributes, such as visibility,
return-type, name, and arguments. It has six components that are known as method
header,
Method Signature: Every method has a method signature. It is a part of the method
declaration. It includes the method name and parameter list.
Access Specifier: Access specifier or modifier is the access type of the method. It
specifies the visibility of the method. Java provides four types of access specifier:
o Public: The method is accessible by all classes when we use public specifier in
our application.
o Private: When we use a private access specifier, the method is accessible only in
the classes in which it is defined.
o Protected: When we use protected access specifier, the method is accessible
within the same package or subclasses in a different package.
Page 22
IISem BCA OOPS USING JAVA
o Default: When we do not use any access specifier in the method declaration,
Java uses default access specifier by default. It is visible only from the same
package only.
Return Type: Return type is a data type that the method returns. It may have a primitive
data type, object, collection, void, etc. If the method does not return anything, we use
void keyword.
Method Name: It is a unique name that is used to define the name of a method. It must
be corresponding to the functionality of the method. Suppose, if we are creating a
method for subtraction of two numbers, the method name must be subtraction(). A
method is invoked by its name.
Parameter List: It is the list of parameters separated by a comma and enclosed in the
pair of parentheses. It contains the data type and variable name. If the method has no
parameter, left the parentheses blank.
Method Body: It is a part of the method declaration. It contains all the actions to be
performed. It is enclosed within the pair of curly braces.
Naming a Method
Types of Method
o Predefined Method
o User-defined Method
Page 23
IISem BCA OOPS USING JAVA
Predefined Method
In Java, 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. Some pre-defined methods are length(), equals(),
compareTo(), sqrt(), etc.
User-defined Method
Method Overloading is a feature that allows two or more methods may have the same
name but different in parameters, These methods are called overloaded methods and
this feature is called method overloading.
void func() { ... }
Here, the func() method is overloaded. These methods have the same name but accept
different arguments.
Example2:
Page 24
IISem BCA OOPS USING JAVA
Java array
Java array is an object which contains elements of a similar data type The elements of
an array are stored in a contiguous memory location. It is a data structure where we
store similar elements.
Page 25
IISem BCA OOPS USING JAVA
Example:
Page 26
IISem BCA OOPS USING JAVA
You can loop through the array elements with the for loop, and use the length property
to specify how many times the loop should run.
System.out.println(cars[i]);
Syntax
...
Example
System.out.println(i);
Page 27
IISem BCA OOPS USING JAVA
class GFG
{
public static void main (String[] args)
{
// declares an Array of integers.
int[] arr;
//so on...
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
Page 28
IISem BCA OOPS USING JAVA
// printing 2D array
for (int i=0; i< 3 ; i++)
{
for (int j=0; j < 3 ; j++)
System.out.print(arr[i][j] + " ");
System.out.println();
}
}
}
Output
279
361
742
Page 29
IISem BCA OOPS USING JAVA
Output
sum of array values : 15
Jagged arrays are a special type of arrays that can be used to store rows of data of
varying lengths
In jagged array we can create a 2-D array but with a variable number of columns in
each row.
class Main {
public static void main(String[] args)
Page 30
IISem BCA OOPS USING JAVA
{
// Declaring 2-D array with 2 rows
int arr[][] = new int[2][];
// Initializing array
int count = 0;
for (int i = 0; i < arr.length; i++)
for (int j = 0; j < arr[i].length; j++)
arr[i][j] = count++;
Output
Contents of 2D Jagged Array
012
34
Page 31
IISem BCA OOPS USING JAVA
Array of objects
class GFG {
Page 32
IISem BCA OOPS USING JAVA
System.out.println(
"Student data in student arr 0: ");
arr[0].display();
System.out.println(
"Student data in student arr 1: ");
arr[1].display();
}
}
Page 33
IISem BCA OOPS USING JAVA
JAVA MATH
The java.lang.Math class contains many methods that allows you to perform
mathematical tasks on numbers.
Page 34
IISem BCA OOPS USING JAVA
} }
Output:
Page 35