Lesson: A Closer Look at the "Hello World!" Application

文章推薦指數: 80 %
投票人數:10人

The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later ... Documentation TheJava™Tutorials HideTOC ACloserLookatthe"HelloWorld!"Application Trail:GettingStarted HomePage > GettingStarted « Previous • Trail • Next » TheJavaTutorialshavebeenwrittenforJDK8.Examplesandpracticesdescribedinthispagedon'ttakeadvantageofimprovementsintroducedinlaterreleasesandmightusetechnologynolongeravailable.SeeJavaLanguageChangesforasummaryofupdatedlanguagefeaturesinJavaSE9andsubsequentreleases.SeeJDKReleaseNotesforinformationaboutnewfeatures,enhancements,andremovedordeprecatedoptionsforallJDKreleases. Lesson:ACloserLookatthe"HelloWorld!"Application Nowthatyou'veseenthe"HelloWorld!"application(andperhapsevencompiledandrunit),youmightbewonderinghowitworks.Hereagainisitscode: classHelloWorldApp{ publicstaticvoidmain(String[]args){ System.out.println("HelloWorld!");//Displaythestring. } } The"HelloWorld!"applicationconsistsofthreeprimarycomponents:sourcecodecomments,theHelloWorldAppclassdefinition,andthemainmethod.Thefollowingexplanationwillprovideyouwithabasicunderstandingofthecode,butthedeeperimplicationswillonlybecomeapparentafteryou'vefinishedreadingtherestofthetutorial. SourceCodeComments Thefollowingboldtextdefinesthecommentsofthe"HelloWorld!"application: /** *TheHelloWorldAppclassimplementsanapplicationthat *simplyprints"HelloWorld!"tostandardoutput. */ classHelloWorldApp{ publicstaticvoidmain(String[]args){ System.out.println("HelloWorld!");//Displaythestring. } } Commentsareignoredbythecompilerbutareusefultootherprogrammers.TheJavaprogramminglanguagesupportsthreekindsofcomments: /*text*/ Thecompilerignoreseverythingfrom/*to*/. /**documentation*/ Thisindicatesadocumentationcomment(doccomment,forshort).Thecompilerignoresthiskindofcomment,justlikeitignorescommentsthatuse/*and*/.Thejavadoctoolusesdoccommentswhenpreparingautomaticallygenerateddocumentation.Formoreinformationonjavadoc,seethe Javadoc™tooldocumentation. //text Thecompilerignoreseverythingfrom//totheendoftheline. TheHelloWorldAppClassDefinition Thefollowingboldtextbeginstheclassdefinitionblockforthe"HelloWorld!"application: /** *TheHelloWorldAppclassimplementsanapplicationthat *simplydisplays"HelloWorld!"tothestandardoutput. */ classHelloWorldApp{ publicstaticvoidmain(String[]args){ System.out.println("HelloWorld!");//Displaythestring. } } Asshownabove,themostbasicformofaclassdefinitionis: classname{ ... } Thekeywordclassbeginstheclassdefinitionforaclassnamedname,andthecodeforeachclassappearsbetweentheopeningandclosingcurlybracesmarkedinboldabove.Chapter2providesanoverviewofclassesingeneral,andChapter4discussesclassesindetail.Fornowitisenoughtoknowthateveryapplicationbeginswithaclassdefinition. ThemainMethod Thefollowingboldtextbeginsthedefinitionofthemainmethod: /** *TheHelloWorldAppclassimplementsanapplicationthat *simplydisplays"HelloWorld!"tothestandardoutput. */ classHelloWorldApp{ publicstaticvoidmain(String[]args){ System.out.println("HelloWorld!");//Displaythestring. } } IntheJavaprogramminglanguage,everyapplicationmustcontainamainmethodwhosesignatureis: publicstaticvoidmain(String[]args) Themodifierspublicandstaticcanbewrittenineitherorder(publicstaticorstaticpublic),buttheconventionistousepublicstaticasshownabove.Youcannametheargumentanythingyouwant,butmostprogrammerschoose"args"or"argv". ThemainmethodissimilartothemainfunctioninCandC++;it'stheentrypointforyourapplicationandwillsubsequentlyinvokealltheothermethodsrequiredbyyourprogram. Themainmethodacceptsasingleargument:anarrayofelementsoftypeString. publicstaticvoidmain(String[]args) Thisarrayisthemechanismthroughwhichtheruntimesystempassesinformationtoyourapplication.Forexample: javaMyApparg1arg2 Eachstringinthearrayiscalledacommand-lineargument.Command-lineargumentsletusersaffecttheoperationoftheapplicationwithoutrecompilingit.Forexample,asortingprogrammightallowtheusertospecifythatthedatabesortedindescendingorderwiththiscommand-lineargument: -descending The"HelloWorld!"applicationignoresitscommand-linearguments,butyoushouldbeawareofthefactthatsuchargumentsdoexist. Finally,theline: System.out.println("HelloWorld!"); usestheSystemclassfromthecorelibrarytoprintthe"HelloWorld!"messagetostandardoutput.Portionsofthislibrary(alsoknownasthe"ApplicationProgrammingInterface",or"API")willbediscussedthroughouttheremainderofthetutorial. «Previous • Trail • Next» AboutOracle| ContactUs| LegalNotices| TermsofUse| YourPrivacyRights Copyright©1995,2022Oracleand/oritsaffiliates.Allrightsreserved. Previouspage:PreviousLesson Nextpage:QuestionsandExercises:GettingStarted



請為這篇文章評分?