Basic Features

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Java Programming Language ( Features of Java )

Java History
Year Progress
1990 Sun decided to developed software that could be used for
electronic devices. And the project called as Green Project head
by James Gosling.
1991 Announcement of a new language named ―Oak‖
1992 The team verified the application of their new language to
manage a list of home appliances using a hand held device.
1993 The World Wide Web appeared on the Internet and
transformed the text-based interface to a graphical rich
environment.
1994 The team developed a new Web browsed called ―Hot Java‖ to
locate and run Applets.
1995 Oak was renamed to Java, as it did not survive ―legal‖
registration. Many companies such as Netscape and Microsoft
announced their support for Java.
1996 Java language is now famous for Internet programming as well
as a general purpose OO language.
1997 Sun releases Java Development Kit(JDK 1.1)
1998 Sun releases Software Development Kit (SDK 1.2)
1999 Sun releases Java 2 platform Standard Edition (J2SE) and
Enterprise Edition(J2EE).
2000 J2SE with SDK 1.3 was released.
2002 J2SE with SDK 1.4 was released.
2004 J2SE with JDK 5.0 was released.

Vivekanand College for BCA 1|Page


Java Programming Language ( Features of Java )

Comparison in Java and C++


Java C++
Java is true Object-oriented language. C++ is basically C with Object-oriented
extension.
Java does not support operator C++ supports operator overloading.
overloading.
It supports labels with loops and It supports goto statement.
statement blocks
Java does not have template classes as C++ has template classes.
in C++.
Java compiled into byte code for the Source code can be written to be platform
Java Virtual Machine. The source code independent and written to take advantage
is independent on operating system. of platform.C++ typically compiled into
machine code.
Java does not support multiple C++ supports multiple inheritance of
inheritance of classes but it supports classes.
interface.
Runs in a protected virtual machine. Exposes low-level system facilities.
Java does not support global variable. C++ support global variable.
Every variable should declare in class.
Java does not use pointer. C++ uses pointer.
It Strictly enforces an object oriented It Allows both procedural programming
programming paradigm. and object-oriented programming.
There are no header files in Java. We have to use header file in C++.

Features of java:
1. Compiled and Interpreted
• Basically a computer language is either compiled or interpreted. Java
comes together both these approach thus making Java a two-stage system.
• Java compiler translates Java code to Bytecode instructions and Java
Interpreter generate machine code that can be directly executed by
machine that is running the Java program.

2. Platform Independent

• Java is Platform independent and supports the feature portability.


• Java programs can be easily moved from one computer system to another
and anywhere.

Vivekanand College for BCA 2|Page


Java Programming Language ( Features of Java )

• Changes and upgrades in operating systems, processors and system


resources will not force any alteration in Java programs.
Java certifies portability in two ways.
First way:=
• Java compiler generates the bytecode.the that can be executed on any
machine.
• the Java interpreter(Java Virtual Machine) reads the byte code, translates it
into the corresponding host machine instructions and then executes the
machine instruction.
• A Java program can run on any computer system for which a JVM (Java
Virtual Machine) and some library routines have been installed.
Second way :=
• size of primitive data types are machine independent.
• Integers are always four bytes long and floating-point variables follow the
IEEE 754.You don’t need to worry that the interpretation of your integer is
going to change if you move from one hardware to another hardware like
Pentium to a PowerPC.
• the execution of that program is possible on any other computer system
loaded with JVM.
• For example, you can write and compile the Java program on Windows 98
and execute the compiled program on JVM of the Macintosh operating
system.
The same concept is explained in Figure 1given below.

3. Object Oriented
• Java is truly object-oriented language.
• Objects have two sections.
• The first is Data (instance variables) and the second is methods.

Vivekanand College for BCA 3|Page


Java Programming Language ( Features of Java )

o Data represents what an object is. A method represents what an object


does.
• In Java, almost everything is an Object. The object model in Java is trouble-
free,easy to enlarge, reuse of programming segments and robust.

