JAVA_NOTES
JAVA_NOTES
Variables
A variable is a container (storage area) used to hold data.
Each variable should be given a unique name (identifier).
package com.apnacollege;
Data Types
Data types are declarations for variables. This determines the type
and size of data associated with variables which is essential to
know since different data types occupy different sizes of memory.
char Character 2 a, b, c ..
A, B, C ..
@, #, $ ..
Eg : String, Arrays
Constants
A constant is a variable in Java which has a fixed value i.e. it cannot
be assigned a different value once assigned.
package com.apnacollege;
int n = 1;
switch(n) {
case 1 :
System.out.println("Monday");
break;
case 2 :
System.out.println("Tuesday");
break;
case 3 :
System.out.println("Wednesday");
break;
case 4 :
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6 :
System.out.println("Saturday");
break;
default :
System.out.println("Sunday");
}
Loops
A loop is used for executing a block of statements repeatedly until a
particular condition is satisfied. A loop consists of an initialization
statement, a test condition and an increment statement.
For Loop
The syntax of the for loop is :
While Loop
The syntax for while loop is :
while(condition) {
// body of the loop
}
int i = 0;
while(i<=20) {
System.out.println(i);
i++;
}
Do-While Loop
The syntax for the do-while loop is :
do {
// body of loop;
}
while (condition);
int i = 0;
do {
System.out.println(i);
i++;
} while(i<=20);
Methods/Functions
A function is a block of code that performs a specific task.
Why are functions used?
a. If some functionality is performed at multiple places in software,
then rather than writing the same code, again and again, we create
a function and call it everywhere. This helps reduce code
redundancy.
b. Functions make maintenance of code easy as we have to change at
one place if we make future changes to the functionality.
c. Functions make the code more readable and easy to understand.
The return type of a function is the data type of the variable that
that function returns.
For eg - If we write a function that adds 2 integers and returns their
sum then the return type of this function will be ‘int’ as we will
return a sum that is an integer value.
When a function does not return any value, in that case the return
type of the function is ‘void’.
function_name
It is the unique name of that function.
It is always recommended to declare a function before it is used.
Parameters
A function can take some parameters as inputs. These parameters
are specified along with their data types.
For eg- if we are writing a function to add 2 integers, the parameters
would be passed like –
int add (int num1, int num2)
main function
The main function is a special function as the computer starts
running the code from the beginning of the main function. Main
function serves as the entry point for the program.
Example :
package com.apnacollege;
}
}
Time & Space Complexity
Time complexity of an algorithm quantifies the amount of time taken by
an algorithm to run as a function of the length of the input.
Types of notations
1. O-notation: It is used to denote asymptotic upper bound. For a
given function g(n), we denote it by O(g(n)). Pronounced as “big-
oh of g of n”. It is also known as worst case time complexity as it
denotes the upper bound in which the algorithm terminates.
2. Ω-notation: It is used to denote asymptotic lower bound. For a
given function g(n), we denote it by Ω(g(n)). Pronounced as “big-
omega of g of n”. It is also known as best case time complexity as
program.
Examples :
Linear Time Complexity. O(n)
n 3 2
O(n ) > O(n!) > O(n ) > O(n ) > O(n.log(n)) > O(n.log(log(n))) > O(n) > O(sqrt(n)) >
O(log(n)) > O(1)
Space Complexity
Space complexity of an algorithm quantifies the amount of
time taken by a program to run as a function of length of the
input. It is directly proportional to the largest memory your
program acquires at any instance during run time.
For example: int consumes 4 bytes of memory.
Arrays In Java
Arrays in Java are like a list of elements of the same type i.e. a list of
integers, a list of booleans etc.
a. Creating an Array (method 1) - with new keyword
int[] marks = new int[3];
marks[0] = 97;
marks[1] = 98;
marks[2] = 95;
Homework Problems
1. Take an array of names as input from the user and print them on
the screen.
import java.util.*;
//input
for(int i=0; i<size; i++) {
names[i] = sc.next();
}
//output
for(int i=0; i<names.length; i++) {
System.out.println("name " + (i+1) +" is : " + names[i]);
}
}
}
//input
for(int i=0; i<size; i++) {
numbers[i] = sc.nextInt();
}
}
}
//input
for(int i=0; i<size; i++) {
numbers[i] = sc.nextInt();
}
if(isAscending) {
System.out.println("The array is sorted in ascending order");
} else {
System.out.println("The array is not sorted in ascending order");
}
}
}