CodeGym
Promotion
CodeGym University
Learning
Courses
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
CodeGym/Help with Java Tasks/How does the established int A and B get attributed to th...
Rica
Level 3
Manchester
  • 16.04.2021
  • 839views
  • 4comments

How does the established int A and B get attributed to the prewritten code with the numbers in the second method?

Question about the task Minimum of two numbers
Java Syntax,  Level 2,  Lesson 8
Under discussion
0
Comments (4)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Anonymous #10755035
Level 3 , Paris, France
22 June 2021, 12:46
int m2; if (a < b) m2 = a; else m2 = b; return m2; }
0
Guadalupe Gagnon
Level 37 , Tampa, United States
16 April 2021, 01:35
Methods are the way different aspects of code communicate with each other. The main purpose is class to class definition, but that complicates a simple construct a bit much when you first are learning it. So: A method is a callable piece of code that you pass input to and either does something with the input, or return a value that typically is the result of doing something with that input. A method is defined with four required parts and one optional: optional static modifier required an access modifier - either public, private, protected, or it can be left blank, which is still a modifier, and is called default required return type - must be included. Use void if the method does not return anything requiredmethod name - follows Javas standard naming rules requiredmethod parameters - inside the parentheses, what is passed to the method from the calling code
public static int min(int a, int b) {
access modifier - public static - included return type - int method name - min method parameters - (int, int) with variable names a and b To successfully call any method you must use its name and then pass it arguments (what you call the values in parentheses from the calling code) that match the methods parameters (just the parameter types). You can see that being done in the main method:
min(12, 33)
min(20, 0)
↑the arguments are (int, int) which match the match parameter of (int, int)
+4
Guadalupe Gagnon
Level 37 , Tampa, United States
16 April 2021, 01:47
It is VERY important that you match the argument types exactly in order as the method parameter. If you had a method with parameters (String, int) and tried calling that method with the arguments (15, "Test String"), you would end up creating an error and the code will fail because the arguments of (int, String) do not match the method parameter of (String, int) As I said way above in the first post, methods are a way of communicating. When you call a method and pass it arguments, the code goes to the method and the variables in the method become the arguments passed to the method. The method's return type is the result of processing those methods with whatever logic you need (in this task just figuring which is the smaller of the two), which is then returned to the calling code location. Another VERY important bit of information is that if a method has a return type specified then ALL code paths in the method must reach a return statement that returns the correct type. If you had a return type of an int, and then coded it to returned a string then the code would fail to compile. If all the return statements are effectively behind logic gates where cases exist where a return statement might not be reached then the code fails to compile. For example:
public boolean isEven(int x){
     if(x < 0) x = x * -1; // if negative makes positive
     while(x > 1) x = x - 2; // will break when x == 0 or 1
     if(x == 0) return true; // if x == 0 then the number was even
     // if x == 1 then this line is reached
}
Here is some simple code of a method that checks if the number passed to it is even by returning a boolean (true or false). This is done by subtracting 2 from the number until you are left with either 1 or 0. If you end up with 0 then the number is even. The problem with this code is that if you end up with 1 then there are no additional return statements. This would cause the code to fail.
+2
Guadalupe Gagnon
Level 37 , Tampa, United States
16 April 2021, 01:52
Simple recap: - methods are ways that programs communicate with other aspects and each other - this communication is done with arguments and return values - arguments are what are passed from calling code, they must match the method parameters - method parameters are part of the method declaration and define what arguments the method accepts - when a method has a return value then the logic in the method MUST reach a valid return statement
+2
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning, and a Java developer’s career.
Follow us
Interface language
English
Deutsch Español हिन्दी Français Português Polski বাংলা 简体中文 मराठी தமிழ் Italiano Bahasa Indonesia 繁體中文 Nederlands 日本語 한국어 Bulgarian Danish Hungarian Basa Jawa Malay Norwegian Romanian Swedish Telugu Thai Українська Filipino Turkish Azərbaycan Русский Vietnamese
Programmers Are Made, Not Born © 2025 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2025 CodeGym