0% found this document useful (0 votes)
9 views10 pages

JAVA_NOTES

The document provides an overview of basic programming concepts in Java, including functions, classes, variables, data types, constants, conditional statements, loops, methods, time and space complexity, and arrays. It explains the syntax and usage of each concept with examples, highlighting the importance of functions for code reusability and maintenance. Additionally, it includes homework problems to practice array manipulation and sorting.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views10 pages

JAVA_NOTES

The document provides an overview of basic programming concepts in Java, including functions, classes, variables, data types, constants, conditional statements, loops, methods, time and space complexity, and arrays. It explains the syntax and usage of each concept with examples, highlighting the importance of functions for code reusability and maintenance. Additionally, it includes homework problems to practice array manipulation and sorting.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Functions

A function is a block of code which takes some input, performs some


operations and returns some output.
The functions stored inside classes are called methods.
The function we have used is called main.
Class
A class is a group of objects which have common properties. A class
can have some properties and functions (called methods).
The class we have used is Main.
public class Main {

public static void main(String[] args) {


// Our 1st Program
System.out.println("Hello World");
}
}

Variables
A variable is a container (storage area) used to hold data.
Each variable should be given a unique name (identifier).

package com.apnacollege;

public class Main {

public static void main(String[] args) {


// Variables
String name = "Aman";
int age = 30;

String neighbour = "Akku";


String friend = neighbour;
}
}

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.

There are 2 types of Data Types :


 Primitive Data types : to store simple values
 Non-Primitive Data types : to store complex values

Primitive Data Types


These are the data types of fixed size.
Data Type Meaning Size Range
(in Bytes)

byte2’s complement integer 1 -128 to 127

short2’s complement integer 2 -32K to 32K

int Integer numbers 4 -2B to 2B

long2’s complement integer 8 -9,223,372,036,854,775,808


to 9,223,372,036,854,775,807
(larger values)

float Floating-point 4 Upto 7 decimal digits

double Double Floating-point 8 Upto 16 decimal digits

char Character 2 a, b, c ..
A, B, C ..
@, #, $ ..

bool Boolean 1 True, false

Non-Primitive Data Types


These are of variable size & are usually declared with a ‘new’
keyword.

Eg : String, Arrays

String name = new String("Aman");


int[] marks = new int[3];
marks[0] = 97;
marks[1] = 98;
marks[2] = 95;

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;

public class Main {

public static void main(String[] args) {


// Constants
final float PI = 3.14F;
}
}
Conditional Statements ‘if-else’
The if block is used to specify the code to be executed if the
condition specified in if is true, the else block is executed
otherwise.

int age = 30;


if(age > 18) {
System.out.println("This is an adult");
} else {
System.out.println("This is not an adult");
}

Conditional Statements ‘switch’


Switch case statements are a substitute for long if statements that
compare a
variable to multiple values. After a match is found, it executes the
corresponding code of that value case.

The following example is to print days of the week:

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 :

for (initialization; condition; update) {


// body of-loop
}

for (int i=1; i<=20; i++) {


System.out.println(i);
}

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 syntax for function declaration is :


return-type function_name (parameter 1, parameter2, ……
parameter n){ //function_body
}
return-type

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;

public class Main {


//A METHOD to calculate sum of 2 numbers - a & b
public static void sum(int a, int b) {
int sum = a + b;
System.out.println(sum);
}

public static void main(String[] args) {


int a = 10;
int b = 20;
sum(a, b); // Function Call

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

3. 𝚯-notation: It is used to denote the average time of a


it denotes the lower bound in which the algorithm terminates.

program.

Examples :
Linear Time Complexity. O(n)

Comparison of functions on the basis of time complexity

It follows the following order in case of time complexity:

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)

Note: Reverse is the order for better performance of a code with


corresponding time complexity, i.e. a program with less time
complexity is more efficient.

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;

b. Creating an Array (method 2)


int[] marks = {98, 97, 95};
c. Taking an array as an input and printing its elements.
import java.util.*;

public class Arrays {


public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
int numbers[] = new int[size];

for(int i=0; i<size; i++) {


numbers[i] = sc.nextInt();
}

//print the numbers in array


for(int i=0; i<arr.length; i++) {
System.out.print(numbers[i]+" ");
}
}
}

Homework Problems
1. Take an array of names as input from the user and print them on
the screen.
import java.util.*;

public class Arrays {


public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
String names[] = new String[size];

//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]);
}

}
}

2. Find the maximum & minimum number in an array of integers.

[HINT : Read about Integer.MIN_VALUE & Integer.MAX_VALUE in


Java]
import java.util.*;

public class Arrays {


public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
int numbers[] = new int[size];

//input
for(int i=0; i<size; i++) {
numbers[i] = sc.nextInt();
}

int max = Integer.MIN_VALUE;


int min = Integer.MAX_VALUE;

for(int i=0; i<numbers.length; i++) {


if(numbers[i] < min) {
min = numbers[i];
}
if(numbers[i] > max) {
max = numbers[i];
}
}

System.out.println("Largest number is : " + max);


System.out.println("Smallest number is : " + min);

}
}

1. Take an array of numbers as input and check if it is an array sorted


in ascending order.

Eg : { 1, 2, 4, 7 } is sorted in ascending order.

{3, 4, 6, 2} is not sorted in ascending order.


import java.util.*;

public class Arrays {


public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
int numbers[] = new int[size];

//input
for(int i=0; i<size; i++) {
numbers[i] = sc.nextInt();
}

boolean isAscending = true;

for(int i=0; i<numbers.length-1; i++) { // NOTICE numbers.length - 1


as termination condition
if(numbers[i] > numbers[i+1]) { // This is the condition for
descending order
isAscending = false;
}
}

if(isAscending) {
System.out.println("The array is sorted in ascending order");
} else {
System.out.println("The array is not sorted in ascending order");
}

}
}

You might also like