4. Easy to Learn
• Java is very small and simple language. Java does not use pointer and
header files, goto statements,etc.
• It eliminates operator overloading , multiple inheritance and pointers.
• in Java is automatic memory allocation and de-allocation.

5. Robust and secure


• Java is a most strong language which provides many securities to make
certain reliable code.
• many times pointers are the main cause of runtime errors because of
improper use of memory. Java eliminates pointer manipulation completely
from the language, and therefore eliminates a large source of runtime
errors. Java programmers need not remember to de-allocate memory in
programs since there is a garbage collection mechanism which handles
de-allocation of memory. It provides powerful a robust exception handling
mechanism to deal with both expected and unexpected errors at run time.
• Java also includes the concept of exception handling, which control serious
errors and reduces all kind of threat of crashing the system.
• Security is an important feature of Java and this is the strong reason that
programmer use this language for programming on Internet.

6. Multithreaded and Interactive


• The execution of two or more sections of a program at the same time and
this technique is known as multithreading.
• Multithreaded means managing multiple tasks simultaneously. Java
maintains multithreaded programs. That means we need not wait for the
application to complete one task before starting next task. This feature is
helpful for graphic applications.

7. Dynamic and Extensible


• The Java compiler is smart and dynamic.
• If you are compiling a file that depends on other non-compiled files, then
the compiler will try to find and compile them also.

Vivekanand College for BCA 4|Page


Java Programming Language ( Features of Java )

• The compiler can handle methods that are used before they’re declared. It
can also determine whether a source code has been changed since the last
time it was compiled.

8. High Performance
• Java is a fast-interpreted language.
• Java has designed for the run-time system can optimize their performance
by compiling bytecode to native machine code on the fly (execute
immediately after compilation). This is called “just in time” (JIT) compilation.

Java Virtual machine:

• As we know that all programming language compilers convert the source


code to machine code.
• In java, Java compiler convert the source code into Intermediate code is
called as bytecode.
• This machine is called the Java Virtual machine and it exits only inside the
computer memory.
the process of compilation

Java Program Java compiler Byte code


(source code)

• The byte code is not machine specific.


• The machine code is generated By Java interpreter by acting as an
intermediary between the two different machines shown below
the process of running program

Byte code Java interpreter Machine code

Java Environment:

• Java environment includes a number of development tools, classes and


methods.

Vivekanand College for BCA 5|Page


Java Programming Language ( Features of Java )

• The development tools are part of the system known as Java Development
Kit (JDK) and the classes and methods are part of the Java Standard Library
(JSL), also known as the Application Programming Interface (API).
• Java Development kit (JDK) – The JDK comes with a set of tools that are used
for developing and running Java program. It includes:
1. Appletviewer( It is used for viewing the applet)
2. Javac(It is a Java Compiler)
3. Java(It is a java interpreter)
4. Javap(Java diassembler,which convert byte code into program
description)
5. Javah(It is for java C header files)
6. Javadoc(It is for creating HTML document)
7. Jdb(It is Java debugger)
For compiling and running the program we have to use following commands:
a) javac (Java compiler)

In java, we can use any text editor for writing program and then save that
program with ―.java extension.
Java compiler convert the source code or program in bytecode and interpreter
convert ―.java file in ―.class file.
Syntax: C:\javac filename.java
If my filename is ―abc.java‖ then the syntax will be
Eg. C:\javac abc.java
b) java(Java Interpreter)

Java interpreter convert bytecode into machine code and ―.java file in ―.class file.
Syntax: C:\java filename
If my filename is abc.java then the syntax will be
Eg. C:\java abc
A Simple Java Program

Java offers two flavors of programming, Java applets and Java application

Java application Program

Let us write your first program with the file name “Application”. This program,
after successful compilation and run, prints the words “This is My First Java
Program” on your display.

Vivekanand College for BCA 6|Page


Java Programming Language ( Features of Java )

1. First of all using any text editor, type Java source file of your program as
given below.
2. Now compile the source file-using compiler named Javac that takes your
source file and translates its statements into a bytecode file.
3. Run the program using interpreter named Java that takes your bytecode file
and translates
them into machine code that your computer can understand.

