Java Java Java
Java Java Java
The tools include compiler (javac.exe), Java application launcher (java.exe), Appletvie er, etc! "ompiler converts java code into #$te code. Java application launcher opens a JRE, loads the class, and invo%es its main method.
There are &' #asic sections to the Java "lass (ile structure)
Magic Number) 'x"A(E*A*E Version of Class File Format) the minor and major versions o+ the class +ile Constant Pool) ,ool o+ constants +or the class Access Flags) +or example hether the class is a#stract, static, etc. This Class) The name o+ the current class Super Class) The name o+ the super class Interfaces) An$ inter+aces in the class Fields) An$ +ields in the class Methods) An$ methods in the class Attributes) An$ attri#utes o+ the class (+or example the name o+ the source+ile, etc.)
-ou need JDK, i+ at all $ou ant to rite $our o n programs, and to compile them. (or running java programs, JRE is su++icient. JRE is targeted +or execution o+ Java +iles i.e. JRE . J/0 1 Java ,ac%ages "lasses(li%e util, math, lang, a t,s ing etc)1runtime li#raries. JDK is mainl$ targeted +or java development. 2.e. -ou can create a Java +ile ( ith the help o+ Java pac%ages), compile a Java +ile and run a java +ile. J ! "Ja#a untime !n#ironment$ Java Runtime Environment contains J/0, class li#raries, and other supporting +iles. 2t does not contain an$ development tools such as compiler, de#ugger, etc. Actuall$ J/0 runs the program, and it uses the class li#raries, and other supporting +iles provided in JRE. 2+ $ou ant to run an$ java program, $ou need to have JRE installed in the s$stem The Java /irtual 0achine provides a plat+orm3independent an$ machine and run it an$ here(an$ machine). a$ o+ executing code4 That mean compile once in
JVM "Ja#a Virtual Machine$ As e all a are hen e compile a Java +ile, output is not an 5exe6 #ut it6s a 5.class6 +ile. 5.class6 +ile consists o+ Java #$te codes hich are understanda#le #$ J/0. Java /irtual 0achine interprets the #$te code into the machine code depending upon the underl$ing operating s$stem and hard are com#ination. 2t is responsi#le +or all the things li%e gar#age collection, arra$ #ounds chec%ing, etc! J/0 is plat+orm dependent. The J/0 is called 7virtual8 #ecause it provides a machine inter+ace that does not depend on the underl$ing operating s$stem and machine hard are architecture. This independence +rom hard are and operating s$stem is a cornerstone o+ the rite3once run3an$ here value o+ Java programs.
The Java virtual machine %no s nothing o+ the Java programming language, onl$ o+ a particular #inar$ +ormat, the
class +ile
+ormat. A class +ile contains Java virtual machine instructions (orbytecodes) and a s$m#ol ta#le, as ell as other ancillar$ in+ormation. (or the sa%e o+ securit$, the Java virtual machine imposes strong +ormat and structural constraints on the code in a class +ile. 9o ever, an$ language ith +unctionalit$ that can #e expressed in terms o+ a valid class +ile can #e hosted #$ the Java virtual machine. Attracted #$ a generall$ availa#le, machine3independent plat+orm, implementers o+ other languages are turning to the Java virtual machine as a deliver$ vehicle +or their languages.
There are di++erent J/0 implementations are there. These ma$ di++er in things li%e per+ormance, relia#ilit$, speed, etc. These implementations ill di++er in those areas here Java speci+ication doesn6t mention ho to
implement the +eatures, li%e ho the gar#age collection process or%s is J/0 dependent, Java spec doesn6t de+ine an$ speci+ic a$ to do this.
JRE . J/0 1 Java ,ac%ages "lasses(li%e util, math, lang, a t,s ing etc)1runtime li#raries.
2n high3level programming languages such as " and "11, e rite a program in a human3reada#le +ormat, and a program called a compiler translates it to a #inar$ +ormat called executa#le code that the computer can understand and execute. The executa#le code depends upon the computer machine that e use to execute our program4 it is machine dependent. 2n Java, this process o+ riting to executing a program is ver$ similar, #ut ith one important di++erence that allo s us to rite Java programs that are machine independent. :sing an interpreter, all Java programs are compiled to an intermediate level called #$te code. ;e can run the compiled #$te code on an$ computer ith the Java runtime environment installed on it. The runtime environment consists o+ a virtual machine and its supporting code.
A set of registers
The registers o+ the Java /irtual 0achine are similar to the registers in our computer. 9o ever, #ecause the /irtual 0achine is stac% #ased, its registers are not used +or passing or receiving arguments. 2n Java, registers hold the machine<s state, and are updated a+ter each line o+ #$te code is executed, to maintain that state. The +ollo ing +our registers hold the state o+ the virtual machine) 1. frame, the re+erence +rame, and contains a pointer to the execution environment o+ the current method. 2. optop, the operand top, and contains a pointer to the top o+ the operand stac%, and is used to evaluate arithmetic expressions. 3. pc, the program counter, and contains the address o+ the next #$te code to #e executed.
The Java /irtual 0achine can support man$ threads o+ execution at once (J=> ?&@). Each Java /irtual 0achine thread has its o n pc (program counter) register. At an$ point, each Java /irtual 0achine thread is executing the code o+ a single method, namel$
native, the pc register contains the address o+ the Java /irtual native, the value o+ the Java /irtual 0achine<s pcregister is unde+ined. The Java /irtual 0achine<s pc register is ide enough to hold a returnAddress or a native pointer on the speci+ic plat+orm.
the current method (?A.B) +or that thread. 2+ that method is not 0achine instruction currentl$ #eing executed. 2+ the method currentl$ #eing executed #$ the thread is
4.
The Java /irtual 0achine uses an operand stac% to suppl$ parameters to methods and operations, and to receive results #ac% +rom them. All #$te code instructions ta%e operands +rom the stac%, operate on them, and return results to the stac% The operand stac% +ollo s the last3in +irst3out (=2(C) methodolog$, and expects the operands on the stac% to #e in a speci+ic order.
Each Java /irtual 0achine thread has a private Java /irtual 0achine stac%, created at the same time as the thread. A Java /irtual 0achine stac% stores +rames (?A.B). A Java /irtual 0achine stac% is analogous to the stac% o+ a conventional language such as ") it holds local varia#les and partial results, and pla$s a part in method invocation and return. *ecause the Java /irtual 0achine stac% is never manipulated directl$ except to push and pop +rames, +rames ma$ #e heap allocated. The memor$ +or a Java /irtual 0achine stac% does not need to #e contiguous. 2+ the computation in a thread reDuires a larger Java /irtual 0achine stac% than is permitted, the Java /irtual 0achine thro s a >tac%Cver+lo Error.
The follo&ing e'ceptional conditions are associated &ith Ja#a Virtual Machine stac%s(
2+ Java /irtual 0achine stac%s can #e d$namicall$ expanded, and expansion is attempted #ut insu++icient memor$ can #e made availa#le to e++ect the expansion, or i+ insu++icient memor$ can #e made availa#le to create the initial Java /irtual 0achine stac% +or a ne thread, the Java /irtual 0achine thro s an CutC+0emor$Error.
Frame
A +rame is used to store data and partial results, as ell as to per+orm d$namic lin%ing, return values +or methods, and dispatch exceptions. A ne +rame is created each time a method is invo%ed. A +rame is destro$ed hen its method invocation completes, hether that completion is normal or a#rupt (it thro s an uncaught exception). (rames are allocated +rom the Java /irtual 0achine stac% o+ the thread creating the +rame. Each +rame has its o n arra$ o+ local varia#les , its o n operand stac% , and a re+erence to the run3time constant pool o+ the class o+ the current method.
The stac% frame holds the state o+ the method ith three sets o+ data) the method<s local varia#les, the method<s execution environment, and the method<s operand stac%. Although the siEes o+ the local varia#le and the execution environment data sets are al a$s +ixed at the start o+ the method call, the siEe o+ the operand stac% changes as the method<s #$te code instructions are executed.
An e'ecution en#ironment
Cracle<s Java execution environment is termed the Java Runtime Environment, or JRE. ,rograms intended to run on a J/0 must #e compiled into Java #$tecode, a standardiEed porta#le #inar$ +ormat hich t$picall$ comes in the +orm o+ .class +iles (Java class +iles). A program ma$ consist o+ man$ classes in di++erent +iles. (or easier distri#ution o+ large programs, multiple class +iles ma$ #e pac%aged together in a .jar +ile (short +or Java archive). The Java application launchers, java and java o++er a standard a$ o+ executing Java code, ith or ithout a console indo respectivel$. The J/0 runtime executes .class or .jar +iles, emulating the J/0 instruction set #$ interpreting it or using a just3in3 time compiler (J2T) such as Cracle<s 9ot>pot. J2T compiling, not interpreting, is used in most J/0s toda$ to achieve greater speed. There are also ahead3o+3time compilers that ena#le developers to precompile class +iles into native code +or particular plat+orms. =i%e most virtual machines, the Java virtual machine has a stac%3#ased architecture a%in to a microcontrollerFmicroprocessor. 9o ever, the J/0 also has lo 3level support +or Java3li%e classes and methods, hich amounts to a highl$ idios$ncraticGclari+ication neededH memor$ model and capa#ilit$3#ased architecture.
;henever a method completes normall$, a value is returned to the calling method. The execution environment handles normal method returns #$ restoring the registers o+ the caller and incrementing the program counter o+ the caller to s%ip the method call instruction. Execution o+ the program then continues in the calling method<s execution environment.
A garbage)collected heap
The Java virtual machine heap is the area o+ memor$ used #$ the J/0, speci+icall$ 9ot>pot, +or d$namic memor$ allocation The heap is divided into generations) The $oung generation stores short3lived o#jects that are created and immediatel$ gar#age collected. C#jects that persist longer are moved to the old generation (also called the tenured generation). The permanent generation (or permgen) is used +or class de+initions and associated metadata. ,ermanent generation is not part o+ the heap
Criginall$ there as no permanent generation, and o#jects and classes ere stored together in the same area. *ut as class unloading occurs much more rarel$ than o#jects are collected, moving class structures to a speci+ic area allo s signi+icant per+ormance improvements.
The Java /irtual 0achine has a heap that is shared among all Java /irtual 0achine threads. The heap is the run3time data area +rom hich memor$ +or all class instances and arra$s is allocated. The heap is created on virtual machine start3up. 9eap storage +or o#jects is reclaimed #$ an automatic storage management s$stem (%no n as a gar#age collector)4 o#jects are never explicitl$ deallocated. The Java /irtual 0achine assumes no particular t$pe o+ automatic storage management s$stem, and the storage management techniDue ma$ #e chosen according to the implementor<s s$stem reDuirements. The heap ma$ #e o+ a +ixed siEe or ma$ #e expanded as reDuired #$ the computation and ma$ #e contracted i+ a larger heap #ecomes unnecessar$. The memor$ +or the heap does not need to #e contiguous. 2+ a computation reDuires more heap than can #e made availa#le #$ the automatic storage management s$stem, the Java /irtual 0achine thro s an CutC+0emor$Error.
A constant pool
A run3time constant pool is a per3class or per3inter+ace run3time representation o+ the constantIpool ta#le in a class +ile . 2t contains several %inds o+ constants, ranging +rom numeric literals %no n at compile3time to method and +ield re+erences that must #e resolved at run3time. Each run3time constant pool is allocated +rom the Java /irtual 0achine<s method area . The run3time constant pool +or a class or inter+ace is constructed hen the class or inter+ace is created #$ the Java /irtual 0achine. Each class in the heap has a constant pool associated ith it. *ecause constants do not change, the$ are usuall$ created at compile time. 2tems in the constant pool encode all the names used #$ an$ method in a particular class. The class contains a count o+ ho man$ constants exist, and an o++set that speci+ies here a particular listing o+ constants #egins ithin the class description.
ith a constant +ollo s a speci+ic +ormat #ased on the t$pe o+ the constant.
A method storage area Java<s method area is similar to the compiled code areas o+ the runtime environments used #$ other programming languages. 2t stores #$te code instructions that are associated ith methods in the compiled code, and the s$m#ol ta#le the execution environment needs +or d$namic lin%ing. An$ de#ugging or additional in+ormation that might need to #e associated ith a method is stored in this area as ell. An instruction set Although programmers pre+er to rite code in a high3level +ormat, our computer cannot execute this code directl$, hich is h$ e must compile Java programs #e+ore e can run them. Jenerall$, compiled code is either in a machine3reada#le +ormat called machine language or in an intermediate3level +ormat such as the assem#l$ language or Java #$te code.
System.out.println("Hello world!");
At compile time, the Java compiler converts the single3line print statement to the +ollo ing #$te code) The JDK provides a tool +or examining #$te code called the Ja#a class file dissembler. ;e can run the disassem#ler #$ t$ping javap at the command line.
*+namic ,in%ing
Each +rame (?A.B) contains a re+erence to the run3time constant pool (?A.K.K) +or the t$pe o+ the current method to support dynamic linking o+ the method code. The class +ile code +or a method re+ers to methods to #e invo%ed and varia#les to #e accessed via s$m#olic re+erences. D$namic lin%ing translates these s$m#olic method re+erences into concrete method re+erences, loading classes as necessar$ to resolve as3$et3unde+ined s$m#ols, and translates varia#le accesses into appropriate o++sets in storage structures associated ith the run3time location o+ these varia#les.
Ja#a ,anguage
Ja#a ,anguage
/a#a
Tools . Tool APIs
/ar JF Troubleshoot
JConsole I*,
*eplo+ment
Ja#aF5
J*Integration ,ibraries
I*,
J*0C
JN*I
MI
MI)II7P
Scripting
J !
7ther 0ase ,ibraries
Ja#a S! API
JA ef 7b/ects Instrumentation
,ogging eflection
Ja#a :otSpot VM