Basic Syntax In Java
Package:
A package in Java is like a labeled folder where related classes are organized. For example, the java.util package contains utility classes that help with operations like data structure manipulation, date/time, etc. It helps in avoiding name conflicts and makes your code easier to organize.
Class:
A class in Java is like a blueprint for creating objects. It defines the structure (data members) and behavior (methods) of objects. For instance, in your Java program, the Main class will hold the code that will be executed.
Static:
In Java, static means that the method or variable belongs to the class itself, not to instances of the class. You don’t need to create an object to use a static method. It’s like having a shared tool that everyone can use without creating a new one.
void:
In Java, void means that a method does not return any value. If a method performs an action but doesn’t give anything back, you declare it as void. In your main method, you’re simply running the code, not expecting any return value.
main Method:
The main method in Java is the starting point of the program. When you run your Java application, the JVM (Java Virtual Machine) looks for this method to begin execution. It’s like pressing the “start” button on your program.
public static void main(String[] args)
This is the signature of the main method, and it’s where the execution of your program begins.
String[] args:
The String[] args are the command-line arguments that you can pass when you run the program. These are like extra instructions or data your program can accept. For example, you can pass filenames or configuration settings.This is used only in print.
System.out.println("Hello World!"):
Think of System.out as the console in Java, and println as a method to print a message to it. When you use System.out.println(“Hello World!”);, you’re telling your program to display “Hello World!” on the screen.
For Example:
import java.util.*;
public class Main {
public static void main(String[] args) {
System.out.println(“Hello World!”);
}
}
import java.util.*;
public class Main {
Explanation: This line defines a class named Main. In Java, every program must be enclosed in a class, which is the blueprint for objects. The Main class is where the program will start executing. Think of it as the container for your program’s code.
public static void main(String[] args) {
Explanation: This line defines the main method, which is the entry point of your Java program. Here’s a breakdown of the components:
- public: This keyword means that this method can be accessed from outside the class.
- static: This indicates that the method belongs to the class itself and not to an instance of the class. You don’t need to create an object of the Main class to run this method.
- void: This means that the method does not return any value.
- main: The name of the method. The Java Virtual Machine (JVM) looks for this specific method to start executing the program.
String[] args: These are the command-line arguments. They allow you to pass information to your program when it starts. For example, if you run the program from the terminal with arguments, those arguments are stored in this array.
System.out.println(“Hello World!”);
- Explanation: This line prints the text “Hello World!” to the console. Here’s how it works:
- System: This is a predefined class that contains several useful methods and variables. It deals with input and output among other things.
- out: This is a static member of the System class. It represents the standard output stream (the console or terminal).
println: This method prints the argument passed to it (in this case, “Hello World!”) and then moves the cursor to the next line. It’s similar to C#’s Console.WriteLine.