Вы находитесь на странице: 1из 5

Object-Oriented Programming (OOP)

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 Ja a because there was already a language called !a". #he original moti ation for Ja a $ #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 de elo%ed using Ja a $ 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 Ja a could be used for Internet %rogramming. What is Java Technology #he Ja a technology is+ ( %rogramming language ( de elo%ment en ironment (n a%%lication en ironment ( de%loyment en ironment $ ,rogramming -anguage (s a %rogramming language, Ja a can create all "inds of a%%lications that you could create using any con entional %rogramming language. $ ( .e elo%ment /n ironment (s a de elo%ment en ironment, Ja a technology %ro ides you with a large suite of tools+ ( com%iler (n inter%reter ( documentation generator ( class file %ac"aging tool $ (n (%%lication and 0untime /n ironment Ja a technology a%%lications are ty%ically general'%ur%ose %rograms that run on any machine where the Ja a runtime en ironment 1J0/2 is installed. $ #here are two main de%loyment en ironments+ 1. #he J0/ su%%lied by the Ja a 3 Software .e elo%ment 4it 1S.42 contains the com%lete set of class files for all the Ja a technology %ac"ages, which includes basic language classes, G5I com%onent classes, and so on. 3. #he other main de%loyment en ironment is on your web browser. Most commercial browsers su%%ly a Ja a technology inter%reter and runtime en ironment. $ Ja a6eatures Some features of Ja a+ #he Ja a 7irtualMachine Garbage 8ollection 8ode Security

$ Java !irt"al #achine (J!#) 1 $ % S & ' ( ) * # Object Oriented Programming ,erci al (. 6ernande*

an imaginary machine that is im%lemented by emulating software on a real machine %ro ides the hardware %latform s%ecifications to which you com%ile all Ja a technology code $ 9ytecode a s%ecial machine language that can be understood by the Ja a 7irtual Machine 1J7M2 inde%endent of any %articular com%uter hardware, so any com%uter with a Ja a inter%reter can e:ecute the com%iled Ja a %rogram, no matter what ty%e of com%uter the %rogram was com%iled on $ Garbage collection thread res%onsible for freeing any memory that can be freed. #his ha%%ens automatically during the lifetime of the Ja a %rogram. %rogrammer is freed from the burden of ha ing to deallocate that memory themsel es $ %ode Sec"rity 8ode security is attained in Ja a through the im%lementation of its Ja a 0untime /n ironment 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 Ja a %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 Ja a %rogram

3 $ % S & ' ( ) * # Object Oriented Programming ,erci al (. 6ernande*

J,!, - Programming ."ndamentals


Dissecting your First Java Program 1 %ublic class ;ello 3 < = >?? @ ? My first Ja a %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 $indicates the name of the class which is /ello $In Ja a, 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 0 indicates the start of a bloc". $In this code, we %laced the curly brace at the ne:t line after the class declaration, howe er, we can also %lace this ne:t to the first line of our code. $#he ne:t three lines indicates a Ja a 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 Ja a %rogram. $(ll %rograms e:ce%t (%%lets written in Ja a start with the main method. $ Ma"e sure to follow the e:act signature. It is good %ractice to add comments to $#he %rogramming command System.out.%rintln12, %rints the your code. te:t enclosed by Luotation on the screen $#he last two lines which contains the two curly braces is used to close the main method and class

%oding G"idelines
= $ % S & ' ( ) * # Object Oriented Programming ,erci al (. 6ernande*

1. Mour Ja a %rograms should always end with the .&a a e:tension. 3. .ilenames sho"ld match the name o+ yo"r 1"blic class . So for e:am%le, if the name of your public class is /ello, you should sa e it in a file called /ello2java2 =. Mou should write comments in your code e:%laining what a certain class does, or what a certain method do. Java %omments $ 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 Ja a 8NN Style 8omments 8 Style 8omments S%ecial Ja adoc 8omments C++-Style Comments 8NN Style comments starts with 33 (ll the te:t after 33 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 34 and ends with a 43. (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 Ja adoc comments are used for generating an ;#M- documentation for your Ja a %rograms. Mou can create &a adoc comments by starting the line with 344 and ending it with 43. -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 &a a doc comments used for On generating an html documentation. It uses tags li"e+ Pauthor 6lorence 9alagtas P ersion 1.3 ?> Java Statements $ Statement one or more lines of code terminated by a semicolon. /:am%le+ System.out.println(Hello world); Java 5locks $ 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 !" ar s )# System.out.println($Hello$); System.out.println($world); % @ $ % S & ' ( ) * # Object Oriented Programming ,erci al (. 6ernande*

Java 6denti+iers $ Identifiers are to"ens that re%resent names of ariables, methods, classes, etc. /:am%les of identifiers are+ Hello, main, System, out. $ Ja a identifiers are case'sensiti e. #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. SubseLuent characters may use numbers J to 9. $ Identifiers cannot use Ja a "eywords li"e class, %ublic, oid, etc. %oding G"idelines 1. 6or names of classes, ca%itali*e the first letter of the class name. 6or e:am%le+ ThisIs n!"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+ thisIs n!"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+ char rray, %ile'um)er, &lass'ame. @. ( oid using underscores at the start of the identifier such as 7read or 78rite. Java 9ey8ords $ 4eywords are %redefined identifiers reser ed by Ja a for a s%ecific %ur%ose. $ Mou cannot use "eywords as names for your ariables,classes, methods ... etc.

6nteger Java :iterals S%ecial Sotations in using integer literals in our $ -iterals are to"ens that do not change or are constant. %rograms+ $ #he different ty%es of literals in Ja a are+ .ecimal 1base 1J2 Integer -iterals $ So s%ecial notation 6loating',oint -iterals $ e:am%le+ 13 9oolean -iterals ;e:adecimal 1base 1B2 8haracter -iterals $ ,recede by J:orJT String -iterals $ e:am%le+ J:8 !ctal 1base F2 $ ,recede by J $e:am%le+ J1@ A $ % S & ' ( ) * # Object Oriented Programming ,erci al (. 6ernande*

Вам также может понравиться