Java Methods - W3Schools
文章推薦指數: 80 %
Java Methods ... A method is a block of code which only runs when it is called. You can pass data, known as parameters, into a method. Methods are used to perform ... Tutorials References Exercises Videos Menu Login FreeWebsite GetCertified Pro HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP BOOTSTRAP HOWTO W3.CSS C C++ C# REACT R JQUERY DJANGO TYPESCRIPT NODEJS MYSQL Darkmode Darkcode × Tutorials HTMLandCSS LearnHTML LearnCSS LearnRWD LearnBootstrap LearnW3.CSS LearnColors LearnIcons LearnGraphics LearnSVG LearnCanvas LearnHowTo LearnSass DataAnalytics LearnAI LearnMachineLearning LearnDataScience LearnNumPy LearnPandas LearnSciPy LearnMatplotlib LearnStatistics LearnExcel XMLTutorials LearnXML LearnXMLAJAX LearnXMLDOM LearnXMLDTD LearnXMLSchema LearnXSLT LearnXPath LearnXQuery JavaScript LearnJavaScript LearnjQuery LearnReact LearnAngularJS LearnJSON LearnAJAX LearnAppML LearnW3.JS Programming LearnPython LearnJava LearnC LearnC++ LearnC# LearnR LearnKotlin LearnGo LearnDjango LearnTypeScript ServerSide LearnSQL LearnMySQL LearnPHP LearnASP LearnNode.js LearnRaspberryPi LearnGit LearnAWSCloud WebBuilding CreateaWebsiteNEW WhereToStart WebTemplates WebStatistics WebCertificates WebDevelopment CodeEditor TestYourTypingSpeed PlayaCodeGame CyberSecurity Accessibility Blog DataAnalytics LearnAI LearnMachineLearning LearnDataScience LearnNumPy LearnPandas LearnSciPy LearnMatplotlib LearnStatistics LearnExcel LearnGoogleSheets XMLTutorials LearnXML LearnXMLAJAX LearnXMLDOM LearnXMLDTD LearnXMLSchema LearnXSLT LearnXPath LearnXQuery × References HTML HTMLTagReference HTMLBrowserSupport HTMLEventReference HTMLColorReference HTMLAttributeReference HTMLCanvasReference HTMLSVGReference GoogleMapsReference CSS CSSReference CSSBrowserSupport CSSSelectorReference Bootstrap3Reference Bootstrap4Reference W3.CSSReference IconReference SassReference JavaScript JavaScriptReference HTMLDOMReference jQueryReference AngularJSReference AppMLReference W3.JSReference Programming PythonReference JavaReference ServerSide SQLReference MySQLReference PHPReference ASPReference XML XMLDOMReference XMLHttpReference XSLTReference XMLSchemaReference CharacterSets HTMLCharacterSets HTMLASCII HTMLANSI HTMLWindows-1252 HTMLISO-8859-1 HTMLSymbols HTMLUTF-8 × ExercisesandQuizzes Exercises HTMLExercises CSSExercises JavaScriptExercises PythonExercises SQLExercises PHPExercises JavaExercises CExercises C++Exercises C#Exercises jQueryExercises React.jsExercises MySQLExercises Bootstrap5Exercises Bootstrap4Exercises Bootstrap3Exercises NumPyExercises PandasExercises SciPyExercises TypeScriptExercises ExcelExercises RExercises GitExercises KotlinExercises GoExercises Quizzes HTMLQuiz CSSQuiz JavaScriptQuiz PythonQuiz SQLQuiz PHPQuiz JavaQuiz CQuiz C++Quiz C#Quiz jQueryQuiz React.jsQuiz MySQLQuiz Bootstrap5Quiz Bootstrap4Quiz Bootstrap3Quiz NumPyQuiz PandasQuiz SciPyQuiz TypeScriptQuiz XMLQuiz RQuiz GitQuiz KotlinQuiz CyberSecurityQuiz AccessibilityQuiz Courses HTMLCourse CSSCourse JavaScriptCourse FrontEndCourse PythonCourse SQLCourse PHPCourse JavaCourse C++Course C#Course jQueryCourse React.jsCourse Bootstrap4Course Bootstrap3Course NumPyCourse PandasCourse TypeScriptCourse XMLCourse RCourse DataAnalyticsCourse CyberSecurityCourse AccessibilityCourse Certificates HTMLCertificate CSSCertificate JavaScriptCertificate FrontEndCertificate PythonCertificate SQLCertificate PHPCertificate JavaCertificate C++Certificate C#Certificate jQueryCertificate React.jsCertificate MySQLCertificate Bootstrap5Certificate Bootstrap4Certificate Bootstrap3Certificate TypeScriptCertificate XMLCertificate ExcelCertificate DataScienceCertificate CyberSecurityCertificate AccessibilityCertificate × Tutorials References Exercises GetCertified Spaces Videos Shop Pro JavaTutorial JavaHOME JavaIntro JavaGetStarted JavaSyntax JavaOutput JavaComments JavaVariables Variables PrintVariables DeclareMultipleVariables Identifiers JavaDataTypes DataTypes Numbers Booleans Characters Non-primitiveTypes JavaTypeCasting JavaOperators JavaStrings Strings Concatenation NumbersandStrings SpecialCharacters JavaMath JavaBooleans JavaIf...Else If...Else ShortHandIf...Else JavaSwitch JavaWhileLoop JavaForLoop ForLoop For-EachLoop JavaBreak/Continue JavaArrays Arrays LoopThroughanArray MultidimensionalArrays JavaMethods JavaMethods JavaMethodParameters JavaMethodOverloading JavaScope JavaRecursion JavaClasses JavaOOP JavaClasses/Objects JavaClassAttributes JavaClassMethods JavaConstructors JavaModifiers JavaEncapsulation JavaPackages/API JavaInheritance JavaPolymorphism JavaInnerClasses JavaAbstraction JavaInterface JavaEnums JavaUserInput JavaDate JavaArrayList JavaLinkedList JavaHashMap JavaHashSet JavaIterator JavaWrapperClasses JavaExceptions JavaRegEx JavaThreads JavaLambda JavaFileHandling JavaFiles JavaCreate/WriteFiles JavaReadFiles JavaDeleteFiles JavaHowTo AddTwoNumbers JavaReference JavaKeywords abstract boolean break byte case catch char class continue default do double else enum extends final finally float for if implements import instanceof int interface long new package private protected public return short static super switch this throw throws try void while JavaStringMethods JavaMathMethods JavaExamples JavaExamples JavaCompiler JavaExercises JavaQuiz JavaCertificate JavaMethods ❮Previous Next❯ Amethodisablockofcodewhichonlyrunswhenitiscalled. Youcanpassdata,knownasparameters,intoamethod. Methodsareusedtoperformcertainactions,andtheyarealsoknownasfunctions. Whyusemethods?Toreusecode:definethecodeonce,anduse itmanytimes. CreateaMethod Amethodmustbedeclaredwithinaclass.Itisdefined withthenameofthemethod,followedbyparentheses().Javaprovidessomepre-definedmethods,suchasSystem.out.println(),butyoucanalsocreateyourownmethodstoperformcertainactions: Example CreateamethodinsideMain: publicclassMain{ staticvoidmyMethod(){ //codetobeexecuted } } ExampleExplained myMethod()isthenameofthemethod staticmeansthatthemethodbelongstothe MainclassandnotanobjectoftheMainclass.Youwilllearnmoreaboutobjectsandhowtoaccessmethodsthroughobjectslaterinthistutorial. voidmeansthatthismethoddoesnothavea returnvalue.Youwilllearnmoreaboutreturnvalueslaterinthischapter CallaMethod TocallamethodinJava,writethemethod'snamefollowedbytwo parentheses()andasemicolon; Inthefollowingexample,myMethod()isusedtoprintatext(theaction),whenitiscalled: Example Insidemain,callthe myMethod()method: publicclassMain{ staticvoidmyMethod(){ System.out.println("Ijustgotexecuted!"); } publicstaticvoidmain(String[]args){ myMethod(); } } //Outputs"Ijustgotexecuted!" TryitYourself» Amethodcanalsobecalledmultipletimes: Example publicclassMain{ staticvoidmyMethod(){ System.out.println("Ijustgotexecuted!"); } publicstaticvoidmain(String[]args){ myMethod(); myMethod(); myMethod(); } } //Ijustgotexecuted! //Ijustgotexecuted! //Ijustgotexecuted! TryitYourself» Inthenextchapter,MethodParameters,youwilllearnhowtopassdata(parameters)intoamethod. TestYourselfWithExercises Exercise: InsertthemissingparttocallmyMethodfrommain. staticvoidmyMethod(){ System.out.println("Ijustgotexecuted!"); } publicstaticvoidmain(String[]args){ ; } SubmitAnswer» StarttheExercise ❮Previous Next❯ NEW WejustlaunchedW3Schoolsvideos Explorenow COLORPICKER Getcertifiedbycompletingacoursetoday! w3schoolsCERTIFIED.2022 Getstarted CODEGAME PlayGame
延伸文章資訊
- 1Methods in Java | Types, Method Signature, Example
Learn types of methods in java with example program, method declaration, method signature in java...
- 2Java Methods (With Examples) - Programiz
Java Methods ; User-defined Methods · Standard Library Methods ; methodName · method body ; modif...
- 3What are Methods in Java? - Great Learning
Different Types of Methods in Java · 1. Pre – Defined Methods/ Standard Library Methods/System de...
- 4What are predefined methods in java - Linux Hint
In this program we utilized println() method to print different type of data i.e. integer, string...
- 5Java Methods - W3Schools
Java Methods ... A method is a block of code which only runs when it is called. You can pass data...