Basic program in Java
Let's create a hello world program.
public class Simple {
public static void main(String args[]) {
System.out.println("Hello world");
}
}
Try this in a text editor.
Save as Simple.java. Remember Filename should be same as class name when it is given public.
Compile as javac Simple.java in command prompt.
Run as java Simple in command prompt.
Output
Hello world
Don't consider class definition which is in first line in this topic and other topics, just consider only what is inside the body of the main function until you learn OOPS concepts in Java.
Do you know about main function ?
If you don't know about main function we have full explanation, you can learn from here.
In the body of the main function,
System.out.print() - is just used to print the output in the console.
This is mainly used to check anything in the program.
We can display a character, number, set of characters, boolean value using this statement.
Values in variables or merging two variables and displaying can also be done using this.
To display as a set of characters we should give characters in between double quotesas in above example.
You can use + to merge a set of characters with other or with variable.
Example
System.out.print("123"+a);
In the above example, a is a variable.
Merge should be done as above.
If you use println instead of print it will print a new line character along with the statement.
Working of this statement will be discussed separately since it needs OOPs concepts. So we will learn after OOPS concepts in Java.