/* This is my First Java Application Save the file as Application.Java: same as class
name */
class Application
{
public static void main (String args[ ] )
{
System.out.println(“This is My First Java Application”);
} // main ends here
} // Code ends here

Let us see the above program line by line.

Line 1. /* This is my First Java Program. Save this file as Application.Java


same as class name */

Comments in Java are similar to C++. Everything between /* and */ is ignored by


the compiler but Comments allow you to describe the details of the program. This
is useful for developing the understandability in your program.

Line 2. class Application

Second line in the program defines a class named Application using a keyword
class.

Line 4. public static void main (String args[ ])

This is the point from where the program will start the execution. This program
starts the execution by calling main () method. In this line public, static, and void
all are keywords. May be you are thinking of the meaning of ‘keyword’. Keywords
are nothing but some reserved words.

Vivekanand College for BCA 7|Page


Java Programming Language ( Features of Java )

• The public keyword is used to control the access of various class members.
If member is public it can be accessed outside the class.
• So we have to declare main () as public because it has to be invoked by the
code outside the class when program is executed.
• Static key word allows the main () method to be executed without creating
an object of that class,
• and void means main () method does not return any value.
• Parentheses ( )
It encloses arguments in method definitions or calling and used to
define test expressions control statements.
• Braces { }
It defines blocks of code
• Square bracket [ ]
It declares array types.

• Semicolons;
are used to terminate statements.

• Single dot “.”


It Selects a method from an object and separates package names from
subpackage
and class names.

Line 6. System.out.println(“This is My First Java Application.”);

This line prints the string “This is My First Java Application”.


• println ()
This function is used to display this line. The println () function accepts
any string and
display its value on console.

Compiling the Program

To compile the Example program, execute the compiler, javac, specifying the
name of the source file on the command line, as shown here:

C:\>javac Application.java

Vivekanand College for BCA 8|Page


Java Programming Language ( Features of Java )

The javac compiler creates a file called Example.class that contains the bytecode
version of the program.
The Java bytecode is the intermediate of program.the Java interpreter will
execute.
Thus, the output of javac is not code that can be directly executed.
To actually run the program, you must use the Java interpreter, called java. To do
so, pass the class name Example as a command-line argument, as shown here:

C:\>java Application

When the program is run, the following output is displayed:

This is My First Java Application.

Java Runtime Environment (JRE) and Java Architecture in Detail


Java Runtime Environment contains JVM, class libraries and other supporting
components.

As you know the Java source code is compiled into bytecode by Java compiler. This
bytecode will be stored in class files. During runtime, this bytecode will be loaded,
verified and JVM interprets the bytecode into machine code which will be
executed in the machine in which the Java program runs.

A Java Runtime Environment performs the following main tasks respectively.

1. Loads the class

This is done by the class loader

2. Verifies the byte code

This is done by byte code verifier.

3. Interprets the byte code

This is done by the JVM

These tasks are described in detail in the subsequent sessions.A detailed Java
architecture can be drawn as given below.

Vivekanand College for BCA 9|Page


Java Programming Language ( Features of Java )

Figure 3: Java Architecture in Detail

4.1. Class loader


Class loader loads all the class files required to execute the program. Class loader
makes the program secure by separating the namespace for the classes obtained
through the network from the classes available locally. Once the bytecode is loaded
successfully, then next step is bytecode verification by bytecode verifier.

4.2. Byte code verifier


The bytecode verifier verifies the byte code to see if any security problems are
there in the code. It checks the byte code and ensures the followings.

1. The code follows JVM specifications.

2. There is no unauthorized access to memory.

3. The code does not cause any stack overflows.

4. There are no illegal data conversions in the code such as float to object
references.

Once this code is verified and proven that there is no security issues with the code,
JVM will convert the byte code into machine code which will be directly executed
by the machine in which the Java program runs.

Vivekanand College for BCA 10 | P a g e


