Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 9

1.

Compiler: A compiler (also known as the translator) is a complex piece of software whose job is
to convert the source code (code written in JAVA) to machine understandable/executable code
(binary code which contains only 0’s and 1’s) in one go. Once the code is converted into the
executable format, this code can be executed in any machine without the need for compilation
again.

However, the compiler must be installed in the computer where the source code is being
written. Hence in the below example, the compiler has to be installed on My Machine in order to
compile the entire source code and once it has compiled, we can then transfer this compiled
code or the executable code(which is the binary code) to the Friend’s Machine.

2. Interpreter: Interpreter is a software program written to translate source code into machine
code line by line. Which means that when we write a source code(in this case the My Machine)
which needs to be run on another machine(in this case the Friend’s Machine), we provide a copy
of the source code to the other computer(Friend’s Machine), the interpreter will then translate
each line of this source code during runtime on the machine on which the source code has to be
run(in this case the Friend’s Machine). The interpreter thus does not generate any executable
code as the compiler did, rather it simply translates each line of code directly and it executes.
Hence in case of interpreter, it is necessary to interpret/translate the code every single time it
has to be run since it does not generate an executable code.

It is important to node, that interpreter will have to be installed on the machine on which the
code has to be run(Friend’s Machine) and it is not necessary to have the interpreter installed on
My Machine because ultimately the code will be running on Friend’s Machine and not on My
Machine.

3. Hybrid Approach: In the hybrid approach, there is a mix of compiled and interpreted
approaches. So in this approach, the source code(JAVA) is converted into an intermediate code
called the Bytecode (do understand that this is not the executable code, rather it is an
intermediate code) which can be transferred to other machines and for executing this Bytecode
it has to be interpreted by the Interpreter which needs to be present on the machine on which
this code will be executed.

4. JAVA is a hybrid language, which means JAVA source code is first compiled into an intermediate
code called the Bytecode. This Bytecode is machine independent language and can be run on
any machine provided the machine on which this code will be executed must have an Interpreter
installed in it. In case of java, JVM is the interpreter for translating the Bytecode into machine
code, hence JVM is installed on the machine. This JVM will now interpret this Bytecode into the
machine code line by line during runtime.

The JVM also has a JIT (Just In Time) Compiler which basically converts the part of the Bytecode
which comes again and again into the machine readable format so that it does not have to be
interpreted everytime.

Below 2 images explain the process or steps of execution of the JAVA programs.
5. JDK (JAVA Development Kit) : The JDK is a software development environment which consists of
a set of tools and libraries which are necessary for developing and executing java programs.

As you can see from the above image, JDK is the collection of development tools which are used
to develop any java program and along with that it also has the JRE(JAVA Runtime Environment)
which is used to run the java programs and the JVM(JAVA Virtual Machine) which is used to
interpret the compiled java programs.

JRE - If an user like you and me only wants to run the java programs and does not have to build
any java programs then having only the JRE (JVM + Library Classes) installed on our machines is
enough. It is JRE is solely useful ONLY to run java programs.

JDK - If an user wants to develop java programs then in that case you have to have the JDK
installed on your machines. It is because JDK has all the necessary tools to code and develop java
based software applications.
6. JIT Compiler(Just In-Time Compiler):

-> The JIT compiler is an integral part of the JVM which helps in performance optimization of
JAVA codes. The JVM has the JIT Compiler bundled within it.

-> In general what happens is first the JAVA Compiler compiles the .java file into .class file which
is a bytecode. Now this .class file is loaded by the class loader into the JVM for it to be
interpreted. The JVM interprets each line of the bytecode, which means it interprets every single
line of bytecode into machine readable code and then this machine readable code is executed by
the hardware, hence it takes a lot of time for java code to execute.

-> In order to aid this and help improve the execution time, JIT was introduced. Now the major
work of the JIT Compiler is to compile a redundant or repetitive set of byte code sequence into
native machine code so that this native machine code can be directly executed in-place of
interpreting it repeatedly by the JVM.

-> Hence in simple terms, the work of the JVM to interpret every single line of bytecode into
machine readable code can be cumbersome and takes more execution time. To help the JVM
improve the performance JIT Compiler is used. This JIT Compiler can compile(compiling means
converting the bytecode into a machine readable format set of code) a set of redundant or
recurring lines of bytecode directly into native machine code so that JVM does not have to
repeatedly interpret the same lines of code. This JIT compiled native machine code can directly
be executed by the hardware without the JVM having to interpret it, hence JIT saves a lot of
execution time.

7. instanceof operator: The instanceof operator is used for type checking. It can be used to test if
an object is of type class/subclass/interface.
Syntax: object instanceof class/subclass/interface

8. Bitwise operators:

(a) Bitwise OR (|) - Performs OR operation for each corresponding bit.

(b) Bitwise AND (&) - Performs AND operation for each corresponding bit.

