You are on page 1of 30

Important question

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,

 Java Virtual Machine (JVM) is an abstract definition of a computing machine.


 Java Runtime Environment (JRE) is the implementation of the JVM.
 Java Development Kit (JDK) is a set of tools using which Java programs can be
developed and compiled.
 Just In Time Compiler (JIT) runs on the fly in the execution environment to make
optimizations on the program.
 Java Development Kit (JDK) contains JRE along with various
development tools like Java libraries, Java source compilers, Java
debuggers, bundling and deployment tools.
 Just In Time compiler (JIT) is runs after the program has started
executing, on the fly. It has access to runtime information and makes
optimizations of the code for better performance.

Java Virtual Machine (JVM)
JVM is an abstract computing machine used to implement Java language. JVM is the key
component in the Java eco system. Java's important property is platform independence and
to achieve that the role of JVM is critical. The JVM sits on top of the hardware and is used to
execute the Java programs. Input for the JVM is Java byte code and it interprets the
instructions in the byte code thus executing the programs.

Java Runtime Environment (JRE)


JRE is the software environment where the compiled Java byte codes executes. JRE is the
implementation of JVM it includes the code required for running the programs and native
code. It contains the implementation of Java interpreter, garbage collector, class loader, java
class libraries and related files. You can download the JRE separately or as a bundle along
with JDK from Oracle's website.
Java Development Kit (JDK)
Java JDK is the software bundle using which the Java applications can be developed. JDK's
main component is the Java compiler using which the Java source code is compiled to byte
code. Along with the Java compiler it contains more tools like Jar, JConsole, Javaw, etc.
These help tools help in various different aspects of developing a Java application. The JDK
bundle can be downloaded from the above given link in Oracle's website.

Just-in Time Compiler (JIT)


JIT compiler is a component that is part of the JVM and hence its implementation JRE. JIT
runs when the Java program is interpreted and its objective is to optimize the Java program
and make it efficient in terms of performance.

JVM becomes an instance of JRE at runtime of a Java program. It is widely


known as a runtime interpreter. The Java virtual machine (JVM) is the
cornerstone on top of which the Java technology is built upon. It is the
component of the Java technology responsible for its hardware and platform
independence. JVM largely helps in the abstraction of inner implementation
from the programmers who make use of libraries for their programmes from
JDK.

Just-in-time Compiler (JIT)


JIT is the part of the Java Virtual Machine (JVM) that is used to
speed up the execution time. JIT compiles parts of the byte code
that have similar functionality at the same time, and hence reduces
the amount of time needed for compilation. Here the term
“compiler” refers to a translator from the instruction set of a Java
virtual machine (JVM) to the instruction set of a specific CPU.
Parameter JDK JRE JVM

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: JAVA VIRTUAL MACHINE JIT: JUST-IN-TIME COMPILER

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

What is JIT in Java?


JIT in Java is an integral part of the JVM. 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.

The JIT compilation includes two approaches AOT (Ahead-of-Time compilation)


and interpretation to translate code into machine code. AOT compiler compiles the
code into a native machine language (the same as the normal compiler). It
transforms the bytecode of a VM into the machine code. The following optimizations
are done by the JIT compilers:

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

Advantages of JIT Compiler


o It requires less memory usages.
o The code optimization is done at run time.
o It uses different levels of optimization.
o It reduces the page faults.
Disadvantages of JIT Compiler
o It increases the complexity of the program.
o The program with less line of code does not take the benefit of the JIT compilation.
o It uses lots of cache memory.

Working of JIT Compiler


If the JIT compiler environment variable is properly set, the JVM reads the .class file
(bytecode) for interpretation after that it passes to the JIT compiler for further
process. After getting the bytecode, the JIT compiler transforms it into the native
code (machine-readable code).

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

Below is the code for the same in Java language.

/*Java Program to find factorial of a number using Recursive


Function*/

import java.util.Scanner;

public class Main

//Driver Code

public static void main(String[] args)

//Take input from the user

Scanner sc = new Scanner(System.in);

System.out.println("Enter the number :");

int num = sc.nextInt(); //Input the number

if(num>=0)

//Call a recursive function to find the factorial

