Chapter 1 Java Features
Chapter 1 Java Features
JAVA was developed by James Gosling at Sun Microsystems Inc in the year
1995, later acquired by Oracle Corporation. It is a simple programming language.
Java makes writing, compiling, and debugging programming easy. It helps to
create reusable code and modular programs. Java is a class-based, object-oriented
programming language.
Features of Java:
1. Simple
2. Object-Oriented
3. Portable
4. Platform independent
5. Secured
6. Robust
7. Architecture neutral
8. Interpreted
9. High Performance
10. Multithreaded
11. Distributed
12. Dynamic
Simple:
Java is very easy to learn, and its syntax is simple, clean and easy to understand.
According to Sun Microsystem, Java language is a simple programming language
because:
Java syntax is based on C++ (so easier for programmers to learn it after
C++).
Java has removed many complicated and rarely-used features, for example,
explicit pointers, operator overloading, etc.
There is no need to remove unreferenced objects because there is an
Automatic Garbage Collection in Java.
Object-oriented
1. Object
2. Class
3. Inheritance
4. Polymorphism
5. Abstraction
6. Encapsulation
Platform Independent:
Example:
Secured:
Java is best known for its security. With Java, we can develop virus-free systems.
Java is secured because:
No explicit pointer
Java Programs run inside a virtual machine sandbox
Classloader: Classloader in Java is a part of the Java Runtime
Environment (JRE) which is used to load Java classes into the Java
Virtual Machine dynamically. It adds security by separating the package
for the classes of the local file system from those that are imported from
network sources.
Bytecode Verifier: It checks the code fragments for illegal code that can
violate access rights to objects.
Security Manager: It determines what resources a class can access such
as reading and writing to the local disk.
Robust:
Java is portable because it facilitates you to carry the Java bytecode to any
platform.
It doesn't require any implementation. Java programs can be easily moved
from one system to another system, anywhere and anytime.
We can change and upgrade system processor and system resources
without any changes in java.
High-performance:
Distributed:
Dynamic:
Introduction:
The Java Development Kit (JDK) is software used for Java programming, along
with the Java Virtual Machine (JVM) and the Java Runtime Environment
(JRE). The JDK includes the compiler and class libraries, allowing developers
to create Java programs executable by the JVM and JRE.
Before installing the Java Development Kit, check if a Java version is already
installed on Windows. Follow the steps below:
Download the latest Java Development Kit installation file for Windows 10 to
have the latest features and bug fixes.
After downloading the installation file, proceed with installing Java on your
Windows system.
After running the installation file, the installation wizard welcome screen
appears.
To set the temporary path of JDK, you need to follow the following steps:
For Example:
set path=C:\Program Files\Java\jdk1.6.0_23\bin
Compile: javac filename.java
ClassLoader:
The heap area is a part of the JVM memory and is created when the
JVM starts up. Its size cannot be static because it increases or decrease
during the application runs.
Stack:
Native Stack:
Execution Engine:
It is the central part of the JVM. Its main task is to execute the byte
code and execute the Java classes. The execution engine has three main
components used for executing Java classes.
Interpreter:
JIT Compiler:
3.Interpreter:
Once the classes get loaded and the code gets verified, then interpreter reads the
assembly code line by line and does the following two functions:
JDK contains:
When object is unreferenced then the JVM send into garbage collector to
destroy the object.
we were using free() function in C language and delete() in C++. But, in java it
is performed automatically. So, java provides better memory management.
Advantage of Garbage Collection:
o It makes java memory efficient because garbage collector removes the
unreferenced objects from heap memory.
o It is automatically done by the garbage collector(a part of JVM) so we
don't need to make extra efforts.
finalize() method:
The finalize() method is invoked each time before the object is garbage collected.
This method can be used to perform cleanup processing.
Syntax:
……………….
……………….
gc() method:
The gc() method is used to invoke the garbage collector to perform cleanup
processing. The gc() is found in System and Runtime classes.
Syntax:
………………
………………
}
Example:
class TestGarbage1
{
public void finalize()
{
System.out.println("object is garbage collected");
}
public static void main(String args[])
{
TestGarbage1 s1=new TestGarbage1();
TestGarbage1 s2=new TestGarbage1();
s1=null;
s2=null;
System.gc();
}
}
Output:
object is garbage collected
object is garbage collected
Lexical Tokens/ Lexical Issues in java
A lexical token may consist of one or more characters, and every single character
is in exactly one token.
The tokens can be keywords, comments, numbers, white space, or strings. All
lines should be terminated by a semi-colon (;).
White Space
White space can contain the characters for tabs, blanks, newlines, and form feeds.
These characters are ignored except when they serve to separate other tokens.
However, blanks and tabs are significant in strings.
Comments
1. Single line comments begin with the token // and end with a carriage return.
For example, //this is the single-line syntax.
2. Multi-Line comments begin with the token /* and end with the token */
For example, /* this is multiline syntax*/
Numbers:
Integer Number
Real Numbers
Signed and Unsigned Numbers
Negative Numbers
Identifiers:
The identifier is the name used to define the object, such as a function, module,
or register. Identifiers should begin with alphabetical characters or underscore
characters.
Escaped Identifiers:
Verilog HDL allows any character to be used in an identifier by escaping the
identifier.
Escaped identifiers begin with the backslash (\). The backslash escapes the
entire identifier.
Operators are special characters used to put conditions or to operate the variables.
There are one, two, and sometimes three characters used to perform operations
on variables.
1. Arithmetic Operators:
These operators perform arithmetic operations. The + and -are used as either
unary (x) or binary (z-y) operators.
2. Relational Operators:
These operators compare two operands and return the result in a single bit, 1 or
0. The Operators included in relational operation are:
== (equal to)
!= (not equal to)
(greater than)
>= (greater than or equal to)
< (less than)
<= (less than or equal to)
3. Bit-wise Operators:
Logical operators are bit-wise operators and are used only for single-bit operands.
They return a single bit value, 0 or 1. They can work on integers or groups of bits,
expressions and treat all non-zero values as 1.
Logical operators are generally used in conditional statements since they work
with expressions. The operators included in Logical operation are:
! (logical NOT)
&& (logical AND)
|| (logical OR)
5. Reduction Operators:
Reduction operators are the unary form of the bitwise operators and operate on
all the bits of an operand vector. These also return a single-bit value. The
operators included in Reduction operation are:
6. Shift Operators:
Shift operators are shifting the first operand by the number of bits specified by
the second operand in the syntax.
Vacant positions are filled with zeros for both directions, left and right shifts
(There is no use sign extension). The Operators included in Shift operation are:
{ }(concatenation)
8. Replication Operator:
The replication operator is making multiple copies of an item. The operator used
in Replication operation is:
9. Conditional Operator:
(Condition) ?
Operands:
1. Literals
Literals are constant-valued operands that are used in Verilog expressions. The
two commonly used Verilog literals are:
Wires, regs, and parameters are the data types used as operands in Verilog
expressions. Bit-Selection "x[2]" and Part-Selection "x[4:2]"
Bit-selects and part-selects are used to select one bit and multiple bits,
respectively, from a wire, regs or parameter vector using square brackets "[ ]".
3. Function Calls:
It just places the function call as one of the types of operands. It is useful to know
the bit width of the return value of the function call.
Keywords
Qns:
What is a keyword and explain different types of keywords in
java
Keywords or Reserved words are the words in a language that are used for
some internal process or represent some predefined actions. These words are
therefore not allowed to use as variable names or objects.
1. abstract:
2. boolean:
Java break keyword is used to break the loop or switch statement. It breaks
the current flow of the program at specified conditions.
4. byte:
Java byte keyword is used to declare a variable that can hold 8-bit data
values.
5. case:
Java case keyword is used with the switch statements to mark blocks of
text.
6. catch:
7. char:
Java char keyword is used to declare a variable that can hold unsigned 16-
bit Unicode characters
8. class:
9. 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.
10. default:
Java default keyword is used to specify the default block of code in a switch
statement.
11. do:
12. double:
Java double keyword is used to declare a variable that can hold 64-bit
floating-point number.
13. else:
14. enum:
15. extends:
16. final:
Java final keyword is used to indicate that a variable holds a constant value.
It is used with a variable. It is used to restrict the user from updating the
value of the variable.
17. finally:
18. float:
Java float keyword is used to declare a variable that can hold a 32-bit
floating-point number.
19. for:
Java for keyword is used to start a for loop. It is used to execute a set of
instructions/functions repeatedly when some condition becomes true. If the
number of iteration is fixed, it is recommended to use for loop.
20. if:
Java if keyword tests the condition. It executes the if block if the condition
is true.
21. implements:
22. import:
Java import keyword makes classes and interfaces available and accessible
to the current source code.
23. instanceof:
24. int:
Java int keyword is used to declare a variable that can hold a 32-bit signed
integer.
25. interface:
26. long:
Java long keyword is used to declare a variable that can hold a 64-bit
integer.
27. native:
28. new:
29. null:
Java null keyword is used to indicate that a reference does not refer to
anything. It removes the garbage value.
30. package:
Java package keyword is used to declare a Java package that includes the
classes.
31. private:
32. protected:
33. public:
34. return:
Java return keyword is used to return from a method when its execution is
complete.
35. short:
Java short keyword is used to declare a variable that can hold a 16-bit
integer.
36. static:
37. strictfp:
38. super:
39. switch:
The Java switch keyword contains a switch statement that executes code
based on test value. The switch statement tests the equality of a variable
against multiple values.
40. synchronized:
41. this:
Java this keyword can be used to refer the current object in a method or
constructor.
42. throw:
43. throws:
44. transient:
45. 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.
46. void:
Java void keyword is used to specify that a method does not have a return
value.
47. volatile:
48. while:
Java while keyword is used to start a while loop. This loop iterates a part
of the program several times. If the number of iterations is not fixed, it is
recommended to use the while loop.
Sample program in Java:
In this section, we will learn how to write the simple program of Java. We can
write a simple hello Java program easily.
class Simple
{
public static void main(String args[])
{
System.out.println("Hello Java");
}
}
Output:
Hello Java