So here is our first program in Java. This will just print "Hello World" on the console. Save this code in text file and name the file "HelloWorld.java". Because in Java a file must have the same name as the public class. Lets review the code
public class HelloWorld
{
public static void main(String[] Args)
{
System.out.println("Hello World");
}
}
This is very simple code. I have use indentation in code so that it can be easily understood. In first line of code, We have defined the public class having name "HelloWorld". Inside this class we have only one method "main". Every java program must have at least one function the "main" function. When we execute the program, it starts from "main" function. In "main" function declaration we have written "public", which means it is a public method NOT private or protected. Next we have written "static". This means that it's a static method and doesn't needs to be belonged with any object. void means it will NOT return any thing. This method takes "String[] Args" in arguments. You can give your program arguments and use then from this array. In this method we are just printing a string. "System.out.println" will print the "Hello World" on the screen for us. "println" will print a line and cursor goes to next line. Here we can use "System.out.print" too. In "print" only the cursor stays on the same line.
So this was our first code and I hope you have enjoyed it. Thanks for being with us
No comments:
Post a Comment