Java Imp 1st Unit
Java Imp 1st Unit
Object Oriented
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
Architecture-neutral
Portable
Multithreaded
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
Distributed
Dynamic
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.
Excution engine
It contains
A virtual processor
3 question
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
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.
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:
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:
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.
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.
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.
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.
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.
Example:
String s = “GeeksforGeeks”;
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.
• 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.
• The size of an array must be specified by an int value and not long or
short.
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.
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.
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.
• +=, 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
• <, 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
• &&, Logical AND: returns true when both conditions are true.
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
• &&, Logical AND: returns true when both conditions are true.
Ternary operator
The ternary operator is a shorthand version of the if-else statement. It has
three operands and hence the name Ternary.
• &, Bitwise AND operator: returns bit by bit AND of input values.