Javamagi Notes
Javamagi Notes
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
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
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
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)
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’
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
System.out.println(Integer.toBinary(decimal));
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.
Long Literals:
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
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.
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
Operating System
Compiler will compile one time and JVM will execute multiple times in any platform!
Day-7: 11/16/2023
Architecture of JVM:
Memory
JVM Memory
L
Loading Linking Initializing
Loading
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:
If any pattern is mismatching any other contains byte code then it will through verify that error.
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.
Day-8: 11/17/2023
Features of Java:
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.
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.
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.
11. Java is Dynamic → Every object is dynamic since those are loaded to Heap.
Dynamic is movable!
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 ().
Eg: x=a+b/2;
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
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
A = ½*b*h;
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 = √s(s-a)(s-b)(s-c)
I/p: A,B,C
Ther is a class called Math which will available in lang package to perform all mathematical formulas!
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
( x +r 1 ) ( x +r 2 )
−b+ √ b2−4 ac
r 1=
2a
−b−√ b2−4 ac
r 2=
2a
Volume and Area of Cuboid:
It is having 6 sides
→Top/Botton = Length*Breadth
Perimeter = 4(l+b+h);
I/p:
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!
Bit-wise Operators:
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.
1. &:
And
A B A&B
0 0 0
0 1 0
1 0 0
1 1 1
2. | : OR
3. ~ : NOT
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
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
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
Int x = x<<1;
0 0 0 1 0 1 0 0 →20
Left shift will give 16 bit
The moved out/removed but the fist zero 0 - will move to garbage collector
Int x = x>>1;
0 0 0 0 0 1 0 1 →5
Day-14: 11/28/2023
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
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
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));
Day-15: 11/30/2023
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!
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
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!
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
Day-16: 12/01/2023