Java is one of the most popular and versatile programming languages in the world. It is widely used for web development, mobile apps, game development, and enterprise solutions. The language was developed by Sun Microsystems in 1995 and has since become a cornerstone in software development.
If you’re new to programming or looking to transition to Java, this blog will guide you through the basics and help you get started with Java programming.
Before diving into coding, let’s understand why Java is such a popular language.
To start programming in Java, you first need to install the Java Development Kit (JDK).
You can write Java code using any text editor, but using an IDE (Integrated Development Environment) will make your coding experience much easier.
Some popular Java IDEs are:
For this tutorial, we’ll use IntelliJ IDEA, but the steps will be similar across different IDEs.
Once you’ve set up your development environment, it’s time to write your first Java program. Let’s create a simple "Hello, World!" application.
HelloWorld.java
.Here’s the code for your first program:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
HelloWorld
. In Java, every application must have a class as its entry point.Hello, World!
to the console.To run the program, click on the green play button in IntelliJ IDEA, or use the following command in your terminal:
java HelloWorld
You should see the output:
Hello, World!
Now that you’ve created your first program, let’s take a closer look at Java syntax.
Java is a strongly-typed language, meaning that every variable must be declared with a specific data type. Some common data types are:
int
: Integer numbers (e.g., 10, 25)double
: Floating-point numbers (e.g., 10.5, 3.14)char
: Single characters (e.g., 'a', 'b')String
: A sequence of characters (e.g., "Hello", "Java")boolean
: True or false values (e.g., true, false)Here's how you can declare and use variables:
public class DataTypesExample {
public static void main(String[] args) {
int age = 25;
double height = 5.9;
char initial = 'J';
String name = "John";
boolean isStudent = true;
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Height: " + height);
System.out.println("Initial: " + initial);
System.out.println("Is Student: " + isStudent);
}
}
Java uses control structures such as if-else, loops, and switch to control the flow of the program.
public class IfElseExample {
public static void main(String[] args) {
int number = 10;
if (number > 0) {
System.out.println("The number is positive.");
} else if (number < 0) {
System.out.println("The number is negative.");
} else {
System.out.println("The number is zero.");
}
}
}
public class ForLoopExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println("Iteration: " + i);
}
}
}
Java is an object-oriented programming language, which means it focuses on the concept of "objects" and "classes."
A class is a blueprint for creating objects. An object is an instance of a class. Let’s create a simple Car
class to demonstrate this.
public class Car {
// Fields
String make;
String model;
int year;
// Method
void displayInfo() {
System.out.println("Car Info: " + make + " " + model + " (" + year + ")");
}
public static void main(String[] args) {
// Creating an object of the Car class
Car myCar = new Car();
myCar.make = "Toyota";
myCar.model = "Corolla";
myCar.year = 2020;
// Calling the method
myCar.displayInfo();
}
}
Car
class with fields (make
, model
, and year
) and a method (displayInfo
).Car
class and set its values.One of the core principles of Java is inheritance, where a subclass inherits the properties and behaviors of its superclass. Polymorphism allows one object to take many forms, which enables flexibility in your programs.
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
public class InheritanceExample {
public static void main(String[] args) {
Animal animal = new Animal();
animal.sound();
Dog dog = new Dog();
dog.sound(); // This calls the overridden method
}
}