int factorial=findFactorial(num);
System.out.println("The factorial of the entered
the number is :"+factorial);

else

System.out.println("Factorial not possible.");

System.out.println("Please enter valid input.");

//Recursive Function to Find the Factorial of a Number

public static int findFactorial(int num)

if(num==0)

return 1;

else if(num==1)

return 1;

else

return num*findFactorial(num-1);

Copy

Enter the number: 10


The factorial of the entered number is:3628800

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.

371 = 3*3*3 + 7*7*7 + 1*1*1


= 27 + 343 + 1
= 371

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.

Method 1: Armstrong Number Program in Java using While Loop


In this approach, we utilize a Java program to check whether a given number is an
Armstrong number. This method utilizes a while loop for the calculations.

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.

Enter a number: 371


371 is an Armstrong number

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.

What are the Java Operators?


Operators in Java are the symbols used for performing specific operations in Java.
Operators make tasks like addition, multiplication, etc which look easy although the
implementation of these tasks is quite complex.
Types of Operators in Java
There are multiple types of operators in Java all are mentioned below:
1. Arithmetic Operators
2. Unary Operators
3. Assignment Operator
4. Relational Operators
5. Logical Operators
6. Ternary Operator
7. Bitwise Operators
8. Shift Operators
9. instance of operator
1. Arithmetic Operators
They are used to perform simple arithmetic operations on primitive data types.
 * : Multiplication
 / : Division
 % : Modulo
 + : Addition
 – : Subtraction
Example:
Java
// Java Program to implement

// Arithmetic Operators

import java.io.*;

// Drive Class

class GFG {

// Main Function

public static void main (String[] args) {

// Arithmetic operators

int a = 10;

int b = 3;

System.out.println("a + b = " + (a + b));

System.out.println("a - b = " + (a - b));

System.out.println("a * b = " + (a * b));

System.out.println("a / b = " + (a / b));

System.out.println("a % b = " + (a % b));

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

public static void main(String[] args)

// Interger declared

int a = 10;

int b = 10;

// Using uniary operators


System.out.println("Postincrement : " + (a++));

System.out.println("Preincrement : " + (++a));

System.out.println("Postdecrement : " + (b--));

System.out.println("Predecrement : " + (--b));

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

public static void main(String[] args)

// Assignment operators

int f = 7;

System.out.println("f += 3: " + (f += 3));

System.out.println("f -= 2: " + (f -= 2));

System.out.println("f *= 4: " + (f *= 4));

System.out.println("f /= 3: " + (f /= 3));

System.out.println("f %= 2: " + (f %= 2));

System.out.println("f &= 0b1010: " + (f &= 0b1010));

System.out.println("f |= 0b1100: " + (f |= 0b1100));

System.out.println("f ^= 0b1010: " + (f ^= 0b1010));

System.out.println("f <<= 2: " + (f <<= 2));

System.out.println("f >>= 1: " + (f >>= 1));

System.out.println("f >>>= 1: " + (f >>>= 1));

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

Some of the relational operators are-


 ==, Equal to returns true if the left-hand side is equal to the right-hand
side.
 !=, Not Equal to returns true if the left-hand side is not equal to the right-
hand side.
 <, less than: returns true if the left-hand side is less than the right-hand
side.
 <=, less than or equal to returns true if the left-hand side is less than or
equal to the right-hand side.
 >, Greater than: returns true if the left-hand side is greater than the right-
hand side.
 >=, Greater than or equal to returns true if the left-hand side is greater
than or equal to the right-hand side.
Example:
Java
// Java Program to implement

// Relational Operators

import java.io.*;

// Driver Class

class GFG {

// main function

public static void main(String[] args)


{

// Comparison operators

int a = 10;

int b = 3;

int c = 5;

System.out.println("a > b: " + (a > b));

System.out.println("a < b: " + (a < b));

System.out.println("a >= b: " + (a >= b));

System.out.println("a <= b: " + (a <= b));

System.out.println("a == c: " + (a == c));

System.out.println("a != c: " + (a != c));

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

public static void main (String[] args) {

// Logical operators

boolean x = true;

boolean y = false;

System.out.println("x && y: " + (x && y));

System.out.println("x || y: " + (x || y));

System.out.println("!x: " + (!x));

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

// max of three numbers using

// ternary operator.

public class operators {

public static void main(String[] args)

int a = 20, b = 10, c = 30, result;

// result holds max of three

// numbers

result

= ((a > b) ? (a > c) ? a : c : (b > c) ? b : c);

System.out.println("Max of three numbers = "

+ 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

public static void main(String[] args)

// Bitwise operators

int d = 0b1010;

int e = 0b1100;

System.out.println("d & e: " + (d & e));

System.out.println("d | e: " + (d | e));

System.out.println("d ^ e: " + (d ^ e));

System.out.println("~d: " + (~d));

System.out.println("d << 2: " + (d << 2));

System.out.println("e >> 1: " + (e >> 1));

System.out.println("e >>> 1: " + (e >>> 1));

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-

number shift_op number_of_places_to_shift;

 <<, 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

public static void main(String[] args)

int a = 10;

// using left shift

System.out.println("a<<1 : " + (a << 1));

// using right shift

System.out.println("a>>1 : " + (a >> 1));

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 {

public static void main(String[] args)

Person obj1 = new Person();

Person obj2 = new Boy();

// As obj is of type person, it is not an

// instance of Boy or interface

System.out.println("obj1 instanceof Person: "

+ (obj1 instanceof Person));

System.out.println("obj1 instanceof Boy: "

+ (obj1 instanceof Boy));

System.out.println("obj1 instanceof MyInterface: "

+ (obj1 instanceof MyInterface));

// Since obj2 is of type boy,

// whose parent class is person

// and it implements the interface Myinterface

// it is instance of all of these classes


System.out.println("obj2 instanceof Person: "

+ (obj2 instanceof Person));

System.out.println("obj2 instanceof Boy: "

+ (obj2 instanceof Boy));

System.out.println("obj2 instanceof MyInterface: "

+ (obj2 instanceof MyInterface));

class Person {

class Boy extends Person implements MyInterface {

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

Precedence and Associativity of Java Operators


Precedence and associative rules are used when dealing with hybrid equations
involving more than one type of operator. In such cases, these rules determine which
part of the equation to consider first, as there can be many different valuations for
the same equation. The below table depicts the precedence of operators in decreasing
order as magnitude, with the top representing the highest precedence and the bottom
showing the lowest precedence.
Table 1: Java operators - precedence chart highest to lowest
Precedenc Operator Description Associativity
e
1 [] array index Left -> Right
() method call
. member access
2 ++ pre or postfix increment Right -> Left
-- pre or postfix decrement
+ - unary plus, minus
~ bitwise NOT
! logical NOT
3 (type cast) type cast Right -> Left
new object creation
4 * multiplication Left -> Right
/ division
% modulus (remainder)
5 + - addition, subtraction Left -> Right
+ string concatenation
6 << left shift Left -> Right
>> signed right shift
>>> unsigned or zero-fill right shift
7 < less than Left -> Right
<= less than or equal to
> greater than
>= greater than or equal to
instanceof reference test
8 == equal to Left -> Right
!= not equal to
9 & bitwise AND Left -> Right
10 ^ bitwise XOR Left -> Right
11 | bitwise OR Left -> Right
12 && logical AND Left -> Right
13 || logical OR Left -> Right
14 ?: conditional (ternary) Right -> Left
15 = assignment and short hand assignment Right -> Left
+= operators
-=
*=
/=
%=
&=
^=
|=
<<=
>>=
>>>=

5> difference between while loop and do while loop in java.


Solution :- while loop:
A while loop is a control flow statement that allows code to be executed repeatedly
based on a given Boolean condition. The while loop can be thought of as a repeating
if statement.
Syntax :
while (boolean condition)
{
loop statements...
}
Flowchart:

Example:
 Java

import java.io.*;

class GFG {
public static void main(String[] args)
{

int i = 5;

while (i < 10) {


i++;
System.out.println("GfG");
}
}
}

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

Condition is checked first then Statement(s) is executed atleast once,


statement(s) is executed. thereafter condition is checked.

It might occur statement(s) is executed At least once the statement(s) is


zero times, If condition is false. executed.

No semicolon at the end of while. Semicolon at the end of while.


while(condition) while(condition);

If there is a single statement, brackets


Brackets are always required.
are not required.

Variable in condition is initialized variable may be initialized before or


before the execution of loop. within the loop.

while loop is entry controlled loop. do-while loop is exit controlled loop.

while(condition) do { statement(s); }
{ statement(s); } while(condition);

You might also like