Java methods tutorial:
In this given tutorial, you are going to learn about Java methods. Java program language a program may consist of one or more classes, also a class contains a method (Function in C and C++).
In java programming main method is a must for the program execution and the method has the name and return type.
-> public, private is the Access specifiers, to decides classes can call a method.
-> static, synchronized are Keywords used for some particular methods.
->Return type indicates return value, Method name is a valid Java identifier name.
keywords, Access specifiers, and arguments are optional.
Types of methods declaration:
public static void main(String[] args);
* // Main method where program execution begins
public static void main(String[] args) {
staticMethod();
Methods object = new Methods();
object.nonStaticMethod();
}
void myMethod();
// Constructor method
Methods()
{
System.out.println("Constructor method is called when an object of its class is created");
}
private int maximum();
public synchronized int search(java.lang.Object);
// Static method
static void staticMethod() {
System.out.println("Static method can be called without creating object");
}
// Non static method
void nonStaticMethod() {
System.out.println("Non static method must be called by creating an object");
}
}
Java String methods:
{
public static void main(String args[])
{
int n;
String s = "Java programming", t = "", u = "";
System.out.println(s);
// Find the length of string
n = s.length();
System.out.println("Number of characters = " + n);
// Replace characters in the string
t = s.replace("Java", "Java Script");
System.out.println(s);
System.out.println(t);
// Concatenating string with another string
u = s.concat(" is fun");
System.out.println(s);
System.out.println(u);
}
}