Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Java Notes RCY

The document provides a comprehensive guide on Java programming, covering the Java Development Kit (JDK), Java Runtime Environment (JRE), and essential programming concepts such as variables, data types, operators, control structures, loops, and methods. It includes setup instructions for Visual Studio Code, compiling and running Java code, and examples of basic input/output operations. Additionally, it outlines variable naming conventions, typecasting, and the syntax for various programming constructs in Java.

Uploaded by

Chaitanya YVRR
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java Notes RCY

The document provides a comprehensive guide on Java programming, covering the Java Development Kit (JDK), Java Runtime Environment (JRE), and essential programming concepts such as variables, data types, operators, control structures, loops, and methods. It includes setup instructions for Visual Studio Code, compiling and running Java code, and examples of basic input/output operations. Additionally, it outlines variable naming conventions, typecasting, and the syntax for various programming constructs in Java.

Uploaded by

Chaitanya YVRR
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Java

JDK (Java Development Kit)


1.​ Dev Tools
a.​ Javac
b.​ Java
2.​ JRE (Java Runtime Environment)
a.​ Libraries
b.​ JVM (Java Virtual Machine - Compiles the Code in Binary

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

1.​ Create a Folder and Open that Folder in VS Code


2.​ Create a File and save it with .java extension
3.​ Type following basic code & click RUN
public class filename{
public static void main(String[] args) {
System.out.println("Hello!");
}
}

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

Type java in the terminal to check whether it’s installed or not

To Compile & Run Java Code


1.​ Just Click Run
2.​ Using Dev Tools
a.​ Type javac filename.java in the terminal
b.​ filename.class will be created in the folder - This is the bytecode
c.​ Type java filename in the terminal

➔​ Programming is a Black Box - Whose behavior is unknown


➔​ Data is crucial in a Program

Variable​ : A container that temporarily stores data


Datatype​ : Type of a variable based on the type of the data is stored

Creating a Variable
Syntax:
datatype variablename; or datatype variablename=data; (initialisation)

Variable Naming Rules & Conventions:


1.​ Use camelCase
2.​ Can contain a-z, A-Z, 0-9, _, $
3.​ Cannot use a keyword

Imp Data Types:


1.​ Primitive
a.​ int​ ​ - Number (+/-) without decimal point. Max 2147483648 (231)
b.​ float​ ​ - Number (+/-) with decimal point (6-7 numbers after point)
c.​ double​ ​ - A double is twice the size of a float
d.​ char​ ​ - Characters / Strings
e.​ boolean​ - True / False
2.​ Non-Primitive

Typecasting (AKA Type Conversion)


Data types order by size:
1.​ short
2.​ int
3.​ float
4.​ double
5.​ long

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

We can print string and variable as


System.out.println(“Result is “+x);

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);

Scanner Functions based on Data Types:


➔​ int ​​ - nextInt()
➔​ float​ - nextFloat()... so on
➔​ String​ - next() or nextLine()
➔​ Char​ - next().charAt(0)

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.

Method - Block of Code with a Name.

Advantages:
1.​ No repetition of code
2.​ Clean code
3.​ Less space required

Syntax:
returntype methodname (parameters)
{
}

➔​ To execute a Method, another Method should Call it.


➔​ Ex: Method A calling Method B​
- If Method A wants to send data to Method B while calling, it’s Parameters
- If Method B wants to return some data to Method A after task completion, it’s Return Type

public static void main(String[] args) - Here:


●​ void​ ​ is Returntype
●​ main​ ​ is Method
●​ String[] args​ are Parameters
●​ public​ ​ is Access Modifier (Let’s Discuss Later)
●​ static​ ​ Abstraction (Let’s Discuss Later)

Basic Output Method:


void hello()
{
System.out.println(“Good Morning!”);
}

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");

void hello(String name) {


System.out.println("Good Morning, " + name);
}

To create a Method to add 2 numbers:


myObj.calculator(2,3);

void calculator(int a, int b)


{
System.out.println(a+b);
}

Using other Return Types:


int result = myObj.multiply(2,3);
System.out.println(result);

int multiply(int a, int b)


{
int c = a*b;
return c;
}

You might also like