0% found this document useful (0 votes)
15 views11 pages

Q 1

Uploaded by

hlemorvan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
15 views11 pages

Q 1

Uploaded by

hlemorvan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 11
2ariv2023 11.25, Java Method Parameters 3 w Tutorials» —Exercisesw Servicese§ QO Log in schools Java Method Parameters [errs] Loew | Parameters and Arguments Information can be passed to methods as parameter. Parameters act as variables inside the method. Parameters are specified after the method name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma. The following example has a method that takes a String called fname as parameter. When the method is called, we pass along a first name, which is used inside the method to print the full name: Example public class Main { system.out.println(fname +" Refsnes"); + public static void main(String[] args) { + // Liam Refsnes // Jenny Refsnes hitpssvwwwschools.comfavaljava_methods_param asp wnt 2ariv2023 11.25, Java Method Parameters 3 w Tutorials» —Exercisesw Servicese§ QO Log in schools When a parameter is passed to the method, it is called an argument. So, from the example above: fname is a parameter, while Liam, Jenny and Anja are arguments. Multiple Parameters You can have as many parameters as you like: Example public class Main { system.out.printIn(fname +" is " + age); public static void main(String[] args) { myMethod("Liam", 5)5 myMethod("Jenny", 8)5 myMethod("Anja", 31); // Liam is 5 // Jenny is 8 // anja is 31 Note that when you are working with multiple parameters, the method call must have the same number of arguments as there are parameters, and the arguments must be hitpsslvwwawschools.comfavaljava_methods_param asp am 2ariv2023 11.25, Java Method Parameters 3 w Tutorials» —Exercisesw Servicese§ QO Log in schools sQ PYTHON PHP Return Values The void keyword, used in the examples above, indicates that the method should not return a value. If you want the method to return a value, you can use a primitive data type (such as int, char, etc.) instead of void, and use the return keyword inside the method: Example public class Main { static int myMethod(int x) { public static void main(String[] args) { system.out.print1n(myMethod(3)); + } // Outputs 8 (5 + 3) This example returns the sum of a method's two parameters: Example public class Main { static int myMethod(int x, int y) { return x + y3 public static void main(String[] args) { system.out.print1n(myMethod(S, 3)); + hitpssivwawschools.comfavaljava_methods_param asp aint 2ariv2023 11.25, Java Method Parameters 3 WW tutoriaisy Exercises» Servicesse§ Q 0 schools CSS JAVASCRIPT SQL. PYTHON. «JAVA. PHP-—«=S« HOWTO —_—W3,CSS Try it Yourselt » You can also store the result in a variable (recommended, as it is easier to read and maintain): Example public class Main { static int myMethod(int x, int y) { return x + ys public static void main(String[] args) { int z = myMethod(5, 3); system. out.printIn(z) 5 + // Outputs 8 (5 + 3) Try it Yourself » ‘ADVERTISEMENT ADVERTISEMENT htipsifinww.w3schoots comiaval 1_methods_param.asp an 2ariv2023 11.25, Java Method Parameters 3 w Tutorials~ Exercisese Serviesey§ QO SignUp Login = css JAVASCRIPT SQL PYTHON JAVA PHP. HOW TO W3.CSS Cc Example public class Main { // Create a checkAge() method with an integer variable called age static void checkAge(int age) { // Tf age is less than 18, print "access denied” if (age < 18) { System.out.println("Access denied - You are not old enough!"); // If age is greater than, or equal to, 18, print “access granted” y else { System.out.printIn("Access granted - You are old enough!"); public static void main(String[] args) { checkAge(2@); // Call the checkAge method and pass along an age of 20 // Outputs “Access granted - You are old enough!" Try it Yourself » Test Yourself With Exercises Exercise: Adda fname parameter of type String to myMethod , and output "John Doe": htipsifmarw.w3schoots.comijavajava_methods_param.asp sat 2ariv2023 11.25, Java Method Parameters 3 w Tutorials» —Exercisesw Servicese§ QO Log in schools public static void main(String[] args) { myMethod("John") ; Start the Exercise ADVERTISEMENT hitpsslvwwawschools.comfavaljava_methods_param asp ent 2ariv2023 11.25, Java Method Parameters 3 w Tutorials Exercisesw Servicesey§ QO O Login schools w COLOR PICKER hitpssivwwwschools.comfavaljava_methods_param.asp mM 2ariv2023 11.25, Java Method Parameters v’ Tutorials» —Exercisesw Servicese§ QO Log in W3Schools Spaces Build Your Web, Mobile, and Desktop Solutions Get Premium! a Exetel) Ey ADVERTISEMENT hitpssivwwwschools.comfavaljava_methods_param.asp ant 2ariv2023 11.25, Java Method Parameters 3 w Tutorials» —Exercisesw Servicese§ QO Log in schools sQ PYTHON PHP = HOWTO ADVERTISEMENT ADVERTISEMENT hitpssivwwwschools.comfavaljava_methods_param.asp ant 2ariv2023 11.25, Java Method Parameters Tutorials» —Exercisesy Servicesey§ QO O SignUp Login CSS JAVASCRIPT SQL. PYTHON = JAVA. Ss PHP-—«S HOWTO W3.CSS_— CC 3 Ww SPACES UPGRADE NEWSLETTER schools GET CERTIFIED REPORT ERROR Top Tutorials HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial cH Tutorial Query Tutorial Top References HTML Reference CSS Reference JavaScript Reference SQL Reference Python Reference WA.CSS Reference Bootstrap Reference PHP Reference HTML Colors Java Reference ‘Angular Reference jQuery Reference Top Examples Get Certified HTML Certificate htipsfmarw.w3schoots.comijavaljava_methods_param.asp sont 2ariv2023 11.25, Java Method Parameters 3 WW tutoriaisy — Brercisese Servicesey Q O SignUp Login schools = CSS JAVASCRIPT SQL. PYTHON «= JAVA. PHP-—«S HOWTO—W3.CSS_— CC Wa.cSS Examples Query Certificate Bootstrap Examples Java Certificate PHP Examples c++ Centficate Java Examples Ci Certificate XML Examples XML Certificate jQuery Examples G@ @& B® © Forum asour \W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 1999-2023 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS, htipsfmarw.w3schoots.comijavaljava_methods_param.asp wnt

You might also like