Computer Programming 2
Java Background: History
was created in 1991
by James Gosling et al. of Sun Microsystems.
Initially called Oak, in honor of the tree outside Gosling's window, its name was changed to Jaa because
there was already a language called !a".
#he original motiation for Jaa
$ #he need for %latform inde%endent language that could be embedded in arious consumer electronic
%roducts li"e toasters and refrigerators.
!ne of the first %ro&ects deelo%ed using Jaa
$ a %ersonal hand'held remote control named Star7.
(t about the same time, the )orld )ide )eb and the Internet were gaining %o%ularity. Gosling et. al. reali*ed
that Jaa could be used for Internet %rogramming.
What is Java Technology?
#he Jaa technology is+
( %rogramming language
( deelo%ment enironment
(n a%%lication enironment
( de%loyment enironment
$ ,rogramming -anguage
(s a %rogramming language, Jaa can create all "inds of a%%lications that you could create using any
conentional %rogramming language.
$ ( .eelo%ment /nironment
(s a deelo%ment enironment, Jaa technology %roides you with a large suite of tools+
( com%iler
(n inter%reter
( documentation generator
( class file %ac"aging tool
$ (n (%%lication and 0untime /nironment
Jaa technology a%%lications are ty%ically general'%ur%ose %rograms that run on any machine where the Jaa
runtime
enironment 1J0/2 is installed.
$ #here are two main de%loyment enironments+
1. #he J0/ su%%lied by the Jaa 3 Software .eelo%ment 4it 1S.42 contains the com%lete set of class files for
all the Jaa technology %ac"ages, which includes basic language classes, G5I com%onent classes, and so on.
3. #he other main de%loyment enironment is on your web browser.
Most commercial browsers su%%ly a Jaa technology inter%reter and runtime enironment.
$ Jaa6eatures
Some features of Jaa+
#he Jaa 7irtualMachine
Garbage 8ollection
8ode Security
$ Java Virtual achine !JV"
an imaginary machine that is im%lemented by emulating software on a real machine
%roides the hardware %latform s%ecifications to which you com%ile all Jaa technology code
1 # C S 2 $ 2 % C & Computer Programming 2
, e r c i a l ( . 6 e r n a n d e *
$ 9ytecode
a s%ecial machine language that can be understood by the Jaa 7irtual Machine 1J7M2
inde%endent of any %articular com%uter hardware, so any com%uter with a Jaa inter%reter can
e:ecute the com%iled Jaa %rogram, no matter what ty%e of com%uter the %rogram was com%iled on
$ Gar'age collection threa(
res%onsible for freeing any memory that can be freed. #his ha%%ens automatically during the lifetime of the
Jaa %rogram.
%rogrammer is freed from the burden of haing to deallocate that memory themseles
$ Co(e Security
8ode security is attained in Jaa through the im%lementation of its Jaa 0untime /nironment 1J0/2.
$ J0/
runs code com%iled for a J7M and %erforms class loading 1through the class loader2, code erification
1through the bytecode erifier2 and finally code e:ecution
$ 8lass -oader
res%onsible for loading all classes needed for the Jaa %rogram
adds security by se%arating the names%aces for the classes of the local file system from those that are
im%orted from networ" sources
(fter loading all the classes, the memory layout of the e:ecutable is then determined. #his adds %rotection
against unauthori*ed access to restricted areas of the code since the memory layout is determined during
runtime
$ 9ytecode erifier
tests the format of the code fragments and chec"s the code fragments for illegal code that can iolate
access rights to ob&ects
Phases o) a Java Program
#he following figure describes the %rocess of com%iling and e:ecuting a Jaa %rogram
J&V& * Programming +un(amentals
Dissecting your First Java Program
1 %ublic class ;ello
3 <
3 # C S 2 $ 2 % C & Computer Programming 2
, e r c i a l ( . 6 e r n a n d e *
= >??
@ ? My first Jaa %rogram
A ?>
B %ublic static oid main1 StringCD args 2<
E
F >>%rints the string ;ello world on screen
9 System.out.%rintln1G;ello worldH2I
1J
11 K
13 K
Co(ing Gui(elines
1. Lour Jaa %rograms should always end with the .&aa e:tension.
3. +ilenames shoul( match the name o) your pu'lic class. So for e:am%le, if the name of your public class is
,ello, you
should sae it in a file called ,ello-.ava-
=. Lou should write comments in your code e:%laining what a certain class does, or what a certain method do.
= # C S 2 $ 2 % C & Computer Programming 2
, e r c i a l ( . 6 e r n a n d e *
$indicates the name of the class which is ,ello
$In Jaa, all code should be %laced inside a class declaration
$#he class uses an access s%ecifier %ublic, which indicates that our
class in accessible to other classes from other %ac"ages 1%ac"ages
are a collection of classes2.
$#he ne:t line which contains a curly brace / indicates the start of a
bloc".
$In this code, we %laced the curly brace at the ne:t line after the class
declaration, howeer, we can also %lace this ne:t to the first line of
our code.
$#he ne:t three lines indicates a Jaa comment.
$( comment is+
Something used to document a %art of a code.
It is not %art of the %rogram itself, but used for documentation
%ur%oses.
It is good %rogramming %ractice to add comments to your code.
$indicates the name of one method in ;ello which is the
main method.
$#he main method is the starting %oint of a Jaa
%rogram.
$(ll %rograms e:ce%t (%%lets written in Jaa start with
the
main method.
$ Ma"e sure to follow the e:act signature. It is good
%rogramming %ractice to add comments to your code.
$#he command System.out.%rintln12, %rints the te:t
enclosed by Muotation on the screen
$#he last two lines which contains the two curly braces
is
used to close the main method and class res%ectiely.
Java Comments
$ 8omments
#hese are notes written to a code for documentation %ur%oses.
#hose te:ts are not %art of the %rogram and does not affect the flow of the %rogram.
$ = #y%es of comments in Jaa
8NN Style 8omments
8 Style 8omments
S%ecial Jaadoc 8omments
C++-Style Comments
8NN Style comments starts with 00
(ll the te:t after 00 are treated as comments
6or e:am%le+ >> #his is a 8NN style or single line comments
C-Style Comments
8'style comments oralso called multiline comments starts with a 01 and ends with a 10.
(ll te:t in between the two delimiters are treated as comments.
5nli"e 8NN style comments, it can s%an multi%le lines.
6or e:am%le+ >? this is an e:am%le of a 8 style or multiline comments ?>
Special Javadoc Comments
S%ecial Jaadoc comments are used for generating an ;#M- documentation for your Jaa %rograms.
Lou can create &aadoc comments by starting the line with 011 and ending it with 10.
-i"e 8'style comments, it can also s%an lines.
It can also contain certain tags to add more information to your comments.
6or e:am%le+ >?? #his is an e:am%le of s%ecial &aa doc comments used for On generating an html
documentation. It uses tags li"e+ Pauthor 6lorence 9alagtas Persion 1.3 ?>
Java Statements
$ Statement
one or more lines of code terminated by a semicolon.
/:am%le+ System.out.println(Hello world);
Java 2locks
$ 9loc"
is one or more statements bounded by an o%ening and closing curly braces that grou%s the statements as one
unit.
9loc" statements can be nested indefinitely.
(ny amount of white s%ace is allowed.
/:am%le+
public static void main( Strin!" ars )#
System.out.println($Hello$);
System.out.println($world);
%
Java 3(enti)iers
$ Identifiers
are to"ens that re%resent names of ariables, methods, classes, etc.
/:am%les of identifiers are+ Hello, main, System, out.
$ Jaa identifiers are case'sensitie.
#his means that the identifier ,ello is not the same as hello.
$ Identifiers must begin with either a letter, an underscore GQH, or a dollar sign GRH. -etters may be lower or u%%er
case.
@ # C S 2 $ 2 % C & Computer Programming 2
, e r c i a l ( . 6 e r n a n d e *
SubseMuent characters may use numbers J to 9.
$ Identifiers cannot use Jaa "eywords li"e class, %ublic, oid, etc.
Co(ing Gui(elines
1. 6or names of classes, ca%itali*e the first letter of the class name.
6or e:am%le+ ThisIsn!"am#le$%&lass'ame
3. 6or names of methods and ariables, the first letter of the word should start with a small letter.
6or e:am%le+ thisIsn!"am#le$%(ethod'ame
=. In case of multi'word identifiers, use ca%ital letters to indicate the start of the word e:ce%t the first word.
6or e:am%le+ charrray, %ile'um)er, &lass'ame.
@. (oid using underscores at the start of the identifier such as 4rea( or 45rite.
Java 6ey5or(s
$ 4eywords are %redefined identifiers resered by Jaa for a s%ecific %ur%ose.
$ Lou cannot use "eywords as names for your ariables,classes, methods ... etc.
Java 7iterals
$ -iterals are to"ens that do not change or are constant.
$ #he different ty%es of literals in Jaa are+
Integer -iterals
6loating',oint -iterals
9oolean -iterals
8haracter -iterals
String -iterals
A # C S 2 $ 2 % C & Computer Programming 2
, e r c i a l ( . 6 e r n a n d e *
3nteger
S%ecial Sotations in using integer literals in our %rograms+
.ecimal 1base 1J2
$ So s%ecial notation
$ e:am%le+ 13
;e:adecimal 1base 1B2
$ ,recede by J:orJT
$ e:am%le+ J:8
!ctal 1base F2
$ ,recede by J
$e:am%le+ J1@