What is Java?
Java is a programming language and a platform. Java is a high level, robust,
object-oriented and secure programming language.
Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in
the year 1995. James Gosling is known as the father of Java. Before Java, its name
was Oak. Since Oak was already a registered company, so James Gosling and his
team changed the name from Oak to Java.
Platform: Any hardware or software environment in which a program runs, is known
as a platform. Since Java has a runtime environment (JRE) and API, it is called a
platform.
Java Example
Let's have a quick look at Java programming example. A detailed description of Hello
Java example is available in next page.
Simple.java
1. class Simple{
2. public static void main(String args[]){
3. System.out.println("Hello Java");
4. }
5. }
Application
According to Sun, 3 billion devices run Java. There are many devices where Java is
currently used. Some of them are as follows:
1. Desktop Applications such as acrobat reader, media player, antivirus, etc.
2. Web Applications such as irctc.co.in, javatpoint.com, etc.
3. Enterprise Applications such as banking applications.
4. Mobile
5. Embedded System
6. Smart Card
7. Robotics
8. Games, etc.
Types of Java Applications
There are mainly 4 types of applications that can be created using Java
programming:
1) Standalone Application
Standalone applications are also known as desktop applications or window-based
applications. These are traditional software that we need to install on every machine.
Examples of standalone application are Media player, antivirus, etc. AWT and Swing
are used in Java for creating standalone applications.
2) Web Application
An application that runs on the server side and creates a dynamic page is called a
web application. Currently, Servlet, JSP, Struts, Spring, Hibernate, JSF, etc.
technologies are used for creating web applications in Java.
3) Enterprise Application
An application that is distributed in nature, such as banking applications, etc. is called
an enterprise application. It has advantages like high-level security, load balancing,
and clustering. In Java, EJB is used for creating enterprise applications.
4) Mobile Application
An application which is created for mobile devices is called a mobile application.
Currently, Android and Java ME are used for creating mobile applications.
Java Platforms / Editions
There are 4 platforms or editions of Java:
1) Java SE (Java Standard Edition)
It is a Java programming platform. It includes Java programming APIs such as
java.lang, java.io, java.net, java.util, java.sql, java.math etc. It includes core topics like
OOPs, String, Regex, Exception, Inner classes, Multithreading, I/O Stream,
Networking, AWT, Swing, Reflection, Collection, etc.
2.) Java EE (Java Enterprise Edition)
It is an enterprise platform that is mainly used to develop web and enterprise
applications. It is built on top of the Java SE platform. It includes topics like Servlet,
JSP, Web Services, EJB, JPA, etc.
3) Java ME (Java Micro Edition)
It is a micro platform that is dedicated to mobile applications.
4) JavaFX
It is used to develop rich internet applications. It uses a lightweight user interface
API.
Prerequisite
To learn Java, you must have the basic knowledge of C/C++ programming language.
Audience
Our Java programming tutorial is designed to help beginners and professionals.
Features of Java
The primary objective of Java programming language creation was to make it
portable, simple and secure programming language. Apart from this, there are also
some excellent features which play an important role in the popularity of this
language. The features of Java are also known as Java buzzwords.
A list of the most important features of the Java language is given below.
1. Simple
2. Object-Oriented
3. Portable
4. Platform independent
5. Secured
6. Robust
7. Architecture neutral
8. Interpreted
9. High Performance
10. Multithreaded
11. Distributed
12. Dynami
Object-oriented
Java is an object-oriented programming language. Everything in Java is an object.
Object-oriented means we organize our software as a combination of different types
of objects that incorporate both data and behavior.
Object-oriented programming (OOPs) is a methodology that simplifies software
development and maintenance by providing some rules.
Basic concepts of OOPs are:
1. Object
2. Class
3. Inheritance
4. Polymorphism
5. Abstraction
6. Encapsulation
C++ vs Java
There are many differences and similarities between the C++ programming language
and Java. A list of top differences between C++ and Java are given below:
C++ Java
Comparison
Index
Platform- C++ is platform-dependent. Java is platform-independent.
independent
Mainly used C++ is mainly used for Java is mainly used for
for system programming. application programming. It is
widely used in Windows-based,
web-based, enterprise, and
mobile applications.
Design Goal C++ was designed for Java was designed and created
systems and applications as an interpreter for printing
programming. It was an systems but later extended as a
extension of the C support network computing. It
programming language. was designed to be easy to use
and accessible to a broader
audience.
Goto C++ supports Java doesn't support the goto
the goto statement. statement.
Multiple C++ supports multiple Java doesn't support multiple
inheritance inheritance. inheritance through class. It can
be achieved by using interfaces
in java.
Operator C++ supports operator Java doesn't support operator
Overloading overloading. overloading.
Pointers C++ supports pointers. You Java supports pointer internally.
can write a pointer program However, you can't write the
in C++. pointer program in java. It means
java has restricted pointer
support in java.
Compiler and C++ uses compiler only. C+ Java uses both compiler and
Interpreter + is compiled and run using interpreter. Java source code is
the compiler which converts converted into bytecode at
source code into machine compilation time. The interpreter
code so, C++ is platform executes this bytecode at
dependent. runtime and produces output.
Java is interpreted that is why it
is platform-independent.
Call by Value C++ supports both call by Java supports call by value only.
and Call by value and call by reference. There is no call by reference in
reference java.
Structure and C++ supports structures and Java doesn't support structures
Union unions. and unions.
Thread C++ doesn't have built-in Java has built-in thread support.
Support support for threads. It relies
on third-party libraries for
thread support.
Polymorphism
If one task is performed in different ways, it is known as polymorphism. For example:
to convince the customer differently, to draw something, for example, shape,
triangle, rectangle, etc.
In Java, we use method overloading and method overriding to achieve
polymorphism.
Another example can be to speak something; for example, a cat speaks meow, dog
barks woof, etc.
Abstraction
Hiding internal details and showing functionality is known as abstraction. For example
phone call, we don't know the internal processing.
In Java, we use abstract class and interface to achieve abstraction.
Encapsulation
Binding (or wrapping) code and data together into a single unit are known as
encapsulation. For example, a capsule, it is wrapped with different medicines.
A java class is the example of encapsulation. Java bean is the fully encapsulated class
because all the data members are private here.
JAVA PROGRAMS:
1.
1. class FibonacciExample1{
2. public static void main(String args[])
3. {
4. int n1=0,n2=1,n3,i,count=10;
5. System.out.print(n1+" "+n2);//printing 0 and 1
6.
7. for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed
8. {
9. n3=n1+n2;
10. System.out.print(" "+n3);
11. n1=n2;
12. n2=n3;
13. }
14.
15. }}
String and String Buffering
String Building
String :
String Buffering:
String Building :
Java Naming Conventions
Starts with Example
Java Thread class
Java provides Thread class to achieve thread programming. Thread class
provides constructors and methods to create and perform operations on a thread.
Thread class extends Object class and implements Runnable interface.
Java Thread Methods
S.N Modifier and Type Method Descriptio
. n
1) void start() It is used to
start the
execution of
the thread.
2) void run() It is used to
do an action
for a thread.
3) static void sleep() It sleeps a
thread for
the specified
amount of
time.
4) static Thread currentThread() It returns a
reference to
the currently
executing
thread
object.
5) void join() It waits for a
thread to
die.
6) int getPriority() It returns the
priority of
the thread.
7) void setPriority() It changes
the priority
of the
thread.
8) String getName() It returns the
name of the
thread.
9) void setName() It changes
the name of
the thread.
10) long getId() It returns the
id of the
thread.
11) boolean isAlive() It tests if the
thread is
alive.
12) static void yield() It causes the
currently
executing
thread
object to
pause and
allow other
threads to
execute
temporarily.
13) void suspend() It is used to
suspend the
thread.
14) void resume() It is used to
resume the
suspended
thread.
15) void stop() It is used to
stop the
thread.
16) void destroy() It is used to
destroy the
thread
group and
all of its
subgroups.
17) boolean isDaemon() It tests if the
thread is a
daemon
thread.
18) void setDaemon() It marks the
thread as
daemon or
user thread.
19) void interrupt() It interrupts
the thread.
20) boolean isinterrupted() It tests
whether the
thread has
been
interrupted.
21) static boolean interrupted() It tests
whether the
current
thread has
been
interrupted.
22) static int activeCount() It returns the
number of
active
threads in
the current
thread's
thread
group.
23) void checkAccess() It
determines if
the currently
running
thread has
permission
to modify
the thread.
24) static boolean holdLock() It returns
true if and
only if the
current
thread holds
the monitor
lock on the
specified
object.
25) static void dumpStack() It is used to
print a stack
trace of the
current
thread to the
standard
error stream.
26) StackTraceElement[] getStackTrace() It returns an
array of
stack trace
elements
representing
the stack
dump of the
thread.
27) static int enumerate() It is used to
copy every
active
thread's
thread
group and
its subgroup
into the
specified
array.
28) Thread.State getState() It is used to
return the
state of the
thread.
29) ThreadGroup getThreadGroup() It is used to
return the
thread
group to
which this
thread
belongs
30) String toString() It is used to
return a
string
representati
on of this
thread,
including the
thread's
name,
priority, and
thread
group.
31) void notify() It is used to
give the
notification
for only one
thread which
is waiting for
a particular
object.
32) void notifyAll() It is used to
give the
notification
to all waiting
threads of a
particular
object.
33) void setContextClassLoader() It sets the
context
ClassLoader
for the
Thread.
34) ClassLoader getContextClassLoader() It returns the
context
ClassLoader
for the
thread.
35) static getDefaultUncaughtExceptionHand It returns the
Thread.UncaughtExceptionHan ler() default
dler handler
invoked
when a
thread
abruptly
terminates
due to an
uncaught
exception.
36) static void setDefaultUncaughtExceptionHandl It sets the
er() default
handler
invoked
when a
thread
abruptly
terminates
due to an
uncaught
exception.