Why Java Still Matters
“Write once, run anywhere.” That’s not just a catchy phrase—it’s the philosophy that has kept Java alive since 1995. Today, over 9 million developers worldwide use Java, making it one of the most in-demand programming languages in both industry and academia.
If you’re starting with programming, Java is an excellent foundation. It’s structured, reliable, and designed to teach you the essentials of coding logic while preparing you for real-world software development. In this lesson, we’ll walk through the fundamentals of Java programming: program structure, development phases, types of errors, coding standards, keywords, identifiers, literals, data types, variables, and output statements.
You won’t just read definitions—you’ll see working examples. From a simple HelloWorld
program to understanding how System.out.println()
works, every section is tied to actual code you can run. By the end, you’ll be able to write small Java programs that follow best practices and avoid common mistakes. That’s the first big step to becoming not just a Java programmer, but a problem-solver with code.
Ready? Let’s break down the building blocks of Java programming, one concept at a time, and turn theory into practical skills you can use immediately.
Program Structure in Java
Every Java program follows a clear structure. At the core, you’ll always find three essential parts:
- Class definition – Every Java program must have at least one class. Think of a class as the container that holds your code.
- Main method – The entry point of the program. It’s where the execution begins.
- Statements – Instructions that tell the computer what to do.
Here’s the simplest Java program you can write:
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
Let’s break it down step by step:
public class HelloWorld
– This defines a class named HelloWorld.public static void main(String[] args)
– This is the main method. The JVM (Java Virtual Machine) looks for this method first when it starts your program.System.out.println("Hello, World!");
– This prints text to the console. The line ends with a semicolon, which tells Java the statement is complete.
By understanding this structure, you can already build simple programs. Add more statements inside the main
method, and you’ll start to see how Java executes instructions in order.
Java Program Development Phases
Writing code is only the beginning. A Java program goes through several phases before it actually runs. Understanding these steps helps you see what’s happening behind the scenes.
- Edit – You write the source code in a text editor or IDE (like VS Code, IntelliJ, or Eclipse). The file is saved with a
.java
extension. - Compile – The
javac
compiler translates your source code into bytecode. This creates a.class
file, which is platform-independent. - Load – The Java Virtual Machine (JVM) loads the bytecode into memory.
- Verify – The JVM checks the bytecode to ensure it’s safe and follows Java’s security rules.
- Execute – Finally, the JVM runs your program, line by line.
Here’s a simple way to picture it: you write instructions in Java, the compiler translates them into a universal language (bytecode), and the JVM makes sure they run correctly on any system. That’s what gives Java its famous platform independence.
For beginners, the key takeaway is this: every time you save and run your Java file, this full cycle—edit, compile, load, verify, execute—is happening in the background. The better you understand it, the easier debugging and optimisation become.
Errors in Java
No program is perfect the first time. Errors are part of the learning and development process. The key is knowing how to recognise them and fix them quickly. In Java, errors usually fall into three categories:
-
Syntax Errors – These happen when you break Java’s grammar rules. For example, forgetting a semicolon at the end of a line:
System.out.println("Hello, World!")
The compiler will immediately complain, and the program won’t run until you fix it. -
Runtime Errors – These occur while the program is running. Imagine dividing a number by zero:
int result = 10 / 0;
The program compiles fine, but crashes when you execute it. -
Logical Errors – These are trickier. The program runs, but the output is wrong because the logic is flawed. For example:
int a = 5, b = 10; System.out.println("Sum = " + a - b);
Instead of adding, this code subtracts. Logical errors don’t stop execution, but they do give you the wrong results.
As a beginner, you’ll likely face all three types. Don’t get discouraged—errors are your best teachers. The more you debug, the faster you’ll learn how Java actually works.
Coding Standards and Guidelines
Writing code that works is one thing. Writing code that others can read, understand, and maintain is another. Good coding standards save time, reduce bugs, and make collaboration much easier.
-
Use meaningful names – Instead of
sn
, usestudentName
. Descriptive names make your code self-explanatory. -
Follow naming conventions – Use camelCase for variables and methods (e.g.,
studentScore
), and PascalCase for classes (e.g.,StudentProfile
). - Indent your code properly – Indentation shows structure. It’s not just about looks; it makes debugging far easier.
- Keep lines short and comments clear – A single line should focus on one idea. Add comments only where the code isn’t obvious.
-
Avoid “magic numbers” – Numbers like
365
in your code can confuse others. Use constants instead:final int DAYS_IN_YEAR = 365;
Following these standards may seem like extra work at first, but in practice, it makes your programs far easier to understand and maintain—both for you and anyone who works with your code in the future.
Keywords, Identifiers, and Literals
Before writing bigger programs, you need to understand the basic “vocabulary” of Java. Three elements form the foundation of every line you write: keywords, identifiers, and literals.
-
Keywords – These are reserved words that have special meaning in Java. You cannot use them as variable names. Examples include:
class, public, if, while, return, static
-
Identifiers – These are names you give to classes, methods, and variables. They must follow rules:
- Must start with a letter,
_
, or$
- Cannot contain spaces
- Are case-sensitive (
score
andScore
are different) - Should be descriptive (
studentAge
is better thanx
)
- Must start with a letter,
-
Literals – These are fixed values written directly in your code. Examples:
int number = 10; // integer literal String name = "Anna"; // string literal double pi = 3.14; // floating-point literal boolean isValid = true; // boolean literal
Once you can tell keywords, identifiers, and literals apart, you’ll find it easier to read and write Java code. This distinction also helps you quickly spot errors when something doesn’t compile.
Data Types in Java
Every variable in Java has a data type. The data type defines what kind of values the variable can hold and what operations you can perform on it. In Java, data types are divided into two categories: primitive and non-primitive.
Primitive Data Types
Primitive types are the most basic building blocks. They store simple values directly in memory.
-
Integer Types
byte
– 8-bit, range: −128 to 127 (byte age = 25;
)short
– 16-bit, range: −32,768 to 32,767 (short year = 2025;
)int
– 32-bit, range: about −2B to +2B (int population = 1500000;
)long
– 64-bit, very large whole numbers (long distanceToMars = 225000000;
)
-
Floating-Point Types
float
– 32-bit, decimal numbers, requiresf
suffix (float price = 19.99f;
)double
– 64-bit, decimal numbers with higher precision (double pi = 3.1415926535;
)
-
Character Type
char
– 16-bit, stores a single Unicode character (char grade = 'A';
)
-
Boolean Type
boolean
– true or false (boolean isJavaFun = true;
)
Non-Primitive Data Types
Non-primitive types are more complex. Instead of storing values directly, they store references to memory locations.
-
Strings – A sequence of characters.
String name = "Henson"; System.out.println(name.toUpperCase()); // HENSON
-
Arrays – Store multiple values of the same type in one container.
int[] scores = {85, 90, 78}; System.out.println(scores[0]); // prints 85
-
Classes and Objects – Classes define blueprints, objects are instances.
class Student { String name; int age; } Student s1 = new Student(); s1.name = "Anna"; s1.age = 20;
Choosing the right data type is important. For example, use int
for counting, double
for precise calculations, and String
for text. This not only saves memory but also keeps your code efficient and readable.
Variables in Java
A variable is a named container in memory that stores data. You can think of it as a label that lets you access and modify values while your program runs.
Declaring Variables
The basic syntax for declaring a variable is:
dataType variableName = value;
Examples:
int age = 20; String name = "John"; boolean isStudent = true;
Key Points About Variables
- Name – The identifier used to access the stored value.
- Type – The kind of data it can hold (
int
,String
,boolean
, etc.). - Value – The actual data stored in the variable.
Rules for Naming Variables
- Must start with a letter,
_
, or$
(not a number). - Cannot contain spaces.
- Case-sensitive (
Age
andage
are different). - Should be descriptive (
studentScore
is better thanx
).
Why Use Variables?
- To store values for later use.
- To make code readable and maintainable.
- To allow values to change during program execution.
For example, instead of writing multiple System.out.println()
statements with hardcoded values, you can store data in variables and reuse them:
String studentName = "Anna"; int studentAge = 20; System.out.println("Name: " + studentName); System.out.println("Age: " + studentAge);
This approach keeps your program flexible. If you update the variable’s value, the change is reflected everywhere it’s used.
Output Statements in Java
Output statements let your program communicate with the user. In Java, the most common way to display results is through the console. Here are the main options:
-
System.out.print()
– Prints text without moving to a new line.System.out.print("Hello"); System.out.print("World!"); // Output: HelloWorld!
-
System.out.println()
– Prints text and moves to a new line.System.out.println("Hello"); System.out.println("World!"); // Output: // Hello // World!
-
System.out.printf()
– Prints formatted text, useful for combining strings, numbers, and variables neatly.String name = "Anna"; int age = 20; System.out.printf("Name: %s, Age: %d", name, age); // Output: Name: Anna, Age: 20
Knowing which output method to use is important. print
is best when you want results on the same line, println
when you need cleaner separation, and printf
when you want more control over formatting.
Quick Recap: Data Types and Variables
Before you start coding on your own, let’s connect two important concepts: data types and variables.
-
A data type defines the kind of value a variable can store (e.g.,
int
for numbers,String
for text,boolean
for true/false). - A variable is the name you assign to that value so you can use it in your program.
Example:
int age = 20; // int = data type, age = variable name, 20 = value String name = "Anna"; // String = data type, name = variable, "Anna" = value
Always choose the right data type for the job. For instance, don’t use double
if you only need whole numbers—stick to int
instead. This keeps your code efficient and easy to maintain.
Practice Activity
It’s time to put theory into practice! Try out the following activities to test your understanding of the fundamentals of Java programming:
-
Write a Java program that displays your name, age, and favourite hobby using
System.out.println()
. - From your program, identify the keywords, identifiers, and literals you used.
-
Change the data type of one of your variables (for example, switch
int
toString
) and observe what happens. Can the program still run?
These exercises will help you reinforce what you’ve learned while also showing you how Java responds when you experiment with code.
Assessment: Test Your Knowledge
Ready to see how well you’ve mastered the fundamentals of Java programming? Take this short quiz to challenge yourself and review the core concepts you’ve just studied.
Download Course Notes
Want to keep a reference copy of today’s lesson? You can download the complete course notes here:
Expand Your Knowledge
Dive deeper into technology and productivity with these related articles:
- Understanding IT – Build a solid foundation in Information Technology essentials.
- Specialist vs Generalist – 85% of companies now seek hybrid talent. Discover whether to specialize or generalize in your career, with actionable strategies to become a T-shaped professional and future-proof your skills.
- Prompt Engineering: Writing Effective AI Prompts – Master the skill of crafting precise AI prompts for better results.
- Understanding Brain Rot in the Digital Age – Break free from digital overload and regain focus.
- Effective Study Techniques for Better Learning – Discover research-backed strategies to boost learning retention.
No comments yet. Be the first to share your thoughts!