You are on page 1of 11

Java (programming language)

Java
Paradigm(s)
multi-paradigm: object-oriented,
structured, imperative, functional,
generic, reflective, concurrent
Designed by
James Gosling and
Sun Microsystems
Developer Oracle Corporation
Appeared in 1!
Stable release
Java Standard "dition # $pdate !
%1&#&'(!) * +pril 1!, ,'1-. , mont/s ago
Typing discipline
Static, strong, safe, nominative,
manifest
Major
implementations
OpenJ01, many ot/ers
Dialects Generic Java, 2i33a
Influenced by
+da #4, C55, C6,
7,8
"iffel,
748
Generic
Java, Mesa,
7-8
Modula-4,
7!8
Oberon,
798

Objective-C,
7:8
$CS0 2ascal,
7#878

Smalltal;
Influenced
+da ,''!, <eanS/ell, C6, Clojure, 0,
"CM+Script, Groovy, J6, JavaScript,
1otlin, 2=2, 2yt/on, Scala, Seed:, >ala

Java is a computer programming language t/at is concurrent, class-based, object-


oriented, and specifically designed to /ave as fe? implementation dependencies as
possible& @t is intended to let application developers A?rite once, run any?/ereA %BOC+),
meaning t/at code t/at runs on one platform does not need to be recompiled to run on
anot/er& Java applications are typically compiled to bytecode %class file) t/at can run on
any Java virtual mac/ine %J>M) regardless of computer arc/itecture& Java is, as of ,'1-,
one of t/e most popular programming languages in use, particularly for client-server ?eb
applications, ?it/ a reported million developers&
71'87118
Java ?as originally developed by
James Gosling at Sun Microsystems %?/ic/ /as since merged into Oracle Corporation)
and released in 1! as a core component of Sun MicrosystemsD Java platform& E/e
language derives muc/ of its syntaF from C and C55, but it /as fe?er lo?-level facilities
t/an eit/er of t/em&
E/e original and reference implementation Java compilers, virtual mac/ines, and class
libraries ?ere developed by Sun from 11 and first released in 1!& +s of May ,'':, in
compliance ?it/ t/e specifications of t/e Java Community 2rocess, Sun relicensed most
of its Java tec/nologies under t/e GG$ General 2ublic Hicense& Ot/ers /ave also
developed alternative implementations of t/ese Sun tec/nologies, suc/ as t/e GG$
Compiler for Java %bytecode compiler), GG$ Classpat/ %standard libraries), and @cedEea-
Beb %bro?ser plugin for applets)&
istory
James Gosling, Mi;e S/eridan, and 2atric; Gaug/ton initiated t/e Java language project
in June 11&
71,8
Java ?as originally designed for interactive television, but it ?as too
advanced for t/e digital cable television industry at t/e time&
7148
E/e language ?as initially
called Oak after an oa; tree t/at stood outside GoslingDs office. it ?ent by t/e name
Green later, and ?as later renamed Java, from Java coffee,
71-8
said to be consumed in
large Iuantities by t/e languageDs creators&
7citation needed8
Gosling aimed to implement a
virtual mac/ine and a language t/at /ad a familiar C*C55 style of notation&
Sun Microsystems released t/e first public implementation as Java 1&' in 1!&
718
@t
promised ABrite Once, Cun +ny?/ereA %BOC+), providing no-cost run-times on
popular platforms& Jairly secure and featuring configurable security, it allo?ed net?or;-
and file-access restrictions& Major ?eb bro?sers soon incorporated t/e ability to run Java
applets ?it/in ?eb pages, and Java Iuic;ly became popular& Bit/ t/e advent of Java 2
%released initially as J,S" 1&, in 0ecember 1# K 1), ne? versions /ad multiple
configurations built for different types of platforms& Jor eFample, J2EE targeted
enterprise applications and t/e greatly stripped-do?n version J2ME for mobile
applications %Mobile Java)& J2SE designated t/e Standard "dition& @n ,''9, for mar;eting
purposes, Sun renamed ne? J2 versions as Java EE, Java ME, and Java SE, respectively&
@n 1:, Sun Microsystems approac/ed t/e @SO*@"C JEC1 standards body and later t/e
"cma @nternational to formali3e Java, but it soon ?it/dre? from t/e process&
7198
Java
remains a de facto standard, controlled t/roug/ t/e Java Community 2rocess&
71:8
+t one
time, Sun made most of its Java implementations available ?it/out c/arge, despite t/eir
proprietary soft?are status& Sun generated revenue from Java t/roug/ t/e selling of
licenses for speciali3ed products suc/ as t/e Java "nterprise System& Sun distinguis/es
bet?een its Soft?are 0evelopment 1it %S01) and Cuntime "nvironment %JC") %a subset
of t/e S01). t/e primary distinction involves t/e JC"Ds lac; of t/e compiler, utility
programs, and /eader files&
On Govember 14, ,''9, Sun released muc/ of Java as free and open source soft?are,
%JOSS), under t/e terms of t/e GG$ General 2ublic Hicense %G2H)& On May #, ,'':,
Sun finis/ed t/e process, ma;ing all of JavaDs core code available under free
soft?are*open-source distribution terms, aside from a small portion of code to ?/ic/ Sun
did not /old t/e copyrig/t&
71#8
SunDs vice-president Cic/ Green said t/at SunDs ideal role ?it/ regards to Java ?as as an
Aevangelist&A
718
Jollo?ing Oracle CorporationDs acIuisition of Sun Microsystems in
,''K,'1', Oracle /as described itself as t/e Aste?ard of Java tec/nology ?it/ a
relentless commitment to fostering a community of participation and transparencyA&
7,'8