(c) Bitwise exclusive OR also called Bitwise XOR (^) - The XOR functions like if 2 corresponding
bits are same it returns 0, but if these bits are different then it returns 1.

(d) Bitwise complement (~) - It inverts all the bits

(e) Left Shift operator (<<) - It shifts the bits to the left side by the number of places mentioned.
Suppose 9 << 2, this shifts the bits of 9 by 2 places to the left. It gives the result for the following
equation: a * (2^b)

(f) Right Shift operator (>>) - It shifts the bits to the right side by the number of places
mentioned. Suppose 9 >> 2, this shifts the bits of 9 by 2 places to the right. It gives the result for
the following equation: a / (2^b)

Follow the link for video lecture - https://www.youtube.com/watch?v=1ueMk6-Msng

9. Variable scope: A variable that is defined within a class but outside of any block or method can
be accessed anywhere within the class without any issues. However, whenever we define a
variable within a block of code or method, then this variable can only be accessed within the
method or block and it gets expired once the method/block execution gets over.

Access Specifier scopes:


Also note, the name of the variable of inner loop and outside the loops must be different,
otherwise it will throw and error. On similar lines, the inner and outer block variables must be
named differently. Below is an example:

The above code will throw an error, because both the inner and outer block of code has the
same variable name.

However, if you consider the below code it will run because the inner loop variable expires once
the loop is over and after that the variable which is defined after the loop it will remain active for
the rest of the program execution.
10. User Input: There are 2 ways t take user input in JAVA which is using the classes BufferedReader
and Scanner.

Example for BufferedReader inputs:

class Marvel
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int a = Integer.parseInt(br.readLine());
String b = br.readLine();
}
}

The other way to take input is through the Scanner class. Below is an example:

class Marvel
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String a = sc.nextLine();
sc.nextLine();
String b = sc.nextLine();
int c = sc.nextInt();
}
}

Scanner class few details:

sc.next() - Used to accept a single word


sc.next().charAt(0) - Used to accept a single character
sc.nextLine() - Used to accept a string or a full line, however if we take 2 consecutive string
inputs we have to use an extra sc.nextLine() in order to read the space character.
Example:

Scanner sc = new Scanner(System.in);


String str1 = sc.nextLine();
sc.nextLine();
String str2 = sc.nextLine();
sc.nextLine();
String str3 = sc.nextLine();
int a = sc.nextInt();

System.in is used when we will be providing user inputs. However, if we want to put these input
as file contents, which means that we would want to read from a file, then we must put the
object for file. Below is the example:

BufferedReader br = new BufferedReader(new FileReader(“C:\Users\user 1\OneDrive\Desktop\


FULL STACK JAVA - 2024\Notes\Core JAVA”));

BufferedReader is preferred when there are huge size of inputs to be taken and also preferred
because it is synchronized. Scanner was introduced in later versions of java. However, Scanner
can very well serve the purpose of user inputs.
11. For-Each loops: There are different types of loops in java namely for-loop, while-loop, do-while
loop, for-each loop. However, here we would discuss a little bit of the for-each loop.
For-each loop is used to traverse an array or Collections.

Example:
class Example
{
public static void main(String args[])
{
int arr[] = {1,2,3,4,5,6,7,8,9};
for(int x : arr)
System.out.println(x);
}
}

For-each loops are only best when we just want to traverse the array or Collections, however we
cannot modify the array elements using this loop.

12. Arrays:

Syntax: int[] arr, int arr[]

Declaration of 1D Array:
int[] arr = new int[5];

Initialization of 1D Array:
int[] arr = new int[] {1,2,3,4,5};
int[] arr = {1,2,3,4,5};
--------------------------------------------------------------------------------------------------------------------------------
Declaring 2D - Arrays:
int[][] arr = new int[2][2];

Initialization:
int[][] arr = {{1,2}, {3,4}};
--------------------------------------------------------------------------------------------------------------------------------

Declaring Jagged Arrays(arrays which can have different row and column):
int[][] arr = new int[5][]; // This signifies that this array will have 5 rows

// Here we are declaring the number of elements each particular row will have
arr[0] = new int[3];
arr[1] = new int[4];
arr[2] = new int[1];
arr[3] = new int[2];
arr[4] = new int[5];

Initialization:
for(int i=0; i<arr.length; i++)
{
for(int j=0; j<arr[i].length; j++)
{
arr[i][j] = count++;
}
}
--------------------------------------------------------------------------------------------------------------------------------
13. Final Array - When an array is declared as final, the contents of the array can be changed.
However, the reference to the array object cannot be changed.

Example:
final int arr[] = {1, 2, 3, 4, 5};

This variable ‘arr’ is pointing to an object which holds an array, since we have declared it as final
we cannot change or assign the reference to a different object. However, we can definitely
change the actual array contents.

Example:

In this example, the line where we are changing the reference or assigning the value of arr2 into
arr1, hence the line arr1 = arr2 will throw an error. However, the line arr2 = arr1 wont cause any
issue.
14.

You might also like