You are on page 1of 5

Java Assignment – I

Date: 21-Oct-2022

1) What is the difference between compiler and interpreter? Which one is better?
Explain.

Compiler Interpreter
Translate the whole source code Translate the whole source code line
at once. by line.

Compiler takes more time to Interpreter takes less time in


analyze source code but it is comparative to compiler to analyze
overall speed is faster than source code, but overall speed is slower
interpreter. than complier.

Compiler takes extra memory as Interpreter takes less memory in


it generates a separate comparative to compiler.
executable file.

Compiler shows all errors at once, Interpreter runs the code line by line,
making it harder to debug. it displays the error when it gets it,
which makes it easier to debug.

Compiler stores the machine It doesn’t save the machine language


language on the disk in the form at all.
of machine code

Programming languages like C, Programming languages like


C++ and Java use Compiler. JavaScript, Python and Ruby use
Interpreter.

CPU utilization is more. CPU utilization is less.

AYUSH DIGGIWAL
Compilers and interpreters both are used to convert a program written in a high-
level language into machine code understood by computers. Compilers usually take
a large amount of time to analyze the source code. However, the overall execution
time is comparatively faster than interpreters. On the other hand, Interpreters
usually take less amount of time to analyze the source code. In case of using
compilers, the program codes are translated into machine code already and hence
the time to execute the code is very less. That’s why Compiler is better than
Interpreter.

2) What are the different data types in java?

There are two types of data types in Java :


 Primitive Data Types
 Non-Primitive Data Types

 Primitive Data Types :


There are some Primitive data types in Java. They can be split into
four categories by the kind of value they hold:
 Integers: these are positive and negative whole numbers.
 Floating Point Numbers: any number that has a fractional part.
 Characters: a single character. Truth Values: either true or false.

Integers hold number values that cannot have a fractional part.


There are four different types:

 byte: uses one byte to store values from -128 to 127


 short: uses two bytes to store values from -32,768 to 32,767
 int: uses four bytes to store values from -2,147,483,648 to
2,147,483,647
 long: uses eight bytes to store values from -
9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Floating Point NumbersUnlike integers, floating point numbers like


fractional parts. There are two different types:
 float: uses four bytes to store values from -3.4028235E+38 to
3.4028235E+38
 double: uses eight bytes to store values from
1.7976931348623157E+308 to 1.7976931348623157E+308

AYUSH DIGGIWAL
Characters There is only one primitive data type that deals with individual
characters – the char. The char can hold the value of one character and is
based on 16-bit Unicode encoding. The character might be a letter, digit,
punctuation, a symbol or a control character (e.g., a character value that
represents a newline or a tab).

 Non-Primitive Data Type:


The Reference Data Types will contain a memory address of variable
values because the reference types won’t store the variable value directly in
memory. They are strings, objects, arrays, etc.

 String: These are defined as an array of characters. String is designed


to hold a sequence of characters in a single variable whereas, a
character array is a collection of separate char type entities. Unlike
C/C++, Java strings are not terminated with a null character.

 Object: It is a basic unit of Object-Oriented Programming and


represents real-life entities. A typical Java program creates many
objects, which as you know, interact by invoking methods.

 Array: An array is a group of like-typed variables that are referred to


by a common name. Arrays in Java work differently than they do in
C/C++. Java array can also be used as a static field, a local variable, or
a method parameter. The size of an array must be specified by an int
value and not long or short.

3) What are the different keywords in java?

Following are the different type of keywords in Java:

 Final Keyword :
In Java, the final keyword can be used while declaring an entity. Using
the final keyword means that the value can’t be modified in the future. If a
variable is declared with the final keyword, its value cannot be changed once
initialized. Note that the variable does not necessarily have to be initialized at
the time of declaration. If it’s declared but not yet initialized, it’s called a
blank final variable.

AYUSH DIGGIWAL
 Abstract:
Java abstract keyword is used to declare an abstract class. An abstract
class can provide the implementation of the interface. It can have abstract
and non-abstract methods. An abstract method can only be used in an
abstract class, and it does not have a body.

 Super:
The super keyword is used to call the super or parent class or objects.
Java super keyword is a reference variable that is used to refer to parent class
objects. It can be used to invoke the immediate parent class method.

 Break :
Java break keyword is used to break the loop or switch statement. It
breaks the current flow of the program at specified conditions.

 Continue:
Java continue keyword is used to continue the loop. It continues the
current flow of the program and skips the remaining code at the specified
condition.

 try:
Java try keyword is used to start a block of code that will be tested for
exceptions. The try block must be followed by either catch or finally block.

 catch:
The catch block is used to handle the uncertain condition of a try
block. A try block is always followed by a catch block, which handles the
exception that occurs in the associated try block.

 throws:
The throws keyword is used for exception handling without try &
catch block. It specifies the exceptions that a method can throw to the caller
and does not handle itself.

 Package:
A java package is a group of similar types of classes, interfaces and
sub-packages. Package in java can be categorized in two form, built-in
package and user-defined package. There are many built-in packages such as
java, lang, awt, javax etc.

AYUSH DIGGIWAL
4) What do you mean by JVM?

A Java virtual machine is a virtual machine that enables a computer to run Java
programs as well as programs written in other languages that are also compiled to
Java byte code. The JVM is detailed by a specification that formally describes what is
required in a JVM implementation. Java Virtual Machine is an abstract machine. It is
a specification that provides runtime environment in which java byte code can be
executed. JVMs are available for many hardware and software platforms i.e. JVM is
platform dependent.
It is:
 A specification where working of Java Virtual Machine is specified. But
implementation provider is independent to choose the algorithm. Its
implementation has been provided by Oracle and other companies.
 An implementation its implementation is known as JRE (Java Runtime
Environment).
 Runtime Instance Whenever you write java command on the command
prompt to run the java class, an instance of JVM is created.

The JVM performs following operation:

 Loads code
 Verifies code
 Executes code
 Provides runtime environment

JVM provides definitions for the:

 Memory area
 Class file format
 Register set
 Garbage-collected heap
 Fatal error reporting etc.

AYUSH DIGGIWAL

You might also like