Lesson: A Closer Look at the "Hello World!" Application
文章推薦指數: 80 %
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
延伸文章資訊
- 1Java Classes and Objects - W3Schools
Java is an object-oriented programming language. Everything in Java is associated with classes an...
- 2Lesson: A Closer Look at the "Hello World!" Application
The Java Tutorials have been written for JDK 8. Examples and practices described in this page don...
- 3Classes and Objects in Java - GeeksforGeeks
A class is a user defined blueprint or prototype from which objects are created. It represents th...
- 4Classes - Java™ Tutorials
The Java Tutorials have been written for JDK 8. ... Here is sample code for a possible implementa...
- 5Objects and Classes in Java - Javatpoint
Class in Java