public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
HelloWorld
class. class
is the syntax for declaring a class, and prepending with the public
modifer means the class will be visible outside HelloWorld
's package.{
... }
define the contents of the HelloWorld
class, in this case a single method, main
main
MethodIn order to make a class executable with the java
command, it must have a main method:
public static void main(String[] args) { ... }
public
means method can be called from outside the classstatic
means the method can be called without instantiating an object of the class. Static methods (and variables) are sometimes called class methods.void
is the return type. In particular, main
returns nothing.
After the method name, main
, comes the parameter list. main
takes a single parameter of type String[]
- an array of String
s. args
is the name of the parameter, which we can refer to within the body of main
The main
method is a special method that is used as the entry point for a Java program. We can define other methods as well. Consider this method from :
public static String extractLastName(String name) {
int commaPos = name.indexOf(",");
int len = name.length();
String lastName = name.substring(0, commaPos).trim();
return lastName;
}
Similar to our main
method but:
return
s a String
valueString
In this method:
public static String extractLastName(String name) {
int commaPos = name.indexOf(",");
int len = name.length();
String lastName = name.substring(0, commaPos).trim();
return lastName;
}
name
is a parameter (or formal parameter), a local scope variable within the extractLastName
method. It is bound to a value when the method is called. In the statement:
String lastName = extractLastName(fullName);
the right-hand side, extractLastName(fullName)
, is a method invocation (or method call). We say that fullName
is the argument (or actual parameter) to this invocation of the extractLastName
method.
Method parameters and variables declared inside the method are local to the method, invisible outside the method. Local variables "shadow" variables of the same name in an enclosing scope
public class Methods {
// Note that static variables cannot appear inside methods
static String message = "Global message.";
static int a = 5;
public static int add(int a, int b) {
String message = "Adding " + a + " and " + b;
System.out.println(message);
return a + b;
}
...
In the add
method, the parameter a
shadows the static
variable a
, and the local variable message
shadows the static
variable message
Methods that return values are expressions which can be used anywhere a value of the method's return type can be used. Given:
public static int add(int a, int b) { ... }
which returns an int
, this:
x + (x + y)
is equivalent to this:
x + add(x, y)
See Methods.java.
Methods are subprograms with
Methods are a powerful form of procedural abstraction, another step in the building of complex programs from simple parts.