Java Programming Language ( Features of Java )

4.3. Just in Time Compiler


when the Java program is executed, the bytecode is interpreted by JVM. But this
interpretation is a slower process. To overcome this difficulty, JRE include the
component JIT compiler. JIT makes the execution faster.

If the JIT Compiler library exists, when a particular bytecode is executed first time,
JIT complier compiles it into native machine code which can be directly executed
by the machine in which the Java program runs. Once the byte code is recompiled
by JIT compiler, the execution time needed will be much lesser. This compilation
happens when the byte code is about to be executed and hence the name “Just in
Time”.

Once the bytecode is compiled into that particular machine code, it is cached by
the JIT compiler and will be reused for the future needs. Hence the main
performance improvement by using JIT compiler can be seen when the same code
is executed again and again because JIT make use of the machine code which is
cached and stored.

Garbage Collection
Garbage collection is a process by which Java achieves better memory
management. As you know, in object oriented programming, objects communicate
to each other by passing messages. (If you are not clear about the concepts of
objects, please read the prior chapter before continuing in this session).

Whenever an object is created, there will be some memory allocated for this object.
This memory will remain as allocated until there are some references to this object.
When there is no reference to this object, Java will assume that this object is not
used anymore. When garbage collection process happens, these objects will be
destroyed and memory will be reclaimed.

Garbage collection happens automatically. There is no way that you can force
garbage collection to happen. There are two methods “System.gc()” and
“Runtime.gc()” through which you can make request for garbage collation. But
calling these methods also will not force garbage collection to happen and you
cannot make sure when this garbage collection will happen.

Difference between JDK, JRE, and JVM


We must understand the differences between JDK, JRE, and JVM before proceeding
further to Java. See the brief overview of JVM here.

Vivekanand College for BCA 11 | P a g e


Java Programming Language ( Features of Java )

If you want to get the detailed knowledge of Java Virtual Machine, move to the next
page. Firstly, let's see the differences between the JDK, JRE, and JVM.

JVM

JVM (Java Virtual Machine) is an abstract machine. It is called a virtual machine


because it doesn't physically exist. It is a specification that provides a runtime
environment in which Java bytecode can be executed. It can also run those
programs which are written in other languages and compiled to Java bytecode.

JVMs are available for many hardware and software platforms. JVM, JRE, and JDK
are platform dependent because the configuration of each OS is different from each
other. However, Java is platform independent. There are three notions of the
JVM: specification, implementation, and instance.

The JVM performs the following main tasks:

o Loads code
o Verifies code
o Executes code
o Provides runtime environment

JRE

JRE is an acronym for Java Runtime Environment. It is also written as Java RTE.
The Java Runtime Environment is a set of software tools which are used for
developing Java applications. It is used to provide the runtime environment. It is
the implementation of JVM. It physically exists. It contains a set of libraries + other
files that JVM uses at runtime.

Vivekanand College for BCA 12 | P a g e


Java Programming Language ( Features of Java )

JDK

JDK is an acronym for Java Development Kit. The Java Development Kit (JDK) is a
software development environment which is used to develop Java applications
and applets. It physically exists. It contains JRE + development tools.

JDK is an implementation of any one of the below given Java Platforms released by
Oracle Corporation:

o Standard Edition Java Platform


o Enterprise Edition Java Platform
o Micro Edition Java Platform

The JDK contains a private Java Virtual Machine (JVM) and a few other resources
such as an interpreter/loader (java), a compiler (javac), an archiver (jar), a
documentation generator (Javadoc), etc. to complete the development of a Java
Application.

Vivekanand College for BCA 13 | P a g e


Java Programming Language ( Features of Java )

Vivekanand College for BCA 14 | P a g e


Java Programming Language ( Features of Java )

PRIMITIVE DATA TYPE AND VARIABLES

Description

Name Width Range

long 64 –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807


int 32 –2,147,483,648 to 2,147,483,647
short 16 –32,768 to 32,767
byte 8 –128 to 127

double 64 4.9e–324 to 1.8e+308


