0% found this document useful (0 votes)
2 views

Introduction To Java

Java is a widely used programming language developed by Sun Microsystems in 1991, known for its portability and security features. It has evolved through multiple versions, introducing significant features like generics, lambda expressions, and modularization, making it suitable for various applications including web and mobile development. The Java Development Kit (JDK) and Java Runtime Environment (JRE) are essential for developing and running Java applications, with the JVM providing the runtime environment for executing Java bytecode.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Introduction To Java

Java is a widely used programming language developed by Sun Microsystems in 1991, known for its portability and security features. It has evolved through multiple versions, introducing significant features like generics, lambda expressions, and modularization, making it suitable for various applications including web and mobile development. The Java Development Kit (JDK) and Java Runtime Environment (JRE) are essential for developing and running Java applications, with the JVM providing the runtime environment for executing Java bytecode.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

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

• Autoboxing and autounboxing

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

• Now String can be used to control Switch statement.

• Multi Catch Exception

• try-with-resource statement
• Binary Integer Literals

• Underscore in numeric literals, etc.

Java SE 8 was released on March 18, 2014. Some of the major new features introduced in JAVA 8 are,
• Lambda Expressions

• New Collection Package java.util.stream to provide Stream API.

• Enhanced Security

• Nashorn Javascript Engine included

• Parallel Array Sorting

• The JDBC-ODBC Bridge has been removed etc.

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:

▪ Modularization of the JDK under Project Jigsaw.


▪ Provided Money and Currency API.
▪ Tight integration with JavaFX.
▪ Java implementation of reactive streams.
▪ More Concurrency Updates.
▪ Provided Java Linker.
▪ Automatic scaling and sizing.
Java 10 is released on March 20, 2018. It contains various new features and enhancements into
many functional areas. Some of its key improvements include enhancements for garbage
collection and compilation as well as local variable types.

▪ Application Data-Class Sharing.


▪ Parallel Full GC for G1.
▪ Garbage Collector Interface.
▪ Consolidate the JDK Forest into a Single Repository.
▪ Local-Variable Type Inference.
▪ Remove the Native-Header Generator Tool.
▪ Thread-Local Handshakes.
▪ Time-Based Release Versioning.
The Java 11 comes as the second LTS release after Java 8, speaking enough for its importance.

• New String and File methods.

• Local-Variable Syntax for Lambda Parameters.

• Java Flight Recorder to gather diagnostics.

• Removed Java EE and COBRA Modules.

• Implicitly compile and run.

Java 12 released in March, 2019 has some addition of features.

• Switch expression with return values.

• Some JVM improvements.

• File mismatch() Method.


• Compact Number Formatting.
• Teeing Collectors in Stream API.
• Java Strings New Methods – indent(), transform(), describeConstable(), and
resolveConstantDesc().

Java 13 arrives with improvements to garbage collection, application class-data sharing, and text blocks.
Some of the important Java 13 features are:

▪ Text Blocks – JEP 355

▪ New Methods in String Class for Text Blocks


▪ Switch Expressions Enhancements – JEP 354
▪ Reimplement the Legacy Socket API – JEP 353
▪ Dynamic CDS Archive – JEP 350
▪ ZGC: Uncommit Unused Memory – JEP 351
▪ FileSystems.newFileSystem() Method
▪ Support for Unicode 12.1
▪ DOM and SAX Factories with Namespace Support
The new features in Java 14 include flight recorder event streaming, switch expressions, NVM support, and records.
◦ Switch Expressions.
◦ Pattern Matching for instanceof.
◦ Helpful NullPointerExceptions.
◦ Records.
◦ Text Blocks.
◦ Packaging Tool .
◦ NUMA-Aware Memory Allocation for G1.
◦ JFR Event Streaming.
◦ Non-Volatile Mapped Byte Buffers.
◦ ZGC on macOS.
◦ ZGC on Windows.
◦ Foreign-Memory Access API.

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.

Following are some other usage of Java :


• Developing Desktop Applications
• Web Applications like Linkedin.com, Snapdeal.com etc
• Mobile Operating System like Android
• Embedded Systems
• Robotics and games etc.

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.

Setting Classpath for Java


Java is freely available on Oracle's Website. Download the latest version of JDK (Java Development Kit)
on your machine. Do check whether your machine is 32 bit or 64 bit and download that particular Java
version. Install JDK on your machine. Once you have installed Java on your machine you would need to
set environment variable to point to correct installation directory.

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.

Setting up path for windows ( 2000/XP/vista/Window 7,8 )


Assuming that you have installed Java in C:\ Program files/ Java / JDK directory
Step 1: Right click on my computer and select properties.
Step 2: Go to the Advance System Settings tab.
Step 3: Click on Environment Variables button.
Step 4: Now alter the path variable so that it also contains the path to JDK installed directory.
For e.g:- Change C:\windows/ system 32. to C:\windows/system 32; C:\program files / Java/ JDK.

Setting up path for Linux , Unix , Solaris, free BSD


Assuming that you have installed Java in C:\program files\ java\ JDK directory, do the following:
Step 1: Environment variable path should be set to point where java binaries have been installed.
Refer to your shell if you have trouble doing this.
For Example: If you use bash as your shell, then you would add following line to the end
bash mc: export PATH=/ Path/to/java
What is JVM?

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

1. Class Loader : Class loader loads the Class for execution.

2. Method area : Stores pre-class structure as constant pool.

3. Heap : Heap is in which objects are allocated.

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.

6. Native method stack : It contains all native used in application.


7. Executive Engine : Execution engine controls the execute of instructions contained in the
methods of the classes.

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.

Let us look at a simple java program.

class Hello

public static void main(String[] args)

System.out.println ("Hello World program");

1. class : class kyword is used to declare classes in Java

2. public : It is an access specifier. Public means this function is visible to all.

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.

Steps to Compile and Run your first Java program


Step 1: Open a text editor and write the code as above.
Step 2: Save the file as Hello.java
Step 3: Open command prompt and go to the directory where you saved your first java program
assuming it is saved in C:\
Step 4: Type javac Hello.java and press Return to compile your code. This command will call the
Java Compiler asking it to compile the specified file. If there are no errors in the code the
command prompt will take you to the next line.
Step 5: Now type java Hello on command prompt to run your program.
Step 6: You will be able to see Hello world program printed on your command prompt.

Now let us see what happens at Runtime


After writing your Java program, when you will try to compile it, compiler will perform some
compilation operation on your program. Once it is compiled successfully byte code (.class file) is
generated by the compiler.
Figure: How java program’s are compiled and executed.

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:

• Console application programs with text-based user interfaces


• GUI-based standalone application programs
• GUI-based applets that can be downloaded over the Internet and run on a local machine
inside a web browser
• Server-based programs, called servlets, that can be invoked by a server to produce some
information that the server can then send to a client over the web.

A Java GUI (Graphical User Interface) may contain


• older "heavyweight" AWT components (AWT = Abstract Windowing Toolkit)

• newer "lightweight" Swing components


• both, though mixing them requires care and should generally be avoided if possible

Java Program Structure

• A Java program will consist of one or more source code files.

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

Some of the existing packages in Java are:


• java.lang - bundles the fundamental classes

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

javac -d Destination_folder file_name.java

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.

You might also like