GETTING STARTED WITH JAVA PROGRAMMING |
Now Let's move to the basic program of all programming language's.
Hello World" in Java: A Beginner's Guide
Java is a popular, object-oriented programming language used for building software applications. It's widely used for building desktop, mobile, and web applications, and has been around for over 25 years. In this blog post, we will write a simple "Hello World" program in Java, and explain the basic concepts behind it.
First, let's write the program. Here's the code for "Hello World" in Java:public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } }
1. public class HelloWorld:
In Java, every application starts with a class. A class is a blueprint for creating objects (instances of that class). This line declares a class named HelloWorld. The public keyword means that this class can be accessed from outside of its package.
2. public static void main(String[] args):
3. System.out.println("Hello World");
:
System.out
object is an instance of the
PrintStream
class, and the println
method is used to print a line of text to the console.
4. And that's it!
You have just written your first Java program. To run the program, save it to a file with the .java extension, and then compile it using the javac command. Finally, run the program using the java command.
Important Points
In Java, every code must be contained within a class. It is a convention that the name of the main class, which contains the main() method, should match the name of the file in which it resides. This naming convention helps to maintain clarity and organization in large codebases. The name of the class defined by the program should be the same as the name of the file, for example, if the file is named HelloWorld.java, the class defined within it should be named HelloWorld. It is important to note that a Java program can have multiple classes, but there can only be one public class, which contains the main() method. This public class acts as the entry point for executing the program.This is a valid Java program Syntax that does nothing.
public class HelloWorld { public static void main(String[] args) { // Write your code here } }
Don't worry if you don't understand the meaning of class
, static
, methods, and so on for now. We will discuss it in detail in later tutorials.