Introduction to Java : Feature to Java, Java Virtual Machine, Differences between C++ and Java,
Part of Java, API Document, Starting a Java Program. Important Classes, Formatting the Output
Introduction to Java : Feature to Java, Java Virtual Machine, Differences between C++ and Java,
Part of Java, API Document, Starting a Java Program. Important Classes, Formatting the Output
1.
INTRODUCTION TO JAVA PROGRAMMING LANGUAGE
UNIT-1
[Feature to Java, Java Virtual Machine, Differences between C++ and Java, Part of Java, API Document,
Starting a Java Program. Important Classes, Formatting the Output]
FEATURE TO JAVA
1. Simple: Java is Easy to write and more readable and eye catching. Most of the concepts are
aimilar to C++, thus making Java learning simpler. It removed many confusing and/or
rarely-used features e.g., explicit pointers, operator overloading etc. Pointers and Operator
Overloading are not there in java but were an important part of C++. No need to remove
unreferenced objects because there is Automatic Garbage Collection in java.
2. Object-Oriented: Object-Oriented Programming Language (OOPs) is the methodology
which provide software development and maintenance by using object state, behavior , and
properties. Object Oriented Programming Language must have the following characteristics.
1. Encapsulation
2. Polymorphism
3. Inheritance
4. Abstraction
Java is pure OOP. Language. (while C++ is semi object oriented).
3. Secure: Java enable us to develop virus free, temper free system as it provides security
features like:
1. No explicit use of pointers
2. Every time when a user compiles the Java program, the Java compiler creates
a class file with Bytecode, which are tested by the JVM at the time of
program execution for viruses and other malicious files.
3. The concept of exception handling enables Java to capture a series of errors
that helps developers to get rid of risk of crashing the system.
4. Garbage Collection, etc.
4. Robust: Different security features of Java make this language robust. Java encourages
error-free programming by being strictly typed and performing run-time checks.
5. 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 (WORA) 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.
Provided By Shipra Swati
2.
INTRODUCTION TO JAVA PROGRAMMING LANGUAGE
6. Architecture-neutral: Java is independent of hardware e.g. size of primitive types is fixed.
In C programming, int data type occupies 2 bytes of memory for 32-bit architecture and 4
bytes of memory for 64-bit architecture. But in java, it occupies 4 bytes of memory for both
32 and 64 bit architectures.
7. Portable: The portability actually comes from architecture-neutrality. Java programs can
execute in any environment for which there is a Java run-time system (JVM), irrespective of
OS and hardware.
8. Multi-Threading: A thread is like a separate program, executing concurrently. 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.
9. High Performance: Java enables the creation of cross-platform programs with the help of
Byte code (Intermediate code). Most previos attempts for implementing cross-plaform
solutions compromise the performance parameter. But, Java bytecode was carefully
designed for easy and direct translation into native machine code by using Just-in-time
compiler for very high perfomance.
10. Distributed: Java is designed for the distributed environment of the internet because it
handles TCP/IP protocols. It also enables support for Remote Method Invocation (RMI),
which helps to invoke methods across a network.
JAVA VIRTUAL MACHINE
The overall development and execution infrastructure for a Java program is shown in following
figure. A program written in the Java programming language is compiled into a set of bytecodes.
Those bytecodes are then loaded and executed by a JVM. If the program makes calls to other Java
classes, the bytecodes for those classes will likewise be loaded, dynamically linked, and executed.
The Java Development Kit (JDK) provides software development environment for developing
Java applications. It includes JRE, java compiler and other required development tools.
Provided By Shipra Swati
3.
INTRODUCTION TO JAVA PROGRAMMING LANGUAGE
JRE stands for “Java Runtime Environment” and may also be written as “Java RTE.” The Java
Runtime Environment provides the minimum requirements for executing a Java application; it
consists of the Java Virtual Machine (JVM), java interpreter and required libraries to run java
application.
JVM is short for Java Virtual Machine. JVM is an abstract computing machine, or virtual machine
or a machine within a machine – which acts like a real Java processor and provides a a
recommended environment to execute java bytecode. It needs the Java code library to run
applications. JVM only works with bytecode. Hence we need to compile our Java application
(.java) so that it can be converted to bytecode format (.class file).
JVM enabling Java bytecode to be executed as actions or operating system calls on any processor
regardless of the operating system. For example, establishing a socket connection from a
workstation to a remote machine involves an operating system call. Since different operating
systems handle sockets in different ways, the JVM translates the programming code so that the two
machines that may be on different platforms are able to connect.
JVM, JRE and JDK are platform dependent because configuration of each OS differs. But,
Java is platform independent.
Internal Architecture of JVM
Let's understand the internal architecture of JVM. It contains classloader, memory area, execution
engine etc. Diagrammatic representation is shown in next page.
1. Classloader: is a subsystem of JVM that is used to load class files.It loads, links. And
initializes the class file when it refers to a class for the first time at runtime, not compile
time.
2. JVM Memory Area: It is divided into 5 major components:
1. Method/Class Area – All the class level data will be stored here, including static
variables. There is only one method area per JVM, and it is a shared resource.
2. Heap: All the Objects and their corresponding instance variables and arrays will
be stored here. There is also one Heap Area per JVM. Since the Method and Heap
areas share memory for multiple threads, the data stored is not thread safe.
3. Stack: For every thread, a separate runtime stack will be created. For every
method call, one entry will be made in the stack memory which is called as Stack
Frame. All local variables will be created in the stack memory. The stack area is
thread safe since it is not a shared resource.
4. PC Registers: Each thread will have separate PC Registers, to hold the address of
current executing instruction. Once the instruction is executed the PC register will
be updated with the next instruction.
5. Native Method stacks: Native Method Stack holds native method information used
in the application. For every thread, a separate native method stack will be created.
Provided By Shipra Swati
4.
INTRODUCTION TO JAVA PROGRAMMING LANGUAGE
3. Execution Engine: The bytecode which is assigned to the JVM Memory Area will be
executed by the Execution Engine. The Execution Engine reads the bytecode and executes
it piece by piece. It contains:
1. Interpreter– The interpreter interprets the bytecode faster, but executes slowly. The
disadvantage of the interpreter is that when one method is called multiple times,
every time a new interpretation is required.
2. JIT Compiler– The JIT Compiler neutralizes the disadvantage of the interpreter by
improving the performance. JIT compiles parts of the byte code that have similar
functionality at the same time, and hence reduces the amount of time needed for
compilation.Here the term “compiler” refers to a translator from the instruction set
of a Java virtual machine (JVM) to the instruction set of a specific CPU.
3. Garbage Collector– It Collects and removes unreferenced objects. Garbage
Collection can be triggered by calling "System.gc()", but the execution is not
guaranteed.
4. Java Native Method Interface: It will be interacting with the Native Method Libraries.
5. Native Method Libraries: It is a collection of the Native Libraries which is required for
the Execution Engine.
Provided By Shipra Swati
5.
INTRODUCTION TO JAVA PROGRAMMING LANGUAGE
DIFFERENCES BETWEEN C++ AND JAVA
C++ JAVA
1.
C++ was developed by Bjarne Stroustrup.
Development began in 1979.
Java was developed by James Gosling
and his team. Development began in
1991.
2. C++ is a compiled language. Java is both compiled and interpreted.
3.
C++ programs are platform dependent. They need
to be compiled for a particular platform.
Java programs are platform independent.
Java programs are written for Java
Virtual Machine (JVM) and wherever a
JVM is installed, Java program will run
without needing recompilation.
4.
C++ does support operator overloading. Function
overloading is also available.
Java does not support operator
overloading. However, function
overloading is possible.
5. C++ fully supports pointers.
Java supports pointer internally. But you
can't write the pointer program in java.
6. C++ supports structures and unions.
Java does not support structures and
unions.
7. C++ does not have built-in support for threads. Java fully supports threads.
8.
C++ supports manual object management through
new and deletekeywords.
Java relies on automatic garbage
collection. It does not support destructors
the way C++ does.
9.
C++ supports goto statement (however the use of
goto is discouraged as not considered a good
practice)
Java does not support goto statement
(although goto is a reserved keyword in
Java)
10. C++ supports multiple inheritance.
Java does not really support multiple
inheritance. But similar results can be
achieved through the use of interfaces.
11.
C++ provides support both for call by value and call
by reference.
Java supports only call by value.
12.
C++ provides virtual keyword to support function
overriding.
Java does not support virtual keyword.
All the non-static Java functions are by
default virtual in nature, and therefore,
can be overridden.
13. C++ is mainly used for system programming.
Java is mainly used for application
programming. It is widely used in
window, web-based, enterprise and
mobile applications.
Provided By Shipra Swati
6.
INTRODUCTION TO JAVA PROGRAMMING LANGUAGE
API Document
An application programming interface (API), in the context of Java, is a collection of prewritten
packages, classes, and interfaces with their respective methods, fields and constructors. In Java,
most basic programming tasks are performed by the API’s classes and packages, which are helpful
in minimizing the number of lines written within pieces of code.
The API is a library of available Java classes, packages and interfaces. The three API types are as
follows:
• Official Java core API, which is bundled with the JDK download
• Optional official Java APIs, which may be downloaded if needed
• Unofficial APIs, which are third-party APIs that may be downloaded from source websites
The official API includes packages, e.g., applet packages, graphics and GUI swing packages,
input/output (IO) packages and Abstract Windows Toolkit (AWT), among others.
Important Classes
• The System (java.lang.System) class contains several useful class fields and methods for
standard input, standard output, and error output streams. It cannot be instantiated.
• The java.lang.String class provides a lot of methods to work on string. By the help of these
methods, we can perform operations on string such as trimming, concatenating, converting,
comparing, replacing strings etc.
• Scanner is a class in java.util package used for obtaining the input of the primitive types
like int, double etc. and strings. It is the easiest way to read input in a Java program.
• The Object class (java.lang.Object) is the parent class of all the classes in java by default. In
other words, it is the topmost class of java.
Provided By Shipra Swati
Parece que tem um bloqueador de anúncios ativo. Ao listar o SlideShare no seu bloqueador de anúncios, está a apoiar a nossa comunidade de criadores de conteúdo.
Odeia anúncios?
Atualizámos a nossa política de privacidade.
Atualizámos a nossa política de privacidade de modo a estarmos em conformidade com os regulamentos de privacidade em constante mutação a nível mundial e para lhe fornecer uma visão sobre as formas limitadas de utilização dos seus dados.
Pode ler os detalhes abaixo. Ao aceitar, está a concordar com a política de privacidade atualizada.