Introduction To Java
Introduction To Java
Overview of Java
Java is one of the world's most important and widely used computer languages, and it has held this
distinction for many years. Unlike some other computer languages whose influence has weared with
passage of time, while Java's has grown. Java is one of the most popular programming languages in use,
particularly for client-server web applications, with a reported 9 million developers using and working on
it.
Creation of Java
Java was developed by James Ghosling, Patrick Naughton, Mike Sheridan at Sun Microsystems Inc. in
1991. It took 18 months to develop the first working version. The initial name was Oak but it was
renamed to Java in 1995 as OAK was a registered trademark of another Tech company.
Evolution of Java
Java was initially launched as Java 1.0 but soon after its initial release, Java 1.1 was launched. Java 1.1
redefined event handling, new library elements were added. In Java 1.2 Swing and Collection framework
was added and suspend(), resume() and stop() methods were deprecated from Thread class. No major
changes were made into Java 1.3 but the next release that was Java 1.4 contained several important
changes. Keyword assert, chained exceptions and channel based I/O System was introduced.
Java 1.5 was called J2SE 5, it added following major new features :
• Generics
• Annotations
• Enumerations
• For-each Loop
• Varargs
• Static Import
• Formatted I/O
• Concurrency utilities
Next major release was Java SE 7 which included many new changes, like :
• try-with-resource statement
• Binary Integer Literals
Java SE 8 was released on March 18, 2014. Some of the major new features introduced in JAVA 8 are,
• Lambda Expressions
• Enhanced Security
Java 9 was released in 2017. Java 9 would include better support for multi-gigabyte heaps, better
native code integration, a different default garbage collector and a self-tuning JVM. It includes
some specific features:
Java 13 arrives with improvements to garbage collection, application class-data sharing, and text blocks.
Some of the important Java 13 features are:
Application of Java
Java is widely used in every corner of world and of human life. Java is not only used in softwares but is
also widely used in designing hardware controlling software components. There are more than 930
million JRE downloads each year and 3 billion mobile phones run java.
Features of Java
The prime reason behind creation of Java was to bring portability and security feature into a computer
language. Beside these two major features, there were many other features that played an important role
in moulding out the final form of this outstanding language. Those features are :
1) Simple
Java is easy to learn and its syntax is quite simple, clean and easy to understand.The confusing and
ambiguous concepts of C++ are either left out in Java or they have been re-implemented in a cleaner way.
Eg : Pointers and Operator Overloading are not there in java but were an important part of C++.
2) Object Oriented
In java everything is Object which has some data and behaviour. Java can be easily extended as it is based
on Object Model.
3) Robust
Java makes an effort to eliminate error prone codes by emphasizing mainly on compile time error
checking and runtime checking. But the main areas which Java improved were Memory Management and
mishandled Exceptions by introducing automatic Garbage Collector and Exception Handling.
4) Platform Independent
Unlike other programming languages such as C, C++ etc which are compiled into platform specific
machines. Java is guaranteed to be write-once, run-anywhere language. On compilation Java program is
compiled into bytecode. This bytecode is platform independent and can be run on any machine, plus this
bytecode format also provide security. Any machine with Java Runtime Environment can run Java
Programs.
5) Secure
When it comes to security, Java is always the first choice. With java secure features it enable us to
develop virus free, temper free system. Java program always runs in Java runtime environment with
almost null interaction with system OS, hence it is more secure.
6) Multi Threading
Java multithreading feature makes it possible to write program that can do many tasks simultaneously.
Benefit of multithreading is that it utilizes same memory and other resources to execute multiple threads
at the same time, like While typing, grammatical errors are checked along.
7) Architectural Neutral
Compiler generates bytecodes, which have nothing to do with a particular computer architecture, hence a
Java program is easy to intrepret on any machine.
8) Portable
Java Byte code can be carried to any platform. No implementation dependent features. Everything related
to storage is predefined, example: size of primitive data types
9) High Performance
Java is an interpreted language, so it will never be as fast as a compiled language like C or C++. But, Java
enables high performance with the use of just-in-time compiler.
An Environment variable is a dynamic "object" on a computer that stores a value(like a key-value pair),
which can be referenced by one or more software programs in Windows. Like for Java, we will set an
environment variable with name "java" and its value will be the path of the /bin directory present in Java
directory. So whenever a program will require Java environment, it will look for the java environment
variable which will give it the path to the execution directory.
Java virtual Machine(JVM) is a virtual Machine that provides runtime environment to execute java byte
code. The JVM doesn't understand Java typo, that's why you compile your *.java files to obtain *.class
files that contain the bytecodes understandable by the JVM. JVM control execution of every Java
program. It enables features such as automated exception handling, Garbage-collected heap.
JVM Architecture
4. Stack : Local variables and partial results are store here. Each thread has a private JVM stack
created when the thread is created.
5. Program register : Program register holds the address of JVM instruction currently being
executed.
8. Native Method Interface : Native method interface gives an interface between java code and
native code during execution.
9. Native Method Libraries : Native Libraries consist of files required for the execution of native
code.
JDK vs JRE
JRE : The Java Runtime Environment (JRE) provides the libraries, the Java Virtual Machine, and other
components to run applets and applications written in the Java programming language. JRE does not
contain tools and utilities such as compilers or debuggers for developing applets and applications.
JDK : The JDK also called Java Development Kit is a superset of the JRE, and contains everything that is
in the JRE, plus tools such as the compilers and debuggers necessary for developing applets and
applications.
Writing Program in Java.
class Hello
3. static : static is again a keyword used to make a function static. To execute a static function you do
not have to create an Object of the class. The main() method here is called by JVM, without creating
any object for class.
4. void : It is the return type, meaning this function will not return anything.
5. main : main() method is the most important method in a Java program. This is the method which is
executed, hence all the logic must be inside the main() method. If a java class is not having a main()
method, it causes compilation error.
6. System.out.println : This is used to print anything on the console like printf in C language.
After compiling when you will try to run the byte code(.class file), the following steps are performed at
runtime:-
• Class loader loads the java class. It is subsystem of JVM Java Virtual machine.
• Byte Code verifier checks the code fragments for illegal codes that can violate access
right to the object.
• Interpreter reads the byte code stream and then executes the instructions, step by step.
Java is a truly object-oriented programming language, so any Java program will contain at least one class,
and any Java program may be viewed as a collection of classes. Programs may be classified as follows:
• Each source code file will contain one or more class definitions.
• At most one of the classes in a file can be "public", in which case the name of this class
must be the same as the name of the file containing it, and the file must have a .java
extension.
In general, each file will start with some comments, which include the name of the file then have one or
more import statements, which makes classes from various "packages" in the Java libraries available for
use in the program then have one or more class definitions, with the public one (if any) coming first.
Packages in Java
Packages are used in Java in order to prevent naming conflicts, to control access, to make
searching or locating and usage of classes, interfaces, enumerations and annotations easier, etc.
A Package can be defined as a grouping of related types (classes, interfaces, enumerations and
annotations ) providing access protection and name space management.
• java.io - classes for input , output functions are bundled in this package
Programmers can define their own packages to bundle group of classes/interfaces, etc. It is a
good practice to group related classes implemented by you so that a programmer can easily
determine that the classes, interfaces, enumerations, annotations are related. Since the package
creates a new namespace there won't be any name conflicts with names in other packages. Using
packages, it is easier to provide access control and it is also easier to locate the related classes.
Creating a package:
While creating a package, you should choose a name for the package and include a package
statement along with that name at the top of every source file that contains the classes, interfaces,
enumerations, and annotation types that you want to include in the package. The package
statement should be the first line in the source file. There can be only one package statement in
each source file, and it applies to all types in the file.
If a package statement is not used then the class, interfaces, enumerations, and annotation types
will be placed in the current default package. It is a good practice to use names of packages with
lower case letters to avoid any conflicts with the names of classes, interfaces. To compile the
Java programs with package statements you have to do use -d option as shown below.
Then a folder with the given package name is created in the specified destination, and the
compiled class files will be placed in that folder.