0% found this document useful (0 votes)
7 views

Javamagi Notes

This document provides an overview of key Java concepts: 1. Java is an object-oriented programming language that is case sensitive and relies on classes and objects. The Java Development Kit (JDK) contains tools like compilers and libraries to develop Java programs. 2. The Java compiler converts Java source code (.java files) into bytecode (.class files), while the Java Runtime Environment (JRE) contains the Java Virtual Machine (JVM) which executes bytecode. 3. Java supports both compiled and interpreted execution - the compiler generates bytecode which is then interpreted by the JVM at runtime.

Uploaded by

magiofficial97
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)
7 views

Javamagi Notes

This document provides an overview of key Java concepts: 1. Java is an object-oriented programming language that is case sensitive and relies on classes and objects. The Java Development Kit (JDK) contains tools like compilers and libraries to develop Java programs. 2. The Java compiler converts Java source code (.java files) into bytecode (.class files), while the Java Runtime Environment (JRE) contains the Java Virtual Machine (JVM) which executes bytecode. 3. Java supports both compiled and interpreted execution - the compiler generates bytecode which is then interpreted by the JVM at runtime.

Uploaded by

magiofficial97
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/ 28

JAVA:

11/03/2023
JAVA is Object oriented Programming language!
Java is completely depends on classes and objects!
Java is case sensitive programming language!
Java is path independent language!
Java is Hybrid level language because it consists of both Compiler and Interpreter!
JDK: Java development (tools) kit: JDK contains java dev tools, libraries, compilers and JRE.
JDK supports you to compile and write the Java program and also debug.

What is compiler?
Compiler is a special program that will translate a programming language’s source code into into
machine code/Bytecode or another programming language.
The source code is typically written as high-level, human readab le language such as java or C++.
MyFirst.java – is source code
MyFirst.class is machine language file which has been created by compiler which is having
under JDK.

JRE: Java Run time Environment: It contains Java class library and JVM.
JRE is to create the Java Program and JVM will execute the program.
JVM – Java Virtual machine will execute the code and display the output as per your Java
Program.
Eg:
Compiler.java – File
Command for execution: javac9JDK) Compiler.class(JRE,JVM)-Execute
Java Compiler
JRE contains interpreter!
Interpreter: After the bytecode successfully loads, the Java interpreter creates an instance of
JVM that allows the java program to executer natively on the underlying machine.

JVM is an interpreter that will convert the Byte code to Machine level code and then it will
execute.

Day-2: 11/06/2023
Writing simple JAVA Program:
To create folder through CMD prompt:
1. md MyJava – to create folder
2. dir – to check the folder
3. cd MyJava – to go to created folder

Main Method:
Public static void main(String args[])
{
Write the logic
}
main() – method definition
String args[] – Command line arguments
void – is a return type, Since we can’t use Int, char or any other return types, it is a prototype in
JAVA!
static – If you want to use anything without creating object then we should use static keyword
in main method.
public – If you want to access anything from outside of class then we need to use public
Keyword.
Output Statement:
System.out.println(“Hello Team”);
main(); - method declaration/method calling
Classes will always start with Upper case!
System is class and out is object and println(); is a method
Commands:
Compile: javac filename.java
Execute: java filename

Day-3: 11/07/2023
Reading from Keyboard:
To print anything on the monitor, we use print method!
To take input or to read anything from keyboard Java provides a Class calls “Scanner”!
This class is built in util package!
Prototype of util package: import.java.util.*;
We need to create object for class as below:
Scanner sc(reference/instance) = new scanner(System.in); → passing the parameters to your object!
Prototype of creating object: Scanner sc = new Scanner(System.in);
Scanner Methods:
1. nextInt() : It will read the integer from the Keyword!
2. nextFloat() : used for decimal with precision of 4 bytes
3. nextDouble() : used for decimal with precision of 8 bytes
4. next() : It will reads only single word
5. nextLine() : It will reads the entire sentence
6. nextByte() : It will read only 1 char
7. nextShort() : if you want to read any big integers
8. nextLong() : Bigger Int
9. nextBoolean() : True/False
10. hasNextInt() : If the next value is Int then it will say True otherwise False
11. hasNextFloat() : If the next value is decimal then it will say True otherwise False

