Create your first Java program
INTRODUCTION
More than 3 billion devices run Java.
Java is used to develop apps for Google's Android OS, various Desktop Applications, such as media players, antivirus programs, Web Applications, Enterprise Applications (i.e. banking), and many more!
"Java Programs are platform independent. Because when the Java program runs in a particular machine it is sent to java compiler, which converts this code into intermediate code called bytecode. This bytecode is sent to Java virtual machine (JVM) which resides in the RAM of any operating system. JVM recognizes the platform it is on and converts the bytecodes into native machine code. Hence java is called platform independent language."
WRITE YOUR FIRST JAVA PROGRAM
you can use any Java development kit to run your java program (ex-: Geany, eclipse or any other)
In Java, each application has an entry point, or a starting point, which is a method called main. Along with main, the keywords public and static will also be explained later
- Every program in Java must have a class
- Every Java program starts from the main method
ex-:
Output -:
In Java, every line of code that can actually run needs to be inside a class.
In our example, we named the class Exmple (also source file save as Exmple.java) . You will learn more about classes in the upcoming modules.
THE MAIN METHOD
To run our program, the main method must be identical to this signature:
public static void main(String[ ] args)
- public: anyone can access it
- static: method can be run without creating an instance of the class containing the main method
- void: method doesn't return any value
- main: the name of the method
For example, the following code declares a method called test, which does not return anything and has no parameters:void test()
The method's parameters are declared inside the parentheses that follow the name of the method.
For main, it's an array of strings called args.
main method declaration
valid declaration -:
- public static void main(String args[])
- public static void main(String []args)
- public static void main(String somename[])
- static public void main(String args[])
- public static void main(String...args)
Invalid declaration -:
This are compiled but not executed -:
- static void main(String args[])
- void main(String args[])
- public static void Main(String args[])
- public static void main(String args)
- public static void main()
These are not execute or compile -:
- public static main(String args[])
- public void static main(String args[])
- public static void main(String[])
- public static void main(String)
System.out.println()
Next is the body of the main method, enclosed in curly braces:
{
System.out.println("Hello World!");
}
The println method prints a line of text to the screen.
The System class and its out stream are used to access the println method.
In classes, methods, and other flow-control structures code is always enclosed in curly braces { }.
Semicolons in Java
In Java, each code statement must end with a semicolon.
Remember: do not use semicolons after method and class declarations that follow with the body defined using the curly braces.
nice work. i hope you post 2nd step quickly
ReplyDelete2nd step -: Java Comment
ReplyDeletehttps://javabykawmadiekariyawasam.blogspot.com/2020/07/step-2-java-comments.html