Introduction to Java
1. Introduction to Java
Java is a high-level, class-based, object-oriented programming language that is designed to
have as few implementation dependencies as possible. It is a general-purpose programming
language intended to let application developers write once, run anywhere (WORA),
meaning that compiled Java code can run on all platforms that support Java without the
need for recompilation.
Setting Up Java Development Environment
Before you start writing Java programs, you need to set up your development environment.
1. Download and install the Java Development Kit (JDK) from Oracle's official website.
2. Install an Integrated Development Environment (IDE) such as Eclipse or IntelliJ IDEA.
Writing Your First Java Program
Let's write a simple Java program that prints "Hello, World!" to the console.
Example:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Exercise
Write a Java program that prints your name to the console.
Solution
public class PrintName {
public static void main(String[] args) {
System.out.println("Your Name");
}
}
2. Basic Syntax
Java syntax is the set of rules that define how a Java program is written and interpreted.
Variables and Data Types
Variables are containers for storing data values. In Java, each variable must be declared with
a specific data type.
Example:
int age = 25;
String name = "John";
Exercise
Create a program that declares variables for your name, age, and city, and then prints them
to the console.
Solution
public class PersonalInfo {
public static void main(String[] args) {
String name = "John";
int age = 25;
String city = "New York";
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("City: " + city);
}
}