Java Notes RCY
Java Notes RCY
JRE makes our compiled output file portable (can run in any OS).
Source Code > Bytecode > Binary
Runtime Setup:
1. Install VS Code
2. Install Java Extension Pack
If doesn’t work:
1. Download JDK
2. C:/Program Files/Java/jdk-17.x.x/bin
3. Double click on the path and copy using Ctrl + C
4. In the Search Bar search for System Environment Variables and open it
5. Advanced > Environment Variables… > Path > Edit
6. Click New > Paste the Path using Ctrl + V > Move Up > OK > OK
Creating a Variable
Syntax:
datatype variablename; or datatype variablename=data; (initialisation)
We can convert top to bottom directly without data loss. But bottom to top conversion will cause
data loss. Still we can convert like:
float a=1.23; int b;
b=(int)a;
Basic IO
Output:
System.out.print(); or System.out.println(); - Automatically go to next line
Input:
import java.util.Scanner;
int a;
System.out.print(“Enter a Number”);
Scanner myObj = new Scanner(System.in); - Creating an object
a = myObj.nextInt();
System.out.print(“You have entered ”+a);
Operators:
a = b + 2;
It is an expression. In this all symbols are known as operators. Remaining characters are operands.
Process in which they both involved is known as operations.
Types of Operators
1. Arithmetic Operators + - * / %
2. Unary Operators ++ -- [Requires only one Operand]
Pre-Operation ++a --a
Post-Operation a++ a--
3. Assignment Operator = += -= *= /=
a=a+b a=a-b a=a*b a=a/b;
4. Relational Operators < > <= >= != == [True/False]
5. Logical Operators && || !
6. Ternary Operator ? Condition ? True : False
7. Bitwise Operators & | >> <<
Conditions
1. if
2. switch
If Syntax for Single Condition: If Syntax for Multiple Conditions:
if (condition) if (condition)
{ {
Code; Code;
} }
else else if (condition 2)
{ {
Code; Code;
} }
else
{
Code;
}
We can have a separate if-else inside an if
Switch Syntax:
switch (expression)
{
case a: Code;
break;
case a: Code;
break;
default: Code;
break; [Optional. But write it.]
}
Loops
Repeating the same code an ‘n’ number of times is called a loop, until a condition is true/false
1. while
2. do while
3. for
Every loop has 3 things in common:
1. Initialization
2. Condition
3. Increment/decrement
While Syntax:
Initialization
while(Condition)
{
Code;
Increment/decrement;
}
While Syntax:
Initialization
do
{
Code;
Increment/decrement;
}
while(Condition);
For Syntax:
for (Initialization; Condition; Increment/decrement)
{
Code;
}
Break
The break statement is used to jump out of a loop.
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
if (i == 3) {
break;
}
System.out.println(i);
}
}
}
Continue
The continue statement breaks one iteration in the loop.
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}
}
}
Methods (Functions)
Methods & Functions are interchangeable words in Java. Both are almost the same. Methods are
Functions associated with the Classes and/or Objects.
Advantages:
1. No repetition of code
2. Clean code
3. Less space required
Syntax:
returntype methodname (parameters)
{
}
To call a Method, first we should create an Object within the same Class. Syntax:
classname objectname = new classname();
objectname.methodname();
Ex:
Methods myObj = new Methods();
myObj.hello();
If we want to say ‘Good Morning!’ to a particular person, add that person’s name as the Parameter
Methods myObj = new Methods();
myObj.hello("Virat");