2. INTRODUCTION
Required Knowledge
Basic Knowledge of C
Programming and C++ will help you to
understand Java Programming quickly, and If
you don't know programming and you are
studying Java, so it's quite complicated.
Java Example
A quick look at the example of Hello
Java program and the detailed description is
given in Java Program Structure page.
3. EXAMPLE
public class Hello
{
public static void main(String[] args)
{
System.out.println("Hello Java");
}
}
Program Output:
Hello Java
The above example has been used to print Hello
Java text on the screen.
4. Java is an object-oriented programming
language with its runtime environment. It is a
combination of features of C and C++ with
some essential additional concepts. Java is
well suited for both standalone and web
application development and is designed to
provide solutions to most of the problems
faced by users of the internet era.
5. WHAT IS JAVA
Java is an object-oriented programming language developed
by Sun Microsystems, and it was released in 1995.
James Gosling initially developed Java in Sun
Microsystems (which was later merged with Oracle Corporation).
Java is a set of features of C and C++. It has obtained its format
from C, and OOP features from C++.
Java programs are platform independent which means they can
be run on any operating system with any processor as long as
the Java interpreter is available on that system.
Java code that runs on one platform does not need to be
recompiled to run on another platform; it's called write once, run
anywhere(WORA).
Java Virtual Machine (JVM) executes Java code, but it has been
written in platform-specific languages such as C/C++/ASM, etc.
JVM is not written in Java and hence cannot be platform
independent, and Java interpreter is a part of JVM.
6. WHERE IS JAVA BEING USED
Earlier Java was only used to design and program small computing
devices, but it was later adopted as one of the platform-independent
programming languages, and now according to Sun, 3 billion devices
run Java.
Java is one of the most important programming languages in today's IT
industries.
JSP - In Java, JSP (Java Server Pages) is used to create dynamic web
pages, such as in PHP and ASP.
Applets - Applets are another type of Java programs that are
implemented on Internet browsers and are always run as part of a web
document.
J2EE - Java 2 Enterprise Edition is a platform-independent environment
that is a set of different protocols and APIs and is used by various
organizations to transfer data between each other.
JavaBeans - This is a set of reusable software components that can be
easily used to create new and advanced applications.
Mobile - In addition to the above technology, Java is widely used in
mobile devices nowadays, many types of games and applications are
being made in Java.
7. TYPES OF JAVA APPLICATION
Web Application - Java is used to create server-side web
applications. Currently, Servlet, JSP, Struts, JSF, etc.
technologies are used.
Standalone Application - It is also known as the desktop
application or window-based application. An application that we
need to install on every machine or server such as media player,
antivirus, etc. AWT and Swing are used in java for creating
standalone applications.
Enterprise Application - An application that is distributed in nature,
such as banking applications, etc. It has the advantage of the
high-level security, load balancing, and clustering. In Java, EJB is
used for creating enterprise applications.
Mobile Application - Java is used to create application software
for mobile devices. Currently, Java ME is used for building
applications for small devices, and also Java is a programming
language for Google Android application development.
8. FEATURES OF JAVA
Object-Oriented - Java supports the features of object-oriented programming. Its
object model is simple and easy to expand.
Platform independent - C and C++ are platform dependency languages hence
the application programs written in one Operating system cannot run in any
other Operating system, but in platform independence language like Java
application programs written in one Operating system can able to run on any
Operating system.
Simple - Java has included many features of C / C ++, which makes it easy to
understand.
Secure - Java provides a wide range of protection from viruses and malicious
programs. It ensures that there will be no damage and no security will be
broken.
Portable - Java provides us the concept of portability. Running the same
program with Java on different platforms is possible.
Robust - During the development of the program, it helps us to find possible
mistakes as soon as possible.
Multi-threaded - The multithreading programming feature in Java allows you to
write a program that performs several different tasks simultaneously.
Distributed - Java is designed for distributed Internet environments as it
manages the TCP/IP protocol.
Popular Java Editors
9. POPULAR JAVA EDITOR
Notepad - On Windows machine, you can
use any simple text editor like Notepad
(Recommended for this tutorial), TextPad.
Netbeans - is a Java IDE that is open source
and free which can be downloaded
from http://www.netbeans.org/index.html
Eclipse - is also a java IDE developed by the
Eclipse open source community and can be
downloaded from http://www.eclipse.org/
10. EVOLUTION OF JAVA
The development of each programming language is
based on a fact: there is a need to solve a problem that
was not resolved by previous programming languages.
Early programmers had to choose different programming
languages, usually for various tasks, such as a specific
language for a type of field. A certain language was
sufficient to solve the problems of its field but was not
able to solve the problems of other fields. For
example, Fortran could have been used to write efficient
programs for scientific problems, but it was not good for
system code. Similarly, Basic was easy to understand but
was not robust to write big programs; While the assembly
language was powerful for writing efficient programs, but
it was not easy to remember and execution.
11. Programming languages such as Cobol, Fortran do not have
structural principles. They use the Goto statement to control
the flow of the program. Therefore, programs using this type
of code are made up of many jumps and conditional
statements that make it difficult to understand.
Therefore, C was invented in 1970, to replace the assembly
language and to create a structured, effective and high-level
language. The development of C was the result of the
development process started with BCPL by Dennis
Ritchie. BCPL is an old language developed by Martin
Richard. Ken Thompson developed a language called B,
which was influenced by BCPL.
C is a processor-oriented programming language; it is easy to
execute and understand. C became quite famous at that time
because it was reliable, simple and easy to use.
12. Though C was quite efficient and successful programming language,
the complexity of the program was seeking more efficient language
to solve problems. When we write a program in C, it has a limit, such
as a maximum of 25000 lines of code, beyond which it can not
handle the complexity. But writing and managing large programs
was a demand at that time. So a new concept came.
C++ came with object-oriented programming features. C++ is the
extension of C language which has been used extensively. It is a
powerful modern language that includes the power and simplicity of
C and the characteristics of OOP. C++ provides more functional
software benefits than C.
C ++ with OOP became quite famous but then a new problem arose,
to control the software on different machines, a separate compiler is
required for that CPU. But building a C++ compiler was quite
expensive. Therefore, an efficient and easy solution was needed,
and this requirement became the reason for the creation of Java,
which is a portable and platform-independent language.
13. HISTORY OF JAVA TECHNOLOGY
History of Java programming language is usually
associated with origin predates of the web. James
Gosling, Patrick Naughton, Chris Warth, Mike
Sheridan and Ed Frank initiated the Java language
project in June 1991. The idea was to develop a
language which was platform-independent and which
could create embedded software for consumer
electronic devices. It took 18 months to develop and
had an initial name as Oak which was renamed
to Java in 1995, due to copyright issues.
14. JAVA VERSION RELEASE HISTORY
Version Released on
JDK1.023 Jan 1996
JDK1.119 Feb 1997
J2SE 1.28 Dec 1998
J2SE 1.38 May 2000
J2SE 1.46 Feb 2002
J2SE 5.030 Sept 2004
Java SE 611 Dec 2006
Java SE 7.028 July 2011
Java SE 8.018 March 2014
Java SE 9.0Sept 2017
Java SE 10 (18.3)2018
15. JAVA PROGRAM STRUCTURE
It is necessary to know the exact structure of
the Java program, and this lesson contains a
detailed description of it. This lesson is
essential for you before proceeding to learn
more advanced lessons of Java
programming. Here, in this chapter, you will
study the structure of the Java program.
Such as how to create a simple Java
program and what its different sections
mean.
16. BASIC STRUCTURE OF JAVA PROGRAM
A Java program involves the following
sections:
Documentation Section
Package Statement
Import Statements
Interface Statement
Class Definition
Main Method Class
Main Method Definition
17. Section Description
Documentation Section You can write a comment in this section.
Comments are beneficial for the
programmer because they help them
understand the code. These are optional,
but we suggest you use them because they
are useful to understand the operation of
the program, so you must write comments
within the program.
Package statement You can create a package with any name.
A package is a group of classes that are
defined by a name. That is, if you want to
declare many classes within one element,
then you can declare it within a package. It
is an optional part of the program, i.e., if
you do not want to declare any package,
then there will be no problem with it, and
you will not get any errors. Here, the
package is a keyword that tells the
compiler that package has been created.It
is declared as:
package package_name;
18. Import statements This line indicates that if you want to
use a class of another package, then
you can do this by importing it directly
into your program.
Example:import calc.add;
Interface statement Interfaces are like a class that includes a
group of method declarations. It's an
optional section and can be used when
programmers want to implement multiple
inheritances within a program.
Class Definition A Java program may contain several class
definitions. Classes are the main and
essential elements of any Java program.
Main Method Class Every Java stand-alone program requires
the main method as the starting point of the
program. This is an essential part of a Java
program. There may be many classes in a
Java program, and only one class defines
the main method. Methods contain data
type declaration and executable
statements.
19. SIMPLE JAVA PROGRAM
//Name of this file will be "Hello.java"
public class Hello
{
/* Writes the words "Hello Java" on the screen */
public static void main(String[] args)
{
System.out.println("Hello Java");
}
}
Program output:
Hello Java
20. public class Hello •This creates a class called Hello.
•All class names must start with a
capital letter.
•The public word means that it is
accessible from any other classes.
/* Comments */ The compiler ignores comment block.
Comment can be used anywhere in
the program to add info about program
or code block, which will be helpful for
developers to understand the existing
code in the future easily.
Braces Two curly brackets {...} are used to
group all the commands, so it is
known that the commands belong to
that class or method.
21. public static void main •When the main method is declared public, it
means that it can also be used by code
outside of its class, due to which the main
method is declared public.
•The word static used when we want to
access a method without creating its
object, as we call the main method, before
creating any class objects.
•The word void indicates that a method does
not return a value. main() is declared as
void because it does not return a value.
•main is a method; this is a starting point of
a Java program.
You will notice that the main method code
has been moved to some spaces left. It is
called indentation which used to make a
program easier to read and understand.
String[] args It is an array where each element of it is a
string, which has been named as "args". If your
Java program is run through the console, you
can pass input parameter, and main() method
takes it as input.
System.out.println(); This statement is used to print text on the
screen as output, where system is a predefined
class, and out is an object of the PrintWriter
class defined in the system. The
method println prints the text on the screen with
a new line. You can also use print()method
22. DIFFERENCE OF JAVA C CPP
C CPP JAVA
Language Procedural Object oriented Pure object Oriented
Header file Stdio.h Iostream.h
conio.h
Java does not
support any header
file
Platform Dependant Dependant Independent
Pointers Support Support Does not Support
Operator
overloading
Not used Used Not used
Multiple Inheritance Not used Used Instead of this we
use Interface
Translator Compiler Compiler Compiler and
interpreter
Web Application Not used Not used Internet
programming like
Frame,Applet
23. C CPP JAVA
Operator Dot operator Dot Operator Dot Operator
Package Not Create Not Create Create own
Package
Program #include<stdio.h>
//header file for
standard input and
output
main() //ma
in method
{
clrscr(); //clear
the screen
printf(“welcome”); //
print the message
getch(); //get the
character
}
#include<iostream.h
> //header file
necessary for input
output stream
#include<conio.h>
//headerfile for
console input output
main() //main
method
{
clrscr(); //clear the
screen
cout<<”welcome”; /
/cout is used for
output
getch(); //get the
character
}
class
main //program
always start with
class because of
pure object oriented
language
{
public static void
main(String
aa[]) //main method
{
System.out.print(“we
lcome”); //print the
message
}
}
24. JAVA SUPPORT SYSTEM & ENVIRONMENT
The Java Virtual Machine is called JVM, is an
abstract computing machine or virtual mac
hine interface that drives the java code.
When we talk about the Java applications,
then it works only on those machines which
have JVM.
25. WHAT IS JVM
JVM i.e., Java Virtual Machine.
JVM is the engine that drives the Java code.
Mostly in other Programming Languages, compiler
produce code for a particular system but Java compiler
produce Bytecode for a Java Virtual Machine.
When we compile a Java program, then bytecode is
generated. Bytecode is the source code that can be used
to run on any platform.
Bytecode is an intermediary language between Java
source and the host system.
It is the medium which compiles Java code to bytecode
which gets interpreted on a different machine and hence it
makes it Platform/Operating system independent.
26. JVM'S WORK CAN BE EXPLAINED IN THE
FOLLOWING MANNER.
Reading Bytecode.
Verifying bytecode.
Linking the code with the library.
Diagram of JVM
JVM generates a .class(Bytecode) file, and that file can be
run in any OS, but JVM should have in OS because JVM is
platform dependent.
27. PLATFORM INDEPENDENT
Java is called platform independent because of
Java Virtual Machine. As different computers with
the different operating system have their JVM,
when we submit a .class file to any operating
system, JVM interprets the bytecode into machine
level language.
JVM is the main component of Java architecture,
and it is the part of the JRE (Java Runtime
Environment).
A program of JVM is written in C Programming
Language, and JVM is Operating System
dependent.
JVM is responsible for allocating the necessary
memory needed by the Java program.
JVM is responsible for deallocating memory space.