float 32 1.4e−045 to 3.4e+038

char 1 store one character


String store String value ( sequence of Characters )

boolean store true or false

Vivekanand College for BCA 15 | P a g e


Java Programming Language ( Features of Java )

As you may know, in some other languages, including C/C++, strings are
implemented as arrays of characters. However, this is not the case in Java. Strings
are actually object types. As you will see later because Java implements strings as
objects, Java includes extensive string-handling capabilities that are both powerful
and easy to use.

Dynamic Initialization

Although the preceding examples have used only constants as initializers, Java
allows variables to be initialized dynamically, using any expression valid at the
time the variable is declared.

For example, here is a short program that computes the length of the hypotenuse
of a right triangle given the lengths of its two opposing sides:

// Demonstrate dynamic initialization.


class DynInit
{
public static void main(String args[])
{
double a = 3.0, b = 4.0;
// c is dynamically initialized
double c = Math.sqrt(a * a + b * b);
System.out.println("Hypotenuse is " + c);
}
}

Vivekanand College for BCA 16 | P a g e


Java Programming Language ( Features of Java )

The Scope and Lifetime of Variables

So far, all of the variables used have been declared at the start of the main( )
method. However, Java allows variables to be declared within any block. A block
defines a scope. Thus, each time you start a new block, you are creating a new
scope. As you probably know from your previous programming experience, a
scope determines what objects are visible to other parts of your program. It also
determines the lifetime of those objects. Most other computer languages define
two general categories of scopes: global and local. However, these traditional
scopes do not fit well with Java’s strict, object oriented model. While it is possible
to create what amounts to being a global scope, it is by far the exception, not the
rule. In Java, the two major scopes are those defined by a class and those defined
by a method. Even this distinction is somewhat artificial. However, since the class
scope has several unique properties and attributes that do not apply to the scope
defined by a method, this distinction makes some sense.

As a general rule, variables declared inside a scope are not visible (that is,
accessible) to code that is defined outside that scope. Thus, when you declare a
variable within a scope, you are localizing that variable and protecting it from
unauthorized access and/or modification. Indeed, the scope rules provide the
foundation for encapsulation. Scopes can be nested. For example, each time you
create a block of code, you are creating a new, nested scope. When this occurs, the
outer scope encloses the inner scope. This means that objects declared in the outer
scope will be visible to code within the inner scope. However, the reverse is not
true. Objects declared within the inner scope will not be visible outside it.

To understand the effect of nested scopes, consider the following program:

// Demonstrate block scope.


class Scope
{
public static void main(String args[])
{
int x; // known to all code within main
x = 10;
if(x == 10)
{ // start new scope
int y = 20; // known only to this block
// x and y both known here.

Vivekanand College for BCA 17 | P a g e


Java Programming Language ( Features of Java )

System.out.println("x and y: " + x + " " + y);


x = y * 2;
}
y = 100; // Error! y not known here
// x is still known here.
System.out.println("x is " + x);
}
}

C++ java
C

Difference between java and C

• It doesn’t include “sizeof” and “typedef”.


• It doesn’t contain “struct” and “union”.
• It doesn’t define type modifier keyword auto,extern,register,signed and
unsigned.
• It doesn’t support pointer type.
• It doesn’t have preprocessor #define , #include statement.
• It requires that the functions with no argument must be declared with empty
parenthesis and not with void keyword.
• Java adds new operators such as instanceof and >>>
• Java adds new labelled such as break and continue
• Java adds many features required for object oriented programming.

Difference between java and C++

• It doesn’t support operator overloading


• It doesn’t have template class in c++
• It doesn’t support multiple inheritance of classes.this is accomplished using
using “interface”

Vivekanand College for BCA 18 | P a g e


Java Programming Language ( Features of Java )

• It doesn’t support global variable.every variable and methods is declared


within a class.
• It doesn’t use pointer.
• It has replaced the destructor function with a finalize() method.
• There are no header files in java.

Vivekanand College for BCA 19 | P a g e

You might also like