Java Notes
Java Notes
1. Install Java
a. Install JDK (https://www.oracle.com/in/java/technologies/javase-
downloads.html)
b. Install IntelliJ (https://www.jetbrains.com/idea/download/#section=mac)
2. Sample Code
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.
4. Variables
A variable is a container (storage area) used to hold data.
Each variable should be given a unique name (identifier).
package com.apnacollege;
char Character 2 a, b, c ..
A, B, C ..
@, #, $ ..
Eg : String, Arrays
6. String Class
Strings are immutable non-primitive data types in Java. Once a string is
created it’s value cannot be changed i.e. if we wish to alter its value
then a new string with a new value has to be created.
This class in java has various important methods that can be used for
Java objects. These include:
a. Concatenation
String name1 = new String("Aman");
String description = new String("is a good boy.");
b. CharAt
String name = new String("Aman");
System.out.println(name.charAt(0));
c. Length
String name = new String("Aman");
System.out.println(name.length());
d. Replace
String name = new String("Aman");
System.out.println(name.replace('a', 'b'));
e. Substring
String name = new String("AmanAndAkku");
System.out.println(name.substring(0, 4));
7. Arrays
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;
8. Casting
Casting in java is the assigning values of one type to another. The
types being considered here are compatible i.e. we can only assign
values of a number type to another type storing numbers (vice-versa is
not allowed i.e. floating values cannot be assigned to boolean data
types).
Casting in Java is of 2 types:
a. Implicit casting
This casting is done by java implicitly i.e. on its own. It is
assigning smaller values to larger data types.
float price = 100.00F;
int gst = 18;
float finalPrice = price + gst;
b. Explicit casting
This casting is done by the programmer. It is assigning larger
values to smaller data types.
int price = 100;
float gst = 18.00F;
int finalPrice = price + (int)gst;
9. 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;
10. Operators
There are 4 types of operators in Java :
a. Arithmetic Operators
Arithmetic operators are just like operators we used in Math.
These include:
1. ‘+’ Add
int a = 30;
int b = 40;
int sum = a + b;
2. ‘-’ Subtract
int a = 30;
int b = 40;
int diff = a - b;
3. ‘*’ Multiply
int a = 30;
int b = 40;
int mul = a * b;
4. ‘/’ Divide
int a = 30;
int b = 40;
int div = a / b;
6. Unary Operators
int a = 30;
a++;
a--;
b. Assignment Operators
Operator Operation Example
*= Multiplies the right operand with the left A*=B means A=A*B
operand and assigns the result to the left
operand.
c. Comparison/Relational Operators
Relational operators define the relation between 2 entities.
They give a boolean value as result i.e true or false.
< Gives true if left operand is less than right A<B is true
operand
d. Logical Operators
Logical operators are used to connect multiple expressions or
conditions together.
We have 3 basic logical operators.
Suppose : A=0 and B=1
b. Min
int a = 10;
int b = 20;
Math.min(a,b);
c. Random
int randomNumber = (int)(Math.random()*100);
Example :
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
float a = sc.nextFloat();
String name = sc.next();
String line = sc.nextLine();
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");
}
if (i%3==0) {
continue;
}
System.out.println(i);
}
int i;
for (i=1; i<=20; i++) {
if (i == 11) {
break;
}
System.out.println(i);
}
In this loop, when i becomes equal to 11, the for loop terminates due
to break statement, Hence, the program will print numbers from 1 to
10 only.
16. 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);
18. 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;
}
}
19. Mini-Project
Let’s create a project where we are trying to ask the user to guess a
randomly generated number.
The number is in the range of 1 to 100.
CODE
package com.apnacollege;
import java.util.Scanner;
do {
System.out.println("Guess my number(1-100) : ");
userNumber = sc.nextInt();
if(userNumber == myNumber) {
System.out.println("WOOHOO .. CORRECT NUMBER!!!");
break;
}
else if(userNumber > myNumber) {
System.out.println("your number is too large");
}
else {
System.out.println("your number is too small");
}
} while(userNumber >= 0);