You are on page 1of 12

JAVA ASSIGNMENT

Data types with size and range


Primitive Data type:

Data Type Size Description

byte 1 byte Stores whole numbers from -128 to 127

short 2 bytes Stores whole numbers from -32,768 to


32,767

int 4 bytes Stores whole numbers from -2,147,483,648


to 2,147,483,647

long 8 bytes Stores whole numbers from -


9,223,372,036,854,775,808 to
9,223,372,036,854,775,807

float 4 bytes Stores fractional numbers. Sufficient for


storing 6 to 7 decimal digits

double 8 bytes Stores fractional numbers. Sufficient for


storing 15 decimal digits

boolean 1 bit Stores true or false values

char 2 bytes Stores a single character/letter or ASCII


values.
Type Casting
Assigning value of one type to a variable of another type.
When assigning value of one data type to another:

1) If data types are compatible - JAVA performs conversion automatically ->


Automatic type conversion(Widening or implicit).

Byte -> Short -> Char -> Int -> Long -> Float -> Double

Implicit casting takes place under two conditions:

-> Compatibility of the data types.

->If the targeted value to be converted has a smaller length e.g. 4 bytes, to a
larger data type e.g. 8 bytes.

2) If data types are not compatible-They need to be converted -> Explicit


Conversion(Narrowing).

Double -> FLoat -> Long -> Int -> Char -> Short -> Byte

Explicit casting takes place under two conditions:

-> Compatibility of the data types.

-> Assigning a data type of high range to a lower range.

Features of JAVA

Object Oriented

In Java, everything is an Object. Java can be easily extended since it is based on the Object
model.
Platform Independent

Unlike many other programming languages including C and C++, when Java is compiled, it is
not compiled into platform specific machine, rather into platform-independent bytecode.
Simple

Java is designed to be easy to learn.


Secure

With Java's secure feature it enables to develop virus-free, tamper-free systems. Authentication
techniques are based on public-key encryption.
Robust

Java makes an effort to eliminate error-prone situations by emphasising mainly on compile time
error checking and runtime checking
Multithreaded

With Java's multithreaded feature it is possible to write programs that can perform many tasks
simultaneously. This design feature allows the developers to construct interactive applications
that can run smoothly.
High Performance

With the use of Just-In-Time compilers, Java enables high performance.

What is JDK,JRE.JIT,JVM
JAVA DEVELOPMENT KIT (JDK)

Java Development Kit The Java Development Kit (JDK) is a software development
environment used for developing Java applications. It includes the Java Runtime Environment
(JRE), an interpreter/loader (Java), a compiler (javac), an archiver (jar), a documentation
generator (Javadoc) and other tools needed in Java development.

JAVA RUNTIME ENVIRONMENT (JRE)

The Java Runtime Environment provides the minimum requirements for executing a Java
application; it consists of the Java Virtual Machine (JVM), core classes, and supporting files.

JAVA VIRTUAL MACHINE (JVM)


It is called JAVA platform because it is responsible to execute our JAVA byte code.Contains
interpreter and JIT.Runs JAVA byte code by converting them into current OS language.

JAVA IN TIME COMPILER (JIT)

Helps interpreter to execute JAVA byte code.Basically improves performance of JAVA program
execution.

How JVM works


Three specific component :-

1) Class Loader - Responsible for loading .class file to memory.

It is mainly responsible for three activities.

• Loading • Linking • Initialization

-> Loading : The Class loader reads the .class file, generate the corresponding binary data and
save it in method area. After loading .class file, JVM creates an object of type Class to represent
this file in the heap memory.

• Bootstrap class loader- Responsible for loading .class file from RT.jar and saving in memory.

• Extension class loader : It is child of bootstrap class loader. Responsible for loading the
additional class file say from JRE,LIB or from EXT folder.

• System/Application class loader : It is child of extension class loader. It is responsible to load


classes from application class path.

-> Linking : Performs verification, preparation, and (optionally) resolution. • Verification : It


ensures the correctness of .class file i.e. it check whether this file is properly formatted and
generated by valid compiler or not.

• Preparation : JVM allocates memory for class variables and initializing the memory to default
values.

• Resolution : It is the process of replacing symbolic references from the type with direct
references.

-> Initialization : In this phase, all static variables are assigned with their values defined in the
code

2) Run Time Data Area

JVM needs memory area to store class file.This run time data areas has many types of memories
like method area,heap memory area,stack memory area,PC register,native memory.

Method area : In method area, all class level information like class name.

Heap area : Information of all objects is stored in heap area.

Stack area : For every thread, JVM create one run-time stack which is stored here.

PC Registers : Store address of current execution instruction of a thread.

Native method stacks : For every thread, separate native stack is created. It stores native method
information.

3) Execution Engine