Day-4: 11/08/2023

Datatypes – variables & Literals:

When the program is running in the memory (temporarily) then that should hold some data
during the execution.
In Java first we need to declare variables and need not store the data in it.
Variable will have some datatype, so the type of data will be storing into it.
Eg: int(Data type) x(variable)
Prototype: int x=5;
Here we are storing the 5 into x variable and that 5 is a integer so we need to use integer
datatype based on the size!
The basic types of data types which are built in the compiler of Java(JDK):
Primitive data type:
It has 4 types:
1. Integral → byte,short,int,long
2. Floating → float, double
3. Character → char, String(class)
4. Boolean

Type Size(bytes) Range Default


Byte(8-bits) 1 -128 to 127 0
Short 2 -32768 to 32767 0
Int 4 -2147483648 to - 0
2147483647
Long 8 - 0L
Float 4 ±1.4E-45 to ±3.4E+38 0.0f
Double 8 ±439E -324 to 0.00d
±1.7E+308
Char “,String “ “ 2 0 to 65535 \40000
Boolean ? True/False false
Day-5: 11/09/2023

Checking size and Range of Data types:

Eg: int → it is datatype


Integer is the class for all int data types.
short → Short
CMD to see the Integer class: javap java.lang.Integer
Prototype: Integer.MIN_VALUE
useRadix(): this is a Scanner Class method to read the binary values.

Variables:
Variables are the names which are given to the data.
Variable is used to store the data.
Variable must have the data type.

Eg: int a = 5;

Here a is stored in the memory and the 5 will be stored in that as bytes based on the data type
size!
We need to declare the datatype for the variable.
And need to initialize the variable; int b = 5; → int b is declaration and =5; is initialization.
Whenever we declare and initialize the variable then that will be stored into main memory on
the base of datatype(Size).

Day-5: 11/14/2023

Variable -Naming Rules:


1. Variable should be case sensitive
Eg: Int a = 4; int A = 5;
2. Contains Alphabets, Number, _, $ and not at all Special Characters
Eg: int room101, room num, NUM$
3. Should start with Alphabet, _ or $
4. Should not be a keyword. (Keywords are the built in words of the compiler)
5. Should not be a classname, if class is also in use!
6. No limit on length of name
7. Follow the camel case!
Eg: byte rollNumber, avgMarksofClass

(camel case: Every word is uppercase except first word)

Literals:
Literals are the constant values those are used in a program.
Eg: int a= 5; intLiteral

float a=6.5f;

double d = 6.455; →Double Literal (Anything we start after decimal then it is double literal)

Char c = “A”; → char Literal

String str = “Java”; → String Literal

Datatype Literal
Byte Int
Short int
Int Int
Long L/l →5L/5l
Float f/F→2.3f/23F
Double d/D→2.3d/2.4D
Char ‘A’

We can have all the following numbers as well:

1. Decimals: 0,1,2,3,4,5,6,7,8,9
2. Binary: 0,1
3. Octal (8-bit): 0,1,2,3,4,5,6,7,10 → 8,11 →9,12 →10
4. Hexa Decimal (16-bit): 0,1,2,3,4,5,6,7,8,9, A→10, B→11, C→12, D→13, E→14, F→15
Eg: byte b=10; →dec

Byte b=0b1010; →Binary

Byte b=0b1100; →octal

Byte b=0b1110; →Hexa

Prototype to convert decimals to Binary:

System.out.println(Integer.toBinary(decimal));

System.out.println(Integer.toOctalString(octal)); → oct to dec

System.out.println(Integer.toHexString(decimal)); →Hexa to dec

All the above will comes under integer literals!

16-bit Binary Table:

8 4 2 1
0 0 0 0
0 0 0 1
0 0 1 0
0 0 1 1
0 1 0 0
0 1 0 1
0 1 1 0
0 1 1 1
1 0 0 0
1 0 0 1
1 0 1 0→10
1 0 1 1→11
1 1 0 0→12
1 1 0 1→13
1 1 1 0→14
1 1 1 1→15

