Professional Documents
Culture Documents
1> what is difference between jdk , jre , jvm and jit ? explain with
diagram
Solution :- In this lesson let us learn about what is Java JVM, JRE, JDK and JIT.
Most of the beginners find it difficult to understand the difference between these jargons. In
short,
Full-Form The JDK is an abbreviation The JRE is an abbreviation for The JVM is an abbreviation
for Java Development Kit. Java Runtime Environment. for Java Virtual Machine.
Definition The JDK (Java Development The Java Runtime The Java Virtual Machine
Kit) is a software Environment (JRE) is an (JVM) is a platform-
development kit that implementation of JVM. It is a independent abstract machine
develops applications in Java. type of software package that that has three notions in the
Along with JRE, the JDK provides class libraries of form of specifications. This
also consists of various Java, JVM, and various other document describes the
development tools (Java components for running the requirement of JVM
Debugger, JavaDoc, applications written in Java implementation.
compilers, etc.) programming.
Functionality The JDK primarily assists in JRE has a major responsibility JVM specifies all of the
executing codes. It primarily for creating an environment implementations. It is
functions in development. for the execution of code. responsible for providing all
of these implementations to
the JRE.
Platform The JDK is platform- JRE, just like JDK, is also The JVM is platform-
Dependency dependent. It means that for platform-dependent. It means independent. It means that you
every different platform, you that for every different won’t require a different JVM
require a different JDK. platform, you require a for every different platform.
different JRE.
Tools Since JDK is primarily JRE, on the other hand, does JVM does not consist of any
responsible for the not consist of any tool- like a tools for software
development, it consists of debugger, compiler, etc. It development.
various tools for debugging, rather contains various
monitoring, and developing supporting files for JVM, and
java applications. the class libraries that help
JVM in running the program.
Implementation JDK = Development Tools + JRE = Libraries for running JVM = Only the runtime
JRE (Java Runtime the application + JVM (Java environment that helps in
Environment) Virtual Machine) executing the Java bytecode.
Why Use It? Why use JDK? Why use JRE? Why use JVM?
Some crucial reasons to Some crucial reasons to
Some crucial reasons to use JRE are: use JVM are:
use JDK are:
If a user wants to run It provides its users
It consists of various the Java applets, then with a platform-
tools required for they must install JRE independent way for
writing Java on their system. executing the Java
programs. The JRE consists of source code.
JDK also contains class libraries along JVM consists of
JRE for executing with JVM and its various tools,
Java programs. supporting files. It has libraries, and multiple
no other tools like a frameworks.
It includes an compiler or a The JVM also comes
Appletviewer, Java debugger for Java
application launcher, with a Just-in-Time
development. (JIT) compiler for
compiler, etc.
JRE uses crucial converting the Java
The compiler helps package classes like source code into a
in converting the util, math, awt, lang, low-level machine
code written in Java
JIT :-
Full form :- java-in-time-compiler
Definition :- Just In Time Compiler (JIT) runs on the fly in the execution environment
to make optimizations on the program.
Functionality:- It accelerates execution performance many times over the previous level. In
other words, it is a long-running, computer-intensive program that provides the best
performance environment. It optimizes the performance of the Java application at compile or
run time.
Platform Depedency :-
Implemenation :-
Why use it :-
Features :-
JVM was developed to make Java platform- JIT was developed to increase the
independent and portable. performance of the JVM and facilitate fast
execution of Java programs
JVM runs pre-compiled Java programs by JIT compiles bytecode to native machine
loading and executing .class files to produce code to optimise efficiency.
the output.
JVM interprets the same sequence of The hardware executes the native code of
bytecode repeatedly and incurs a longer repeated method calls.
execution time.
JVM is a part of the Java Runtime JIT compiler is a part of the JVM.
Environment(JRE)
The use of JVM is not optional as it plays a The use of JIT is optional as it serves to
vital role in executing Java code. increase the performance of program
execution.
JVM has many components like JVM JIT compiler is a functionality of the JVM that
language stacks, heaps, native method works with the interpreter,
libraries, etc
o Method In-lining
o Local Optimizations
o Control Flow Optimizations
o Constant Folding
o Dead Code Elimination
o Global Optimizations
o Heuristics for optimizing call sites
o Java Development Kit (JDK) provides the Java compiler ( javac) to compile the Java
source code into the bytecode (.class file). After that, JVM loads the .class file at
runtime and transform the bytecode into the binary code (machine code). Further,
the machine code is used by the interpreter.
o We know that the interpretation of Java bytecode reduces the performance of the
native application. It is the reason to implement the JIT compiler. The JIT compiler
accelerates the performance of the application by compiling the bytecode into native
machine code.
o It is enabled by default when a method is invoked. The JVM directly invokes the
compiled code of the method without interpreting it. It does not require much
memory usage.
2 > write a java program to take input from the keyboard at runtime
and calculate the factorial of given number using recursion .
Solution :- Algorithm
1. Start
2. Declare a variable to store a number.
3. Ask the user to initialize the number.
4. Check whether it is possible to calculate the factorial or not.
5. If the number is greater than and equal to 0, then call a recursive function to calculate
the factorial of the entered number.
6. If the number is lesser than 0, print the message that it is not possible to calculate the
factorial.
7. If the entered number is 0 or 1, then return 1.
8. If the entered number is other than 0 or 1, then calculate the factorial by recursively
calling the same method.
9. Return the result.
10. Print the factorial of the entered number.
11. Stop
import java.util.Scanner;
//Driver Code
if(num>=0)
int factorial=findFactorial(num);
System.out.println("The factorial of the entered
the number is :"+factorial);
else
if(num==0)
return 1;
else if(num==1)
return 1;
else
return num*findFactorial(num-1);
Copy
3 > write a java program to take input from the keyboard at runtime
and check whether the given number is armstrong number or not .
An Armstrong Number in Java is a unique integer where the sum of the cubes of its
individual digits equals the number itself. Examples of Armstrong numbers include
0, 1, 153, 370, 371, 407, and more.
How to Identify Armstrong Numbers?
Identifying Armstrong numbers involves a simple algorithm:
1.
Count the number of digits (n) in the given number.
2.
Extract each digit of the number.
3.
Raise each digit to the power of n.
4.
Sum all the results.
5.
Check if the sum is equal to the original number. If it is, the number is
an Armstrong number.
Formula to calculate Armstrong Number:
wxyz = pow(w,n) + pow(x,n) + pow(y,n) + pow(z,n)
Example:
Let’s look at 371 as an example to understand why it’s an Armstrong number.
Problem Description
Write a Java program to check if a given number is Armstrong. If the number is an
Armstrong then display it is an Armstrong number else display it is not an
Armstrong number.
Program/Source Code
Here is the source code of the Java Program to Check If a Given Number is
ArmStrong Number. The Java program is successfully compiled and run on a
Windows system. The program output is also shown below.
/*
* Armstrong Number in Java using While Loop
*/
import java.util.Scanner;
public class ArmStrong
{
public static void main(String[] args)
{
int n, count = 0, a, b, c, sum = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter a number:");
n = s.nextInt();
a = n;
c = n;
while(a > 0)
{
a = a / 10;
count++;
}
while(n > 0)
{
b = n % 10;
sum = (int) (sum+Math.pow(b, count));
n = n / 10;
}
if(sum == c)
{
System.out.println(c+ " is an Armstrong number");
}
else
{
System.out.println(c+ " is not an Armstrong number");
}
}
}
Program Explanation
1. Take the number as input and store it in the variable “n“.
2. Initialize variables “count”, “a”, “b”, “c”, and “sum” to keep track of the
calculation.
3. Use a while loop to count the number of digits in the input number and store it in
the “count” variable.
4. Use another while loop to calculate the sum of the cubes of each digit in the input
number and store it in the “sum” variable.
5. Check if the calculated sum is equal to the original number “c”.
6. If the sum is equal to “c”, print that it’s an Armstrong number; otherwise, print
that it’s not.
Time Complexity: O(log(n))
The program has a time complexity of O(log(n)) because both while loops run for
log(n) iterations. In each iteration, the number is divided by 10 until it becomes 0.
Space Complexity: O(1)
The space complexity of the program is O(1) because it uses a fixed number of
variables, and no additional space grows with the input size.
Runtime Test Cases
Testcase 1: In this case, we enter the number “371” as input to check whether it is a
armstrong number or not.
Testcase 2: In this case, we enter the number “150” as input to check whether it is a
prime number or not.
Enter a number: 150
150 is not an Armstrong number
4 > discuss the different type of operators in java along with their precedence
and associativity realtionships.
// Arithmetic Operators
import java.io.*;
// Drive Class
class GFG {
// Main Function
// Arithmetic operators
int a = 10;
int b = 3;
Output
a + b = 13
a - b = 7
a * b = 30
a / b = 3
a % b = 1
2. Unary Operators
Unary operators need only one operand. They are used to increment, decrement, or
negate a value.
– : Unary minus, used for negating the values.
+ : Unary plus indicates the positive value (numbers are positive without
this, however). It performs an automatic conversion to int when the type of
its operand is the byte, char, or short. This is called unary numeric
promotion.
++ : Increment operator, used for incrementing the value by 1. There are
two varieties of increment operators.
Post-Increment: Value is first used for computing the result and
then incremented.
Pre-Increment: Value is incremented first, and then the result is
computed.
– – : Decrement operator, used for decrementing the value by 1. There
are two varieties of decrement operators.
Post-decrement: Value is first used for computing the result
and then decremented.
Pre-Decrement: The value is decremented first, and then the
result is computed.
! : Logical not operator, used for inverting a boolean value.
Example:
Java
// Java Program to implement
// Uniary Operators
import java.io.*;
// Driver Class
class GFG {
// main function
// Interger declared
int a = 10;
int b = 10;
Output
Postincrement : 10
Preincrement : 12
Postdecrement : 10
Predecrement : 8
3. Assignment Operator
‘=’ Assignment operator is used to assign a value to any variable. It has right-to-left
associativity, i.e. value given on the right-hand side of the operator is assigned to the
variable on the left, and therefore right-hand side value must be declared before
using it or should be a constant.
The general format of the assignment operator is:
variable = value;
In many cases, the assignment operator can be combined with other operators to
build a shorter version of the statement called a Compound Statement. For
example, instead of a = a+5, we can write a += 5.
+=, for adding the left operand with the right operand and then assigning it
to the variable on the left.
-=, for subtracting the right operand from the left operand and then
assigning it to the variable on the left.
*=, for multiplying the left operand with the right operand and then
assigning it to the variable on the left.
/=, for dividing the left operand by the right operand and then assigning it
to the variable on the left.
%=, for assigning the modulo of the left operand by the right operand and
then assigning it to the variable on the left.
Example:
Java
// Java Program to implement
// Assignment Operators
import java.io.*;
// Driver Class
class GFG {
// Main Function
// Assignment operators
int f = 7;
Output
f += 3: 10
f -= 2: 8
f *= 4: 32
f /= 3: 10
f %= 2: 0
f &= 0b1010: 0
f |= 0b1100: 12
f ^= 0b1010: 6
f <<= 2: 24
f >>= 1: 12
f >>>= 1: 6
4. Relational Operators
These operators are used to check for relations like equality, greater than, and less
than. They return boolean results after the comparison and are extensively used in
looping statements as well as conditional if-else statements. The general format is,
variable relation_operator value
// Relational Operators
import java.io.*;
// Driver Class
class GFG {
// main function
// Comparison operators
int a = 10;
int b = 3;
int c = 5;
Output
a > b: true
a < b: false
a >= b: true
a <= b: false
a == c: false
a != c: true
5. Logical Operators
These operators are used to perform “logical AND” and “logical OR” operations,
i.e., a function similar to AND gate and OR gate in digital electronics. One thing to
keep in mind is the second condition is not evaluated if the first one is false, i.e., it
has a short-circuiting effect. Used extensively to test for several conditions for
making a decision. Java also has “Logical NOT”, which returns true when the
condition is false and vice-versa
Conditional operators are:
&&, Logical AND: returns true when both conditions are true.
||, Logical OR: returns true if at least one condition is true.
!, Logical NOT: returns true when a condition is false and vice-versa
Example:
Java
// Java Program to implemenet
// Logical operators
import java.io.*;
// Driver Class
class GFG {
// Main Function
// Logical operators
boolean x = true;
boolean y = false;
Output
x && y: false
x || y: true
!x: false
6. Ternary operator
The ternary operator is a shorthand version of the if-else statement. It has three
operands and hence the name Ternary.
The general format is:
condition ? if true : if false
The above statement means that if the condition evaluates to true, then execute the
statements after the ‘?’ else execute the statements after the ‘:’.
Example:
Java
// Java program to illustrate
// ternary operator.
// numbers
result
+ result);
Output
Max of three numbers = 30
7. Bitwise Operators
These operators are used to perform the manipulation of individual bits of a number.
They can be used with any of the integer types. They are used when performing
update and query operations of the Binary indexed trees.
&, Bitwise AND operator: returns bit by bit AND of input values.
|, Bitwise OR operator: returns bit by bit OR of input values.
^, Bitwise XOR operator: returns bit-by-bit XOR of input values.
~, Bitwise Complement Operator: This is a unary operator which returns
the one’s complement representation of the input value, i.e., with all bits
inverted.
Java
// Java Program to implement
// bitwise operators
import java.io.*;
// Driver class
class GFG {
// main function
// Bitwise operators
int d = 0b1010;
int e = 0b1100;
Output
d & e: 8
d | e: 14
d ^ e: 6
~d: -11
d << 2: 40
e >> 1: 6
e >>> 1: 6
8. Shift Operators
These operators are used to shift the bits of a number left or right, thereby
multiplying or dividing the number by two, respectively. They can be used when we
have to multiply or divide a number by two. General format-
<<, Left shift operator: shifts the bits of the number to the left and fills 0
on voids left as a result. Similar effect as multiplying the number with
some power of two.
>>, Signed Right shift operator: shifts the bits of the number to the right
and fills 0 on voids left as a result. The leftmost bit depends on the sign of
the initial number. Similar effect to dividing the number with some power
of two.
>>>, Unsigned Right shift operator: shifts the bits of the number to the
right and fills 0 on voids left as a result. The leftmost bit is set to 0.
Java
// Java Program to implement
// shift operators
import java.io.*;
// Driver Class
class GFG {
// main function
int a = 10;
Output
a<<1 : 20
a>>1 : 5
9. instanceof operator
The instance of the operator is used for type checking. It can be used to test if an
object is an instance of a class, a subclass, or an interface. General format-
object instance of class/subclass/interface
Java
// Java program to illustrate
// instance of operator
class operators {
class Person {
interface MyInterface {
Output
obj1 instanceof Person: true
obj1 instanceof Boy: false
obj1 instanceof MyInterface: false
obj2 instanceof Person: true
obj2 instanceof Boy: true
obj2 instanceof MyInterface: true
Example:
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int i = 5;
Output:
GFG
GFG
GFG
GFG
GFG
do-while loop:
do while loop is similar to while loop with the only difference that it checks for the
condition after executing the statements, and therefore is an example of Exit
Control Loop.
Syntax:
do
{
statements..
}
while (condition);
Flowchart:
Example:
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int i = 5;
do {
i++;
System.out.println("GfG");
} while (i < 10);
}
}
Output:
GFG
GFG
GFG
GFG
GFG
Here is the difference table:
while do-while
while loop is entry controlled loop. do-while loop is exit controlled loop.
while(condition) do { statement(s); }
{ statement(s); } while(condition);