Execution engine execute the .class (bytecode). It reads the byte-code line by line, use data and
information present in various memory area and execute instructions. It can be classified in
three parts :-

• Interpreter : It interprets the bytecode line by line and then executes. The disadvantage here
is that when one method is called multiple times, every time interpretation is required.

• Just-In-Time Compiler(JIT) : It is used to increase efficiency of interpreter.It compiles the


entire bytecode and changes it to native code so whenever interpreter see repeated method
calls,JIT provide direct native code for that part so re-interpretation is not required,thus
efficiency is improved.

• Garbage Collector : Responsible of collecting/destroying objects which is no longer used.

Class Types
In Java, the class is a blueprint from which we can create an individual object. Java
provides a keyword named class by which we can declare a class. Inside the class, we
define class members and functions. It is not possible to create Java programs without
class.

1. Final Class

When a variable, function, or class is declared final, its value persists throughout the
program. Declaring a method with the final keyword indicates that the method cannot be
overridden by subclasses.

2. Static Class

Static is a Java word that explains how objects are kept in memory.A static class has
only static members. An object cannot be created for a static class.

3. Abstract Class

A class that has zero or more abstract methods and is specified with the abstract
keyword is called an abstract class. We must rigorously extend the abstract classes to a
concrete class in order to use them because they are incomplete classes.

4. Concrete Class
A normal class with an implementation for all of its methods and no abstract methods is
called a concrete class.

5. POJO Class

A Plain Old Java Object class has only private variables with setter and getter methods
to access them.

7. Inner Class

We can define a class within a class in Java, and these classes are referred to as
nested classes.

Constructor Types
A constructor in Java is similar to a method that is invoked when an object of the class
is created.A constructor has the same name as that of the class and does not have any
return type

Ex: class Test {

Test() {

// constructor body

In Java, constructor can be divided into three types:

• No-Arg Constructor
• Parameterized Constructor

1. No-argument constructor: A constructor that has no parameter is known as the


default constructor. If we don’t define a constructor in a class, then the compiler creates
a default constructor(with no arguments) for the class.

2. Parameterized Constructor: A constructor that has parameters is known as


parameterized constructor. If we want to initialise fields of the class with our own values,
then use a parameterized constructor.

Constructor Chaining
Constructor chaining is the process of calling one constructor from another constructor
with respect to current object.

One of the main use of constructor chaining is to avoid duplicate codes while having
multiple constructor (by means of constructor overloading) and make code more
readable.

Constructor chaining can be done in two ways:

• Within same class: It can be done using this() keyword for constructors in the
same class
• From base class: by using super() keyword to call the constructor from the
base class.

This process is used when we want to perform multiple tasks in a single


constructor rather than creating a code for each task in a single constructor we
create a separate constructor for each task and make their chain which makes
the program more readable.

Rules of constructor chaining :

• The this() expression should always be the first line of the constructor.
• There should be at-least be one constructor without the this() keyword
(constructor 3 in above example).
• Constructor chaining can be achieved in any order.
WRAPPER CLASS

A Wrapper class is a class which contains the primitive data types (int, char, short, byte,
etc). In other words, wrapper classes provide a way to use primitive data types (int,
char, short, byte, etc) as objects. Autoboxing
and unboxing feature convert primitives into objects and objects into primitives
automatically. The automatic conversion of primitive into an object is known as
autoboxing and vice-versa unboxing.

Primitive Type Wrapper class

boolean Boolean

char Character

byte Byte

short Short

int Integer

long Long

float Float
double Double

How will you achieve data encapsulation

Encapsulation is defined as the wrapping up of data under a single unit. It is the


mechanism that binds together code and the data it manipulates. Another way to think
about encapsulation is, that it is a protective shield that prevents the data from being
accessed by the code outside this shield. Technically in
encapsulation, the variables or data of a class is hidden from any other class and can
be accessed only through any member function of its own class in which it is declared.It
is also known as a combination of data-hiding and abstraction. Encapsulation can be
achieved by Declaring all the variables in the class as private and writing public
methods in the class to set and get the values of variables.It is more defined with the
setter and getter method.

Data Abstraction

Data abstraction is the process of hiding certain details and showing only
essential information to the user.Abstraction can be achieved with abstract
classes The
abstract keyword is a non-access modifier, used for classes and methods:

• Abstract class: is a restricted class that cannot be used to create objects (to
access it, it must be inherited from another class).
• Abstract method: can only be used in an abstract class, and it does not have a
body. The body is provided by the subclass (inherited from).

Difference Between Primitive and Reference data types

Primitive types are the basic types of data: byte, short, int, long, float, double, boolean,
char. Primitive variables store primitive values.

Reference types are any instantiable class as well as arrays: String, Scanner, Random,
Die, int[], String[], etc. Reference variables store addresses to locations in memory for
where the data is stored.

You might also like