E/is did not /old Oracle, /o?ever, from filing a la?suit against Google s/ortly after t/at
for using Java inside t/e +ndroid S01 %see Google section belo?)& Java soft?are runs on
everyt/ing from laptops to data centers, game consoles to scientific supercomputers&
E/ere are 4' million Java Cuntime "nvironment do?nloads eac/ year and 4 billion
mobile p/ones run Java&
7,18
On +pril ,, ,'1', James Gosling resigned from Oracle&
7,,8
Principles
James Gosling, t/e creator of Java
E/ere ?ere five primary goals in t/e creation of t/e Java language:
7,48
1& @t s/ould be Asimple, object-oriented and familiarA
,& @t s/ould be Arobust and secureA
4& @t s/ould be Aarc/itecture-neutral and portableA
-& @t s/ould eFecute ?it/ A/ig/ performanceA
!& @t s/ould be Ainterpreted, t/readed, and dynamicA
!ersions
Main article: Java version /istory
Major release versions of Java, along ?it/ t/eir release dates:
J01 1&' %January ,1, 19)
J01 1&1 %Jebruary 1, 1:)
J,S" 1&, %0ecember #, 1#)
J,S" 1&4 %May #, ,''')
J,S" 1&- %Jebruary 9, ,'',)
J,S" !&' %September 4', ,''-)
Java S" 9 %0ecember 11, ,''9)
Java S" : %July ,#, ,'11)
Java S" # %Marc/ 1#, ,'1-)
Practices
Java platform
Main articles: Java %soft?are platform) and Java virtual mac/ine
One c/aracteristic of Java is portability, ?/ic/ means t/at computer programs ?ritten in
t/e Java language must run similarly on any /ard?are*operating-system platform& E/is is
ac/ieved by compiling t/e Java language code to an intermediate representation called
Java bytecode, instead of directly to platform-specific mac/ine code& Java bytecode
instructions are analogous to mac/ine code, but t/ey are intended to be interpreted by a
virtual mac/ine %>M) ?ritten specifically for t/e /ost /ard?are& "nd-users commonly
use a Java Cuntime "nvironment %JC") installed on t/eir o?n mac/ine for standalone
Java applications, or in a Beb bro?ser for Java applets&
Standardi3ed libraries provide a generic ?ay to access /ost-specific features suc/ as
grap/ics, t/reading, and net?or;ing&
+ major benefit of using bytecode is porting& =o?ever, t/e over/ead of interpretation
means t/at interpreted programs almost al?ays run more slo?ly t/an programs compiled
to native eFecutables ?ould& Just-in-Eime %J@E) compilers ?ere introduced from an early
stage t/at compile bytecodes to mac/ine code during runtime&
Implementations
See also: Jree Java implementations
Oracle Corporation is t/e current o?ner of t/e official implementation of t/e Java S"
platform, follo?ing t/eir acIuisition of Sun Microsystems on January ,:, ,'1'& E/is
implementation is based on t/e original implementation of Java by Sun& E/e Oracle
implementation is available for Mac OS L, Bindo?s and Solaris& <ecause Java lac;s any
formal standardi3ation recogni3ed by "cma @nternational, @SO*@"C, +GS@, or ot/er t/ird-
party standards organi3ation, t/e Oracle implementation is t/e de facto standard&
E/e Oracle implementation is pac;aged into t?o different distributions: E/e Java
Cuntime "nvironment %JC") ?/ic/ contains t/e parts of t/e Java S" platform reIuired to
run Java programs and is intended for end-users, and t/e Java 0evelopment 1it %J01),
?/ic/ is intended for soft?are developers and includes development tools suc/ as t/e
Java compiler, Javadoc, Jar, and a debugger&
OpenJ01 is anot/er notable Java S" implementation t/at is licensed under t/e G2H& E/e
implementation started ?/en Sun began releasing t/e Java source code under t/e G2H&
+s of Java S" :, OpenJ01 is t/e official Java reference implementation&
E/e goal of Java is to ma;e all implementations of Java compatible& =istorically, SunDs
trademar; license for usage of t/e Java brand insists t/at all implementations be
AcompatibleA& E/is resulted in a legal dispute ?it/ Microsoft after Sun claimed t/at t/e
Microsoft implementation did not support CM@ or JG@ and /ad added platform-specific
features of t/eir o?n& Sun sued in 1:, and in ,''1 ?on a settlement of $SM,' million,
as ?ell as a court order enforcing t/e terms of t/e license from Sun&
7,-8
+s a result,
Microsoft no longer s/ips Bindo?s ?it/ Java&
2latform-independent Java is essential to Java "", and an even more rigorous validation
is reIuired to certify an implementation& E/is environment enables portable server-side
applications&
Performance
Main article: Java performance
2rograms ?ritten in Java /ave a reputation for being slo?er and reIuiring more memory
t/an t/ose ?ritten in C55& =o?ever, Java programsD eFecution speed improved
significantly ?it/ t/e introduction of Just-in-time compilation in 1:*1# for Java 1&1,
7
t/e addition of language features supporting better code analysis %suc/ as inner classes,
t/e String<uilder class, optional assertions, etc&), and optimi3ations in t/e Java virtual
mac/ine itself, suc/ as =otSpot becoming t/e default for SunDs J>M in ,'''&
Some platforms offer direct /ard?are support for Java. t/ere are microcontrollers t/at can
run Java in /ard?are instead of a soft?are Java virtual mac/ine, and +CM based
processors can /ave /ard?are support for eFecuting Java bytecode t/roug/ t/eir Ja3elle
option&
Automatic memory management
Java uses an automatic garbage collector to manage memory in t/e object lifecycle& E/e
programmer determines ?/en objects are created, and t/e Java runtime is responsible for
recovering t/e memory once objects are no longer in use& Once no references to an object
remain, t/e unreac/able memory becomes eligible to be freed automatically by t/e
garbage collector& Somet/ing similar to a memory lea; may still occur if a programmerDs
code /olds a reference to an object t/at is no longer needed, typically ?/en objects t/at
are no longer needed are stored in containers t/at are still in use& @f met/ods for a
noneFistent object are called, a Anull pointer eFceptionA is t/ro?n&
One of t/e ideas be/ind JavaDs automatic memory management model is t/at
programmers can be spared t/e burden of /aving to perform manual memory
management& @n some languages, memory for t/e creation of objects is implicitly
allocated on t/e stac;, or eFplicitly allocated and deallocated from t/e /eap& @n t/e latter
case t/e responsibility of managing memory resides ?it/ t/e programmer& @f t/e program
does not deallocate an object, a memory lea; occurs& @f t/e program attempts to access or
deallocate memory t/at /as already been deallocated, t/e result is undefined and difficult
to predict, and t/e program is li;ely to become unstable and*or cras/& E/is can be
partially remedied by t/e use of smart pointers, but t/ese add over/ead and compleFity&
Gote t/at garbage collection does not prevent AlogicalA memory lea;s, i&e& t/ose ?/ere
t/e memory is still referenced but never used&
Garbage collection may /appen at any time& @deally, it ?ill occur ?/en a program is idle&
@t is guaranteed to be triggered if t/ere is insufficient free memory on t/e /eap to allocate
a ne? object. t/is can cause a program to stall momentarily& "Fplicit memory
management is not possible in Java&
Java does not support C*C55 style pointer arit/metic, ?/ere object addresses and
unsigned integers %usually long integers) can be used interc/angeably& E/is allo?s t/e
garbage collector to relocate referenced objects and ensures type safety and security&
+s in C55 and some ot/er object-oriented languages, variables of JavaDs primitive data
types are not objects& >alues of primitive types are eit/er stored directly in fields %for
objects) or on t/e stac; %for met/ods) rat/er t/an on t/e /eap, as is commonly true for
objects %but see escape analysis)& E/is ?as a conscious decision by JavaDs designers for
performance reasons& <ecause of t/is, Java ?as not considered to be a pure object-
oriented programming language& =o?ever, as of Java !&', autoboFing enables
programmers to proceed as if primitive types ?ere instances of t/eir ?rapper class&
Java contains multiple types of garbage collectors& <y default,
7citation needed8
=otSpot uses t/e
parallel scavenge garbage collector& =o?ever, t/ere are also several ot/er garbage
collectors t/at can be used to manage t/e /eap& Jor 'N of applications in Java, t/e
Concurrent Mar; S?eep garbage collector is sufficient&
74'8
Oracle aims to replace CMS
?it/ t/e Garbage-first collector %G1)&
Synta"
Main article: Java syntaF
E/e syntaF of Java is largely derived from C55& $nli;e C55, ?/ic/ combines t/e syntaF
for structured, generic, and object-oriented programming, Java ?as built almost
eFclusively as an object-oriented language& +ll code is ?ritten inside a class, and
everyt/ing is an object, ?it/ t/e eFception of t/e primitive data types %i&e& integers,
floating-point numbers, boolean values, and c/aracters), ?/ic/ are not classes for
performance reasons&
$nli;e C55, Java does not support operator overloading or multiple in/eritance for
classes& E/is simplifies t/e language and aids in preventing potential errors and anti-
pattern design&
Java uses similar commenting met/ods to C55& E/ere are t/ree different styles of
comments: a single line style mar;ed ?it/ t?o slas/es %//), a multiple line style opened
?it/ /* and closed ?it/ */, and t/e Javadoc commenting style opened ?it/ /** and
closed ?it/ */& E/e Javadoc style of commenting allo?s t/e user to run t/e Javadoc
eFecutable to compile documentation for t/e program&
#"ample$
// This is an example of a single line comment using two slashes

/* This is an example of a multiple line comment using the slash and
asterisk.
This type of comment can be used to hold a lot of information or
deactivate
code, but it is very important to remember to close the comment. */

package fibsandlies;
import java.util.ash!ap;

/**
* This is an example of a "avadoc comment; "avadoc can compile
documentation
* from this text. "avadoc comments must immediately precede the thing
being documented.
*/
public class #ib$alculator extends #ibonacci implements $alculator %
private static ash!ap&'nteger, 'nteger( memoi)ed * new
ash!ap&'nteger, 'nteger(+,;
static %
memoi)ed.put+-, -,;
memoi)ed.put+., -,;
/

/** 0n example of a method written in "ava, wrapped in a class.
* 1iven a non2negative number #'3'4567, returns
* the 4th #ibonacci number, where 4 e8uals #'3'4567.
* 9param fib'ndex The index of the #ibonacci number
* 9return The #ibonacci number itself
*/
public static int fibonacci+int fib'ndex, %
if +memoi)ed.contains:ey+fib'ndex,, %
return memoi)ed.get+fib'ndex,;
/ else %
int answer * fibonacci+fib'ndex 2 -, ; fibonacci+fib'ndex 2
.,;
memoi)ed.put+fib'ndex, answer,;
return answer;
/
/
/
#"amples
ello %orld
E/e traditional =ello ?orld program can be ?ritten in Java as:
7418
class ello<orld0pp %
public static void main+=tring>? args, %
=ystem.out.println+@ello <orldA@,; // 5isplay the string.
/
/
Eo compare t/is to ot/er programming languages see t/e list of /ello ?orld program
eFamples&
Source files must be named after t/e public class t/ey contain, appending t/e suffiF
.java, for eFample, ello<orld0pp.java& @t must first be compiled into bytecode, using
a Java compiler, producing a file named ello<orld0pp.class& Only t/en can it be
eFecuted, or Dlaunc/edD& E/e Java source file may only contain one public class, but it can
contain multiple classes ?it/ ot/er t/an public access and any number of public inner
classes&
+ class t/at is not declared public may be stored in any &java file& E/e compiler ?ill
generate a class file for eac/ class defined in t/e source file& E/e name of t/e class file is
t/e name of t/e class, ?it/ .class appended& Jor class file generation, anonymous classes
are treated as if t/eir name ?ere t/e concatenation of t/e name of t/eir enclosing class, a
$, and an integer&
E/e ;ey?ord public denotes t/at a met/od can be called from code in ot/er classes, or
t/at a class may be used by classes outside t/e class /ierarc/y& E/e class /ierarc/y is
related to t/e name of t/e directory in ?/ic/ t/e &java file is located&
E/e ;ey?ord static in front of a met/od indicates a static met/od, ?/ic/ is associated
only ?it/ t/e class and not ?it/ any specific instance of t/at class& Only static met/ods
can be invo;ed ?it/out a reference to an object& Static met/ods cannot access any class
members t/at are not also static&
E/e ;ey?ord void indicates t/at t/e main met/od does not return any value to t/e caller&
@f a Java program is to eFit ?it/ an error code, it must call System&eFit%) eFplicitly&
E/e met/od name AmainA is not a ;ey?ord in t/e Java language& @t is simply t/e name of
t/e met/od t/e Java launc/er calls to pass control to t/e program& Java classes t/at run in
managed environments suc/ as applets and "nterprise Java<ean do not use or need a
main+, met/od& + Java program may contain multiple classes t/at /ave main met/ods,
?/ic/ means t/at t/e >M needs to be eFplicitly told ?/ic/ class to launc/ from&
E/e main met/od must accept an array of String objects& <y convention, it is referenced
as args alt/oug/ any ot/er legal identifier name can be used& Since Java !, t/e main
met/od can also use variable arguments, in t/e form of public static void
main+=tring... args,, allo?ing t/e main met/od to be invo;ed ?it/ an arbitrary
number of =tring arguments& E/e effect of t/is alternate declaration is semantically
identical %t/e args parameter is still an array of =tring objects), but it allo?s an
alternative syntaF for creating and passing t/e array&
E/e Java launc/er launc/es Java by loading a given class %specified on t/e command line
or as an attribute in a J+C) and starting its public static void main+=tring>?,
met/od& Stand-alone programs must declare t/is met/od eFplicitly& E/e =tring>? args
parameter is an array of =tring objects containing any arguments passed to t/e class&
E/e parameters to main are often passed by means of a command line&
2rinting is part of a Java standard library: E/e System class defines a public static field
called out& E/e out object is an instance of t/e Brint=tream class and provides many
met/ods for printing data to standard out, including println(String) ?/ic/ also
appends a ne? line to t/e passed string&
E/e string A=ello BorldOA is automatically converted to a String object by t/e compiler&
Special classes
Applet
// ello.java
import javax.swing."0pplet;
import java.awt.1raphics;

public class ello extends "0pplet %
public void paint$omponent+final 1raphics g, %
g.draw=tring+@ello, worldA@, CD, ED,;
/
/
E/e import statements direct t/e Java compiler to include t/e javax.swing.JApplet
and java.awt.Graphics classes in t/e compilation& E/e import statement allo?s t/ese
classes to be referenced in t/e source code using t/e simple class name %i&e& "0pplet)
instead of t/e fully qualified class name %FQ!, i&e& javax.swing."0pplet)&
E/e ello class extends %subclasses) t/e JApplet %Java +pplet) class. t/e "0pplet
class provides t/e frame?or; for t/e /ost application to display and control t/e lifecycle
of t/e applet& E/e "0pplet class is a JComponent %Java Grap/ical Component) ?/ic/
provides t/e applet ?it/ t/e capability to display a grap/ical user interface %G$@) and
respond to user events&
E/e ello class overrides t/e paintComponent(Graphics) met/od %additionally
indicated ?it/ t/e annotation, supported as of J01 1&!, Fverride) in/erited from t/e
$ontainer superclass to provide t/e code to display t/e applet& E/e paint$omponent+,
met/od is passed a Graphics object t/at contains t/e grap/ic conteFt used to display t/e
applet& E/e paint$omponent+, met/od calls t/e grap/ic conteFt drawString(String,
int, int) met/od to display t/e &ello' %orld(& string at a piFel offset of %!", #")
from t/e upper-left corner in t/e appletDs display&
&A5F$TGB6 T!H BI3H'$ @2//<J$//5T5 T!H K.L-//64@
@httpM//www.wJ.org/TN/htmlK/strict.dtd@(
&A22 ello.html 22(
&html(
&head(
&title(ello <orld 0pplet&/title(
&/head(
&body(
&applet code*@ello.class@ width*@.LL@ height*@.LL@(
&/applet(
&/body(
&/html(
+n applet is placed in an =EMH document using t/e $applet% =EMH element& E/e
applet tag /as t/ree attributes set: code&'(ello' specifies t/e name of t/e "0pplet
class and width&')**' height&')**' sets t/e piFel ?idt/ and /eig/t of t/e applet&
+pplets may also be embedded in =EMH using eit/er t/e object or embed element,
74,8

alt/oug/ support for t/ese elements by Beb bro?sers is inconsistent&
7448
=o?ever, t/e
applet tag is deprecated, so t/e object tag is preferred ?/ere supported&
E/e /ost application, typically a Beb bro?ser, instantiates t/e (ello applet and creates
an 0pplet$ontext for t/e applet& Once t/e applet /as initiali3ed itself, it is added to t/e
+BE display /ierarc/y& E/e paint$omponent+, met/od is called by t/e +BE event
dispatc/ing t/read ?/enever t/e display needs t/e applet to dra? itself&
Servlet
Main article: Java Servlet
Java Servlet tec/nology provides Beb developers ?it/ a simple, consistent mec/anism
for eFtending t/e functionality of a Beb server and for accessing eFisting business
systems& Servlets are server-side Java "" components t/at generate responses %typically
=EMH pages) to reIuests %typically =EE2 reIuests) from clients& + servlet can almost be
t/oug/t of as an applet t/at runs on t/e server sideP?it/out a face&
// ello.java
import java.io.*;
import javax.servlet.*;

public class ello extends 1eneric=ervlet %
public void service+final =ervletNe8uest re8uest, final
=ervletNesponse response,
throws =ervlet6xception, 'F6xception %
response.set$ontentType+@text/html@,;
final Brint<riter pw * response.get<riter+,;
try %
pw.println+@ello, worldA@,;
/ finally %
pw.close+,;
/
/
/
E/e import statements direct t/e Java compiler to include all of t/e public classes and
interfaces from t/e java.io and javax.servlet pac;ages in t/e compilation& 2ac;ages
ma;e Java ?ell suited for large scale applications&
E/e (ello class extends t/e GenericServlet class. t/e 1eneric=ervlet class
provides t/e interface for t/e server to for?ard reIuests to t/e servlet and control t/e
servletDs lifecycle&
E/e ello class overrides t/e service(Servlet+e,uest, Servlet+esponse) met/od
defined by t/e =ervlet interface to provide t/e code for t/e service reIuest /andler& E/e
service+, met/od is passed: a Servlet+e,uest object t/at contains t/e reIuest from
t/e client and a Servlet+esponse object used to create t/e response returned to t/e
client& E/e service+, met/od declares t/at it throws t/e eFceptions =ervlet6xception
and 'F6xception if a problem prevents it from responding to t/e reIuest&
beAdvantages
- +utomatic memory allocation and garbage collection
- fully object-oriented language facilitating modular design
- platform-independent
- secure
- facilitates distributed programming
- strong multit/reading support
- strictly type language promoting robust code development
Disadvantages
- slo?er t/an C, C55, and many ot/er languages
- strictly typed
- not suitable for scripting
en ?ritten to t/e stream to be returned to t/e client&

You might also like