For Self Reference purpose,
This repository is to contain my java programs as I learn this language forward.
Create a file with .java extension Open the file, the main class name is the same name as file.
If the file name is farhan.java then the main class is farhan
// main class
class farhan {
// code
}
In the main class, the main method is defined as
public static void main(String[] args)
The type for main method is always void. args is string array holding all command-line arguments.
Two type of methods exist in Java
- public
- private
public methods can be accessed through class instances (Objects) private methods cannot be accessed through class instances
private methods are useful when developers would like to execute a certain code within the class and don't want it to be accessed outside the class through class instances manually.
Rest of the syntax is a similar to that of C language.
Print Hello World Program
// main class
class farhan {
// main method of main class
public static void main(String args[])
{
System.out.println("Hello World!");
// 'println' mean print line - prints an additional '\n' at the end | there is also the method 'print' - prints whatever is given without additional '\n' like in 'println'
}
}
To create a class object, say you created a class calculator with four public methods (sum, dif, product, div)
you first create a variable of the class type and then create a class instance with new keyword.
It would look like this...
calculator calc_object = new calculator();
Here calculator is used as datatype for object calc_object