0% found this document useful (0 votes)
51 views20 pages

Java Imp 1st Unit

The document discusses the key features of the Java programming language including that it is object-oriented, platform independent, simple, secure, architecture-neutral, portable, robust, multithreaded, interpreted, and high performance. It also discusses the Java Virtual Machine architecture including the classloader, class area, heap, stack, program counter register, and native method stack.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views20 pages

Java Imp 1st Unit

The document discusses the key features of the Java programming language including that it is object-oriented, platform independent, simple, secure, architecture-neutral, portable, robust, multithreaded, interpreted, and high performance. It also discusses the Java Virtual Machine architecture including the classloader, class area, heap, stack, program counter register, and native method stack.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Features of java

Following are the notable 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 byte code. This byte code is distributed over the web and interpreted
by the Java Virtual Machine (JVM) on whichever platform it is being run on.

Simple

Java is designed to be easy to learn. If you understand the basic concept of OOP
Java, it would be easy to master.

Secure

With Java's secure feature it enables to develop virus-free, tamper-free systems.


Authentication techniques are based on public-key encryption.

Architecture-neutral

Java compiler generates an architecture-neutral object file format, which makes


the compiled code executable on many processors, with the presence of Java
runtime system.

Portable

Being architecture-neutral and having no implementation dependent aspects of


the specification makes Java portable. The compiler in Java is written in ANSI C
with a clean portability boundary, which is a POSIX subset.
Robust

Java makes an effort to eliminate error-prone situations by emphasizing 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.

Interpreted

Java byte code is translated on the fly to native machine instructions and is not
stored anywhere. The development process is more rapid and analytical since the
linking is an incremental and light-weight process.

High Performance

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

Distributed

Java is designed for the distributed environment of the internet.

Dynamic

Java is considered to be more dynamic than C or C++ since it is designed to adapt


to an evolving environment. Java programs can carry an extensive amount of
run-time information that can be used to verify and resolve accesses to objects
at run-time.

2 question
JVM Architecture
Let's understand the internal architecture of JVM. It contains classloader, memory area,
execution engine etc.

1) Classloader
Classloader is a subsystem of JVM which is used to load class files. Whenever we run the
java program, it is loaded first by the classloader. There are three built-in classloaders in
Java.
1. Bootstrap ClassLoader: This is the first classloader which is the super class of Extension
classloader. It loads the rt.jar file which contains all class files of Java Standard Edition like
java.lang package classes, java.net package classes, java.util package classes, java.io
package classes, java.sql package classes etc.
2. Extension ClassLoader: This is the child classloader of Bootstrap and parent classloader
of System classloader. It loades the jar files located
inside $JAVA_HOME/jre/lib/ext directory.
3. System/Application ClassLoader: This is the child classloader of Extension classloader. It
loads the classfiles from classpath. By default, classpath is set to current directory. You can
change the classpath using "-cp" or "-classpath" switch. It is also known as Application
classloader
4. These are the internal classloaders provided by Java. If you want to create your own
classloader, you need to extend the ClassLoader class.

2) Class(Method) Area
5. Class(Method) Area stores per-class structures such as the runtime constant pool,
field and method data, the code for methods.

3) Heap
6. It is the runtime data area in which objects are allocated.

4) Stack
7. Java Stack stores frames. It holds local variables and partial results, and plays a part
in method invocation and return.
8. Each thread has a private JVM stack, created at the same time as thread.
9. A new frame is created each time a method is invoked. A frame is destroyed when
its method invocation completes.

5) Program Counter Register


10. PC (program counter) register contains the address of the Java virtual machine
instruction currently being executed.
6) Native Method Stack
11. It contains all the native methods used in the application.

Excution engine

It contains

A virtual processor

Interpreter: Read bytecode stream then execute the instructions.

Just-In-Time(JIT) compiler: It is used to improve the performance. 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.

8) Java Native Interface

Java Native Interface (JNI) is a framework which provides an interface to


communicate with another application written in another language like C, C++,
Assembly etc. Java uses JNI framework to send output to the Console or interact
with OS libraries.

3 question

Data Types in Java


Data types specify the different sizes and values that can be stored in the variable. There
are two types of data types in Java:

1. Primitive data types: The primitive data types include boolean, char, byte, short, int, long,
float and double.
2. Non-primitive data types: The non-primitive data types include Classes, Interfaces,
and Arrays.
Java Primitive Data Types
In Java language, primitive data types are the building blocks of data manipulation. These
are the most basic data types available in Java language.

