Java Basics - an Introduction
Published on: Mar 2nd, 2025
What is Java?
Java is a high-level, object-oriented programming language that was first released by Sun Microsystems in 1995. It is designed to be platform-independent, meaning that Java applications can run on any device or operating system with a Java Virtual Machine (JVM). Java is widely used for building everything from mobile apps (especially Android) to large-scale enterprise systems, thanks to its reliability, scalability, and rich ecosystem of libraries and frameworks.
Java is also one of the first programming languages that most educational institutions teach when introducing students to object-oriented programming (OOP). Its strong typing, structured approach, and widespread use make it an ideal language for learning key programming concepts such as inheritance, encapsulation, and polymorphism.
Why Use Java?
Java's primary strength lies in its portability, scalability, and vast community support. Java follows the principle of "Write Once, Run Anywhere" (WORA), meaning that you can write a program once and run it on any platform with a JVM. This makes Java an excellent choice for applications that need to work across different devices and operating systems. Additionally, Java’s built-in garbage collection and strong exception handling ensure that your applications run smoothly and manage resources efficiently.
Java also provides a mature ecosystem of tools, libraries, and frameworks, such as Spring and Hibernate, which can help developers build everything from simple websites to complex enterprise applications.
Setting Up Your First Java Project
To get started with Java, the first thing you need to do is install the
Java Development Kit (JDK).
installed by visiting this link JDK
Download, you then want to open
your IDE, I personally use IntelliJ Idea
IntelliJ Download but
you can use any IDE
that supports the use of Java, after
you open your IDE click "Create New Project" and select Java from the list of options, then give
your project a name and the SDK you installed earlier,
then click finish and you are ready to start coding. In the project make sure to create a
Main.java which will be the starting point for the program to launch form
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
This is a simple example of a Java program that will print "Hello, World!" to the console when run, this is a very basic example but it shows the basic structure of a Java program.
Understanding Classes and Objects in Java
In Java, the core concept is the class. A class is a blueprint for creating objects, which are instances of the class. Each object can hold its own state and behavior, defined by fields (variables) and methods (functions) within the class.
For example, consider a class called Person
that represents a person's attributes:
public class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void greet() {
System.out.println("Hello, my name is " + name + " and I am " + age + " years old.");
}
}
In this example, the Person
class has two fields: name
and
age
, and a constructor that initializes these fields. It also has a method called
greet()
that prints a greeting message using the person's name and age.
To create an instance of the Person
class and call the greet()
method:
public class Main {
public static void main(String[] args) {
Person person = new Person("Alice", 30);
person.greet();
}
}
This will create a new Person
object named person
with the name
"Alice"
and age 30, and then call the greet()
method to print the greeting message.
Understanding Variables and Data Types
Java is a statically typed language, which means you must declare the type of a variable before you can use it. Here’s an overview of some common data types in Java:
int
: Integer values (e.g., 42)double
: Floating-point values (e.g., 3.14)boolean
: Boolean values (e.g., true or false)String
: Textual data (e.g., "Hello, World!")char
: Single characters (e.g., 'A')
Here’s an example of declaring and initializing variables of different data types:
int age = 30;
double pi = 3.14;
boolean isStudent = true;
String name = "Alice";
char grade = 'A';
In this example, we have variables for an integer age
, a double pi
, a
boolean isStudent
, a String name
, and a char grade
.
Conditional Statements and Loops in Java
Java offers several control flow statements like if-else, switch, and while loops, which help manage the flow of your program.
Here's an example of an if-else statement:
int age = 30;
if (age >= 18) {
System.out.println("You are an adult.");
} else {
System.out.println("You are a minor.");
}
This code checks if the variable age
is greater than or equal to 18 and prints a
message accordingly.
Here's an example of a while loop:
int count = 0;
while (count < 5) {
System.out.println("Count: " + count);
count++;
}
This code prints the value of the count
variable from 0 to 4 using a while loop.
Here's an example of a for loop
for (int i = 0; i < 5; i++) {
System.out.println("Count: " + i);
}
This code prints the value of the i
variable from 0 to 4 using a for loop.
Handling Exceptions in Java
Exceptions are unexpected events that occur during the execution of a program, such as division by zero or file not found. Java provides a robust exception handling mechanism to deal with such situations.
Here's an example of catching an exception:
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
}
This code attempts to divide 10 by 0, which will throw an ArithmeticException
. The
catch block catches the exception and prints an error message.
In Conclusion
Java remains one of the most popular and versatile programming languages due to its platform independence, rich feature set, and large ecosystem of libraries and tools. By understanding the basics of classes, objects, variables, control flow, and error handling, you can begin building powerful applications in Java.
Additionally, Java is one of the first languages most educational institutions teach when introducing students to object-oriented programming. Its structured approach, clear syntax, and wide usage in the industry make it an excellent foundation for anyone looking to start a career in software development. Whether you're developing desktop applications, mobile apps, or web services, Java provides the tools and flexibility you need to succeed.