Day-6: 11/15/2023
Float Literals:

Float f = 12.4f;

Double Literals:

Double d = 12.45d;
Float: computer should store the value but not the decimal point(.), So the number which ever after the
decimal point, those has to float and this point is call floating point.

It can be RHS or LHS.

Long Literals:

Long l = 783773878583L; 999_999_999L;

Features and Architectures of Java:

Compiler Interpreter
These are used to translate user level code to Interpreter will translate line by line and it will
Byte code read the machine level code.
Compiler will write the separate text(Generated Interpreter won’t generate any file and it will read
file)) and it will read whenever you want. every time when you execute.
If any error present in your code then the If any error found in that particular line then the
compiler won’t allow you to move further and interpreter will execute from above successfully
the entire code will blocked. It will ask you to and It stop from the errored line.
debug then only it will generate the class file
Compiler requires only 1 time! Interpreter is always needed to see the output on
the monitor.
C,C++ are compiler level languages JavaScript is Interpreter language and doesn’t
require any compiler since we only need
Interpreter to execute that is browser.

First.java

First.class

JVM/JIT (Just om Time Compiler)

Operating System

So, we have JIT in latest versions because interpreter is very slow when compared to compiler, JIT is
just in Time compiler! To execute the code.

How Java is Platform Independent?


Here Java Platform is called Operating System.