>Note

Java is a statically-typed programming language. It means, all variables must be


declared before its use. That is why we need to declare variable's type and name.

There are 8 types of primitive data types:

Boolean data type

Byte data type

Char data type

Short data type


Int data type

Long data type

Float data type

Double data type

Java Data Types

Data Type Default Value Default size

Boolean Data Type

The Boolean data type is used to store only two possible values: true and false. This data type is
used for simple flags that track true/false conditions.
The Boolean data type specifies one bit of information, but its “size” can’t be defined precisely.

Example: Boolean one = false

Byte Data Type

The byte data type is an example of primitive data type. It isan 8-bit signed two’s complement
integer. Its value-range lies between -128 to 127 (inclusive). Its minimum value is -128 and
maximum value is 127. Its default value is 0.

The byte data type is used to save memory in large arrays where the memory savings is most
required. It saves space because a byte is 4 times smaller than an integer. It can also be used in
place of “int” data type.

Example:

Byte a = 10, byte b = -20

Short Data Type

The short data type is a 16-bit signed two’s complement integer. Its value-range lies between -
32,768 to 32,767 (inclusive). Its minimum value is -32,768 and maximum value is 32,767. Its default
value is 0.

The short data type can also be used to save memory just like byte data type. A short data type is
2 times smaller than an integer.

Example:

Short s = 10000, short r = -5000


Int Data Type

The int data type is a 32-bit signed two’s complement integer. Its value-range lies between –
2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1) (inclusive). Its minimum value is –
2,147,483,648and maximum value is 2,147,483,647. Its default value is 0.

The Int data type is generally used as a default data type for integral values unless if there is no
problem about memory.

Example: Int a = 100000, int b = -200000

Long Data Type

The long data type is a 64-bit two’s complement integer. Its value-range lies between -
9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -1)(inclusive). Its minimum
value is – 9,223,372,036,854,775,808and maximum value is 9,223,372,036,854,775,807. Its default
value is 0. The long data type is used when you need a range of values more than those provided
by int.

Float Data Type

The float data type is a single-precision 32-bit IEEE 754 floating point.Its value range is unlimited.
It is recommended to use a float (instead of double) if you need to save memory in large arrays
of floating point numbers. The float data type should never be used for precise values, such as
currency. Its default value is 0.0F.

Example: Float f1 = 234.5f

Double Data Type


The double data type is a double-precision 64-bit IEEE 754 floating point. Its value range is
unlimited. The double data type is generally used for decimal values just like float. The double
data type also should never be used for precise values, such as currency. Its default value is 0.0d.

Example: Double d1 = 12.3

Char Data Type

The char data type is a single 16-bit Unicode character. Its value-range lies between ‘\u0000’ (or
0) to ‘\uffff’ (or 65,535 inclusive).The char data type is used to store characters.

Example: Char letterA = ‘A’

Non-Primitive Data Type

Strings

Strings are defined as an array of characters. The difference between a character array and a string in
Java is, that the 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.

Syntax: Declaring a string

<String_Type> <string_variable> = “<sequence_of_string>”;

Example:

// Declare String without using new operator

String s = “GeeksforGeeks”;

// Declare String using new operator

String s1 = new String(“GeeksforGeeks”);


Class

A class is a user-defined blueprint or prototype from which objects are created. It represents the set
of properties or methods that are common to all objects of one type. In general, class declarations can
include these components, in order:

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++. The following are
some important points about Java arrays.

• In Java, all arrays are dynamically allocated. (discussed below)

• Since arrays are objects in Java, we can find their length using member
length. This is different from C/C++ where we find length using size.

• A Java array variable can also be declared like other variables with []
after the data type.

• The variables in the array are ordered and each has an index beginning
with 0.

• 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.

• The direct superclass of an array type is Object.

• Every array type implements the


interfaces Cloneable and java.io.Serializable.

Example program
Operators in Java
Java provides many types of operators which can be used according to
the need. They are classified based on the functionality they provide. In
this article, we will learn about Java Operators and learn all their types.

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:

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.

o Post-Increment: Value is first used for computing the result


and then incremented.

o 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.

o Post-decrement: Value is first used for computing the result


and then decremented.

o Pre-Decrement: The value is decremented first, and then the


result is computed.

•! : Logical not operator, used for inverting a boolean value.

Example:
. 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:

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:

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

Relational 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 trees.

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

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
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.

You might also like