Class name: In Java, code is organized into "classes," which act like blueprints for your program. For example, the line public class MyJavaApp
creates a class named MyJavaApp
.
The name of the file must match the class name. If your class is called MyJavaApp
, the file should be named MyJavaApp.java
. If the names don’t match, Java will give you an error when you try to run the program.
Main method: Every Java program starts executing from the main
method. It is declared with public static void main(String[] args)
.
Here’s what each part means:
public: The method can be accessed from anywhere.
static: It belongs to the class itself, not an instance of the class.
void: The method doesn’t return any value.
String[] args: This is an array of strings that holds any arguments passed to the program from the command line.
Java syntax and core concepts#
To build a strong foundation in Java, you'll want to focus first on its building blocks. Let's explore those now.
Data types and variables#
In Java, you must specify the type of data a variable will hold. This is called being statically typed. There are two main types of data:
Primitive Data Types: Basic types like numbers and characters (e.g., int
, double
, char
).
Non-Primitive Data Types: More complex types like objects or arrays, which you define.
Some common data types include:
int
: For integers
double
: For floating-point numbers
char
: For single characters
String
: For strings of characters (objects)
Control structures#
Control structures allow you to dictate the flow of your program:
If-else statements: For decision-making
Loops: Such as for
loops, while
loops, and do-while
loops for repetition of a block of code
Switch statements: For choosing one out of multiple code block to be executed
Object-oriented programming (OOP)#
Java is an "object-oriented programming language (OOP)," which refers to a type of language that organizes code by grouping related tasks and data into "objects." This makes programs easier to manage, use, and expand as they grow.
Classes and objects: The most building blocks of Java codes
Inheritance: Makes it possible for one class to inherit class another by borrowing its field and methods
Polymorphism: Enables one interface to be used for a general class of actions
Encapsulation: The process of grouping the data (attributes) and the operations performed on the data and the data group (functions) under one unit or class
Abstraction: Hiding the implementation details and showing only the functionality
Exception handling#
In Java, exception handling helps prevent your program from crashing when errors occur. You use try
, catch
, and finally
blocks to handle exceptions:
try: The code that might cause an error goes here.
catch: This block handles the error if one occurs.
finally: This block runs after try
and catch
, whether there’s an error or not.
Common exceptions#
In Java, exceptions are errors that happen while the program is running. Here are a few common ones:
NullPointerException
: This happens when you try to use an object that hasn’t been properly set up (initialized).
ArrayIndexOutOfBoundsException
: This occurs when you try to access an item from an array using an index that doesn’t exist (for example, asking for the 5th item in an array that only has 4 items).
ArithmeticException
: This error comes up when you try to do an illegal math operation, like dividing a number by zero.
Ready to build your first Java program?#
Now that you’ve learned the basics about what Java is and where it’s used, it’s time to start writing some actual code!
But before we dive in, we need to set up your computer so you can run Java programs. This involves installing a couple of tools that will help you write and test your Java code.
Set up your development environment#
To start coding in Java, you need to set up something called a development environment. Don’t worry—it’s easier than it sounds! You’ll need to install two things:
Java Development Kit (JDK): This gives you all the tools you need to write and run Java code.
Integrated Development Environment (IDE): This is a program where you’ll write your Java code and test it.
Step 1: Install the JDK#
The JDK is like a toolbox for Java programming. It includes the Java compiler (which turns your code into something the computer can understand) and the Java Virtual Machine (JVM), which runs your programs.
Here’s how to install it:
Go to Oracle’s website and download the latest version of the JDK for your computer.
Follow the installation instructions based on whether you’re using Windows, macOS, or Linux.
After installing, you’ll need to set something called the JAVA_HOME
environment variable, which helps your computer find the JDK. Follow the instructions provided during installation for your operating system.
2. Choose your IDE (your coding workspace)#
An IDE (Integrated Development Environment) is a special program where you write, test, and debug your code. Think of it as your workspace for coding — like a word processor, but for programming! Here are some popular Java IDEs you can choose from:
IntelliJ IDEA
Why it's great: IntelliJ IDEA helps you write code faster with its smart features like auto-completing your code and suggesting improvements. It’s perfect if you’re working on bigger projects, especially since it works really well with tools like Git (for version control) and databases.
Best for: Developers working on more complex projects or who want extra help writing cleaner code.
Eclipse:
Why it's great: Eclipse is an open-source IDE that you can customize with plugins to fit your needs. It’s powerful and works with lots of programming languages, not just Java. However, it might feel a bit heavy for small projects.
Best for: Developers who want flexibility and are working on larger, enterprise-level projects.
Apache NetBeans:
Why it's great: NetBeans is free and easy to use, making it a great choice for beginners. It has a simple, no-frills interface that lets you focus on learning Java without getting overwhelmed by extra features.
Best for: Beginners who want something straightforward and easy to get started with.
Okay, once you've installed an IDE of your preference, you're all set to begin writing actual Java code!
Let's give it a try.
Your first Java program#
Once your environment is set up, it’s time to write your first Java code snippet! A classic way to start is by creating a simple “Hello, World!” program:
Open your IDE and create a new project.
Create a new Java class and name it HelloWorld
.
Write the following code: