Introduction to Java
What is Java?
Java is a high-level, object-oriented programming language developed by Sun Microsystems (now owned by Oracle). It is platform-independent and can run on any device with a Java Virtual Machine (JVM).
Features of Java
- Platform Independence
- Object-Oriented
- Secure
- Robust
- Multithreaded
- High Performance
Setting Up Java
To start writing Java programs, follow these steps:
- Download and install the Java Development Kit (JDK) from the Oracle website or OpenJDK.
- Set the environment variable PATH to the JDK's bin directory.
- Verify installation by opening the command prompt and typing
java -version
andjavac -version
.
Writing Your First Java Program
Follow these steps to write and run your first Java program:
Step 1: Create a Java File
Open a text editor and type the following code:
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
Save the file as HelloWorld.java.
Step 2: Compile the Java Program
Open the command prompt, navigate to the directory where you saved the file, and type:
javac HelloWorld.java
This will generate a HelloWorld.class file.
Step 3: Run the Java Program
In the same directory, type:
java HelloWorld
You should see the output:
Hello, World!
Understanding the Code
- public class HelloWorld: Defines a public class named HelloWorld. The file name must match the class name.
- public static void main(String[] args): The main method is the entry point of the program.
- System.out.println("Hello, World!"): Prints "Hello, World!" to the console.
Conclusion
In this tutorial, we introduced Java, set up the environment, and wrote a simple Java program. Explore more by learning Java syntax, loops, and object-oriented programming concepts.