In Java Program, the JVM will communicate with Hardware through operating system ((Windows, MAC
OS, Linux etc.,) and will help us to execute the program.

This ask to OS is call “System call” then OS will print the output in the monitor.
So, First.class is a byte code and it wont understood by the OS, Class file will send input to JVM
and JVM will understands the Byte code and have a capability to convert from byte code
to machine level code and sends input to OS.
The OS will generate the output in the monitor.
Here JVM will makes Java a platform independent.

First.java

First.class

JVM/JIT (Just om Time Compiler)

Operating System

Windows Linux MAC OS

Compiler will compile one time and JVM will execute multiple times in any platform!
Day-7: 11/16/2023

Architecture of JVM:

Memory

CPU Heap Memory Every object is created in Heap.

(New Keyword) - new

Stack x,y,z, str… All variables will be stored

Method Area in Java Methods will be created here

Eg: print(), main(), etc..,

created here print(0, main()..


Class Loader

JVM Memory

Method Area Heap Stack PC Registers Native Stack


Method

Execution Engine – JIT Native method Native method libraries


compiler interface

Garbage collectors – It will collect the unused objects from Heap!


Class loader: This willk bring the byte code into main memory (Disc main memory).
Then the CPU will the code but CPU won’t understand the byte code, So here JVM or JIT
compiler will run the code and sends the input to OS.
Whenever the method is called the stack frame will be created! (Print();)

Class Loader subsystem

L
Loading Linking Initializing
Loading

Bootstrap class loader Verify


Extension class loader Prepare Initialization

Application class loader Resolve

Loading: The classes which are loaded in class loader then the objects will also loads to heap.
It supports reflection API.
Application class loader: The classes which are loaded and belongs to out applications! And
these were loaded by the application class loader.
Bootstrap and Extensive Class Loader will load the Run time classes that are required by JVM.

Extensive class loader will loads the Run time classes by the JVM.

Inside JRE(1.8) lib → rt jar file → It contains all java SE classes. This will loaded by Bootstrap class loader.

There is a folder called EXT in JRE LIB → There are some jar files contains and those are useful for JVM at
Runtime and there are loaded by extension Class Loaders.

Linking:

Linking is having 3 types of operations.


Verify: It will verify that byte code is perfect java byte code or not.

If any pattern is mismatching any other contains byte code then it will through verify that error.

It will check the running code whether it is secure or not!

Prepare: This will allocate a memory for static variables used in our classes.

Static Variables which will not stored in the stack or heap area, they are stored in the method area!

Resolve: Writing the reference of the method at the place of method call is called linking and this
operation is done by Resolve.

Initialization: This will initialize the static variables

All the static members of the classes will be initiated here.

It will execute the static blocks!

Day-8: 11/17/2023

Features of Java:

Features of java also called as “Buzz words”.

1. Java is Simple.
2. Java is Secure.--> Java will run under the shell of JVM. (verifier)
3. Java is Portable → You can run on any platform
4. Java is Object Oriented → This is the method of developing java programs.

Principles: Abstraction, Encapsulation, Inheritance and Polymorphism.

5. Java is Roubust → strong, never fail, never crash even in the worst conditions , these are
handling by exceptional handling functions. → It means very strong, never fails, never crash even
in worst conditions.
Even if we doesn’t have any resources it wont fail.

This is handled by “Exceptional Handling”.

6. Java is Multi-threading language. →Multi-threading means we can break our codes and split into
different types of pieces. → Java supports multi threading.
Multi threading means splitting the program into multiple programs and run
simultaneously.
7. Java is Architectural-Neutral → It is always a hardware architecture
Commonly used architecture is Von-Neumann architecture.
ARM(Mobile), RISC(Based on ARM), CISC,Embeded.
Java can run on any architecture because it is path independent.
8. Java is Interpreted → JIT compiler will works faster than interpreter.
9. Java is High Performance
10. Java is Distributed → It can collaborate with different components and work as a single program.
Those applications are called Enterprise applications.

Eg: EJB, Spring. – These are applications

11. Java is Dynamic → Every object is dynamic since those are loaded to Heap.
Dynamic is movable!

Operators and Expressions:

Arithmetic Operations: These are to perform mathematic operations such as +,*,-,/,%!

/ is called addition but it give Quotient.

% this symbol is called Modulus: This will returns Remainder.

This will also work on Float and double also.

If you find *,/,% in the operation then will get execute them first then it will execute +,- later because *,/,
% these operators have higher precedence than Add and Subtraction.

To overcome this if you want perform Add and Subtraction first then you need to keep them in
parenthesis ().

() → This will be used in java to change the precedence of the operation.

Eg: x=a+b/2;

It will try to execute division first


Eg: x=a+b/a*2;

Try below Expressions:.

1. X = a²-b²
2. X = a²-b²/2a

3. X = a%b/2c

Day-9: 11/20/2023

Byte+byte

Short+short

Int+byte

Int+int

Int+shoft – you will get int only

Here The compiler is converting all about data types into int data type and this mechanism is called
“coercion” if you perform any arithmetic operations!

Exercise:

Int X=byte+short;

Int X=short+int;

Float X=int+float;

Float X=long+float

Int X = char+short

Int X = char+flaot
ASCII Values → A-65 and so on

Double X = float+double

Double X = long+double

Calculate Area of a Triangle: 3 -sides

Formula: Area of a triangle is ½*b*h

A = ½*b*h;

You should take Base and heights from Keyboard!

Give the data as double since the result might get in decimals!

A = b*h*0.5;

A = b*h/2;

A = 1f/2f*b*h;

Area of 3 sides of triangle:


Sides = ½(a+b+c)

Area = √s(s-a)(s-b)(s-c)

Variables: A,B,C,S, Area

I/p: A,B,C

Ther is a class called Math which will available in lang package to perform all mathematical formulas!

Area = Math.sqrt(s*s-a*s-b*s-c); or Area = Math.sqrt(s*(s-a)*(s-b)*(s-c));

Day -10: 11/22/2023

Roots of Quadratic Equation:

Any equation that contains highest power (x square) like that is called quadratic equation.

−b ± √ b2−4 ac
Eg: x= (tough eg)
2a
Eg: a x 2 +bx +c=0 → QE

We will form the above equation by multiplying below 2 terms:

So, we need to find the roots of quadric equations:

( x +r 1 ) ( x +r 2 )

To find the above roots then we should know a,b,c (co-efficients)

−b+ √ b2−4 ac
r 1=
2a

−b−√ b2−4 ac
r 2=
2a
Volume and Area of Cuboid:

It is a rectangle with height!

It is having 6 sides

→Front/back Area = Length*height

→Right/Left Area = Height*Breadth

→Top/Botton = Length*Breadth

6 sides we are covering – this is for single side

Total Area is volume

Volume of cuboid = l*b*h;

Perimeter = 4(l+b+h);

Total Area of a cuboid = 2(l*h+h*b+l*b) sq. units;

I/p:

l,b,h,A, P, V from Keyboard!


Day -11: 11/23/2023

Increment and Decrement Operators:

Completely depends on assigining and ++/--

There are 2 types of increment Operators:

1. Post Increment: x++ → It will first assign then it will do increment value by 1.

Eg: int x = 5;
S.o.p(x++); o/p is 5!

2. Pre Increment: ++X → It will first do increment then it will assign into to variable!.

Eg: int x = 5;
S.o.p(++x); o/p is 6

3. Post Decrement: x-- → It will first assign then it will do decrement value by 1.

Eg: int x = 5;
S.o.p(x--); o/p is 5!

4. Pre Decrement: --X → It will first do decrement then it will assign into to variable!.

Eg: int x = 5;
S.o.p(++x); o/p is 4

Exercise:

1. C = a*x+++b;
2. C= a*++x+b;

Note: We Can perform these operators for float and double values also and for character also based on
ASCII coes!

Increment – add one more line to see the increment value

System.out.println(x); →To see the Increment value

If we want to see the increment value in first sysout itself


System.out.println(a++ +”\n”+ a);

Day -12: 11/24/2023

Bit-wise Operators:

There are only int data type!

These operators perform the operations on bits of the data.

As a computer is an electronic device that it can only reads binary language and works on bitwise
operators only.

These are operators are very faster than any other operators.

These are operators are very faster than any other operators.

Operator Math Symbol (DE - Digital Program Symbol


Electronic Symbol)
AND ^ (Amber Sign) & (Ambersand)
OR v | (Pipeline Symbol)
NOT ¬ ~
XOR (Circle – ^
Plus Sumbold Middle)
Right Shift >>
Left Shift <<
Unsiged Right Shift >>>

1. &:

And

Truth table: (Multiply)

A B A&B
0 0 0
0 1 0
1 0 0
1 1 1

2. | : OR

Truth table: (ADD)


A B A|B
0 0 0
0 1 1
1 0 1
1 1 1

3. ~ : NOT

Truth table: (Opposite Values)

A ~A
0 1
1 0

4. ^: XOR:

Truth table: (If we have 1 atleast then result is 1, if we have 0 on both or 1 on both then the
result is 0)

A B A^B
0 0 0
0 1 1
1 0 1
1 1 0

Note:If we have similar kind of birts then the results will be 0

Eg: int x = 10, int y=6

int z = x&y;

x→ 0 0 0 0 1 0 1 0

y→ 0 0 0 0 0 1 1 0

x&y→ 0 0 0 0 0 0 1 0 → 2
Eg: int x = 10, int y=6

int z = x|y;

x→ 0 0 0 0 1 0 1 0

y→ 0 0 0 0 0 1 1 0

x&y→ 0 0 0 0 1 1 1 0 → 14

Eg: int x = 10, int y=6

int z = x^y;

x→ 0 0 0 0 1 0 1 0

y→ 0 0 0 0 0 1 1 0

x&y→ 0 0 0 0 1 1 0 0 → 12

Day-13: 11/27/2023

Left Shift Operator: <<

Only one variable is enough to do this operation!

Eg: int x = 10;

Int x = x<<1;

x→ 0 0 0 0 1 0 1 0 (left shifting 1 bit)

0 0 0 1 0 1 0 0 →20
Left shift will give 16 bit

If we left shift the value by 1 then the result will be multiplied by 2

If we left shift the value by 2 then the result will be multiplied by 4

The moved out/removed but the fist zero 0 - will move to garbage collector

Right Shift Operator: >>

Only one variable is enough to do this operation!

Eg: int x = 10;

Int x = x>>1;

x→ 0 0 0 0 1 0 1 0 (Right shifting 1 bit)

0 0 0 0 0 1 0 1 →5

When we do Right shift operation then the 1 ( Formula−(n|¿ ¿ 2 k )¿ will be divided by 2

Day-14: 11/28/2023

Unsigned Right Shift Operator: >>>

Only 1 variable is enough to perform this operation!

Steps:

1. Perform 1s compliment (Opp values for the given input, if it is 0 then the 1’ss compliment is 1
and if its 1 then the 1’s compliment is 0).
2. Perform 2’s compliment as well
3. Perform Right shift by 1(x>>1) for 2’s compliment result!
4. Perform unsigned right shift operation (x>>>1)
5. For validation just take right shifted result bits and perform 1’s and 2’s compliments and if you
are seeing the result is divided by 2 for the i/p variable then its correct!
1’s Compliment – 1’s

Eg: int x = -10;

x→ 0 0 0 0 1 0 1 0

1’s → 1 1 1 1 0 1 0 1

+1

2’s → 1 1 1 1 0 1 1 0 → -10

If the 1st digit is 1 (unsigned bit) – it’s a negative value

Here first bit have to come as 0 is Called sigened bit, if you get 1 then it is unsigned bit and the result is -
ve number of the input variable!

x = 2’s → 1 1 1 1 0 1 1 0 → -10

x>>1 → 1 1 1 1 1 0 1 1

0 1 1 1 1 0 1 1 → 123

If we have unsigned bit at right shift operation we should not touch it, it should be remains same as
shown as above!
In unsiged Right shift operation the last bit will be moved to 64th bit place from right shfit operation
result and the last bit will be zero, This process is called unsiged right shift operation.

Shift the unsiged bit right is called unsigned right shift operation!

If you give -ve variables then you will get long integers1

x>>1 →1 1 1 1 1 0 1 1

1’s → 0 0 0 0 0 1 0 0

+1

0 0 0 0 0 1 0 1 →5

If you want to print the bits then use below string class in print method:

System.out.println(String.format("%s", Integer.toBirnaryString(z));

System.out.println(String.format("%s", Integer.toUnsignedString(z));

→ This will give the unsigned integer!

Day-15: 11/30/2023

Bit Merging and Bit Masking:

These are perform by Bitwise operators and these are Bitwise operators applications.

Eg: byte b = 0;

0 0 0 0 0 0 0 0
1 1 2 3 4 5 6 7

With the help of merging and masking we can check the bit is stored in 0 or 1.

Eg: a → 0 0 0 0 0 0 0 0

b → 0 0 0 0 1 0 0 0

a|b → 0 0 0 0 1 0 0 0

We can set of any bit in a byte as 1 and this process of setting 1 in a byte is called Merging!

The process of setting one of the bit as 1 from all zeros is called Bit Merging!

Masking:

Masking is hiding all the bits and looking in 1 or more Particular bits only!

4 bits = 1 Nibble

Eg: a → 0 0 0 0 1 0 0 0

b → 0 0 0 0 1 0 0 0

a|b → 0 0 0 0 1 0 0 0
If we want to store 2 numbers in a byte:

a=5→ 0 0 0 0 0 1 0 1

b=9→ 0 0 0 0 1 0 0 1

b<<9 → 1 0 0 1 0 0 0 0

a|b → 1 0 0 1 0 1 0 1

Bit – Swapping:

We need to use XOR operator to swap 2 numbers without using third variable!

Eg: int a = 9, int b = 12;

Steps:

1. Perform a^b

a→ 0 0 0 0 1 0 0 1

b→ 0 0 0 0 1 1 0 0

a=a^b → 0 0 0 0 0 1 0 1
2. Perform b=a^b!

0 0 0 0 0 1 0 1

0 0 0 0 1 1 0 0

0 0 0 0 1 0 0 1

3. Again perform a = a^b

0 0 0 0 0 1 0 1

0 0 0 0 1 0 0 1

0 0 0 0 1 1 0 0

If we perform XOR operation the result will not exceed the i/p variables!

System.out.println((c&0b11110000)>>4); → To read LHS side value

System.out.println(c&0b00001111); → To read RHS value

By OR Operation and shifting we can store the value

By AND operation and shifting we can retrieve the value!


a=5→ 0 0 0 0 0 1 0 1

b=9→ 0 0 0 0 1 0 0 1

a|b → 0 0 0 0 1 1 0 1

1 1 0 1 0 0 0 0

We have to do AND operation then!

Day-16: 12/01/2023

You might also like