Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

Partner – Microsoft – NPI EA (cat = Baeldung)
announcement - icon

Azure Container Apps is a fully managed serverless container service that enables you to build and deploy modern, cloud-native Java applications and microservices at scale. It offers a simplified developer experience while providing the flexibility and portability of containers.

Of course, Azure Container Apps has really solid support for our ecosystem, from a number of build options, managed Java components, native metrics, dynamic logger, and quite a bit more.

To learn more about Java features on Azure Container Apps, visit the documentation page.

You can also ask questions and leave feedback on the Azure Container Apps GitHub page.

Partner – Microsoft – NPI EA (cat= Spring Boot)
announcement - icon

Azure Container Apps is a fully managed serverless container service that enables you to build and deploy modern, cloud-native Java applications and microservices at scale. It offers a simplified developer experience while providing the flexibility and portability of containers.

Of course, Azure Container Apps has really solid support for our ecosystem, from a number of build options, managed Java components, native metrics, dynamic logger, and quite a bit more.

To learn more about Java features on Azure Container Apps, you can get started over on the documentation page.

And, you can also ask questions and leave feedback on the Azure Container Apps GitHub page.

Partner – Orkes – NPI EA (cat=Spring)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

Partner – Orkes – NPI EA (tag=Microservices)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – All Access – NPI EA (cat= Spring)
announcement - icon

All Access is finally out, with all of my Spring courses. Learn JUnit is out as well, and Learn Maven is coming fast. And, of course, quite a bit more affordable. Finally.

>> GET THE COURSE
Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – LambdaTest – NPI EA (cat=Testing)
announcement - icon

End-to-end testing is a very useful method to make sure that your application works as intended. This highlights issues in the overall functionality of the software, that the unit and integration test stages may miss.

Playwright is an easy-to-use, but powerful tool that automates end-to-end testing, and supports all modern browsers and platforms.

When coupled with LambdaTest (an AI-powered cloud-based test execution platform) it can be further scaled to run the Playwright scripts in parallel across 3000+ browser and device combinations:

>> Automated End-to-End Testing With Playwright

Course – Spring Sale 2025 – NPI EA (cat= Baeldung)
announcement - icon

Yes, we're now running our Spring Sale. All Courses are 25% off until 26th May, 2025:

>> EXPLORE ACCESS NOW

Course – Spring Sale 2025 – NPI (cat=Baeldung)
announcement - icon

Yes, we're now running our Spring Sale. All Courses are 25% off until 26th May, 2025:

>> EXPLORE ACCESS NOW

1. Overview

Java is a statically-typed, object-oriented programming language. It’s also platform-independent — Java programs can be written and compiled on one type of machine, such as a Windows system, and executed on another, such as MacOS, without any modification to the source code.

In this tutorial, we’re going to look at and understand the basics of Java syntax.

2. Data Types

There are two broad categories of data types in Java: primitive types and objects/reference types.

Primitive types are the basic data types that store simple data and form the foundation of data manipulation. For example, Java has primitive types for integer values (int, long, byte, short)floating-point values (float and double)character values (char), and logical values (boolean).

On the other hand, reference types are objects that contain references to values and/or other objects, or to the special value null to denote the absence of value.

The String class is a good example of a reference type. An instance of the class, called an object, represents a sequence of characters, such as “Hello World”.

3. Declaring Variables in Java

To declare a variable in Java, we must specify its name (also called an identifier) and type. Let’s see a simple example:

int a;
int b;
double c;

In the above example, the variables will receive default initial values based on their declared types. Since we declared our variables to be int and double, they’ll have a default of 0 and 0.0, respectively.

Alternatively, we can use the assignment operator (=) to initialize variables during declaration:

int a = 10;

In the above example, we declare a variable with an identifier a to be of type int and assign a value of 10 to it using the assignment operator (=) and terminate the statement with a semi-colon (;). It’s compulsory, in Java, that all statements end with a semi-colon.

An identifier is a name of any length, consisting of letters, digits, underscore, and dollar sign, that conforms to the following rules:

  • starts with a letter, an underscore (_), or a dollar sign ($)
  • can’t be a reserved keyword
  • can’t be true, false, or null

Let’s expand our code snippet above to include a simple arithmetic operation:

int a = 10;
int b = 5;
double c = a + b;
System.out.println( a + " + " + b + " = " + c);

We can read the first three lines of the code snippet above as “assign the value of 10 to a, assign the value of 5 to b, sum the values of and b and assign the result to c”. In the last line, we output the result of the operation to the console:

10 + 5 = 15.0

Declaration and initialization of variables of other types follow the same syntax that we’ve shown above. For example, let’s declare String, char, and boolean variables:

String name = "Baeldung Blog";
char toggler = 'Y';
boolean isVerified = true;

For emphasis sake, the main difference in representing literal values of char and String is the number of quotes that surrounds the values. Therefore, ‘a’ is a char while “a” is a String.

4. Arrays

An array is a reference type that can store a collection of values of a specific type. The general syntax for declaring an array in Java is:

type[] identifier = new type[length];

The type can any primitive or reference type.

For example, let’s see how to declare an array that can hold a maximum of 100 integers:

int[] numbers = new int[100];

To refer to a specific element of an array, or to assign a value to an element, we use the variable name and its index:

numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;
int thirdElement = numbers[2];

In Java, array indexes start at zero. The first element of an array is at index 0, the second element is at index 1, and so on.

Additionally, we can get the length of the array by calling numbers.length:

int lengthOfNumbersArray = numbers.length;

5. Java Keywords

Keywords are reserved words that have special meaning in Java.

For example, public, static, class, main, new, instanceof, are keywords in Java, and as such, we can’t use them as identifiers (variable names).

6. Operators in Java

Now that we’ve seen the assignment operator (=) above, let’s explore some other types of operators in the Java language:

6.1. Arithmetic Operators

Java supports the following arithmetic operators that can be used for writing mathematical, computational logic:

  • + (plus or addition; also used for string concatenation)
  • – (minus or subtraction)
  • * (multiplication)
  • / (division)
  • % (modulus or remainder)

We’ve used the plus (+) operator in our previous code example to perform addition of two variables. The other arithmetic operators are used similarly.

Another use of plus (+) is for concatenation (joining) of strings to form a whole new string:

String output =  a + " + " + b + " = " + c;

6.2. Logical Operators

In addition to arithmetic operators, Java supports the following logical operators for evaluating boolean expressions:

  • && (AND)
  • || (OR)
  • ! (NOT)

Let’s consider the following code snippets that demonstrate the logical AND and OR operators. The first example shows a print statement that executes when the number variable is divisible both by 2 AND by 3:

int number = 6;
        
if (number % 2 == 0 && number % 3 == 0) {
    System.out.println(number + " is divisible by 2 AND 3");
}

While the second is executed when number is divisible by 2 OR by 5:

if (number % 2 == 0 || number % 5 == 0) {
    System.out.println(number + " is divisible by 2 OR 5");
}

6.3. Comparison Operators

When we need to compare the value of one variable to that of another, we can use Java’s comparison operators:

  • < (less than)
  • <= (less than or equal to)
  • > (greater than)
  • >= (greater than or equal to)
  • == (equal to)
  • != (NOT equal to)

For example, we can use a comparison operator to determine the eligibility of a voter:

public boolean canVote(int age) {
    if(age < 18) {
        return false;
    }
    return true;
}

7. Java Program Structure

Now that we’ve learned about data types, variables, and a few basic operators, let’s see how to put these elements together in a simple, executable program.

The basic unit of a Java program is a Class. A Class can have one or more fields (sometimes called properties)methods, and even other class members called inner classes.

For a Class to be executable, it must have a main method. The main method signifies the entry point of the program.

Let’s write a simple, executable Class to exercise one of the code snippets we considered earlier:

public class SimpleAddition {

    public static void main(String[] args) {
        int a = 10;
        int b = 5;
        double c = a + b;
        System.out.println( a + " + " + b + " = " + c);
    }
}

The name of the class is SimpleAddition, and inside of it, we have a main method that houses our logic. The segment of code between an opening and closing curly braces is called a code block.

The source code for a Java program is stored in a file with an extension of .java.

8. Compiling and Executing a Program

To execute our source code, we first need to compile it. This process will generate a binary file with the .class file extension. We can execute the binary file on any machine that has a Java Runtime Environment (JRE) installed.

Let’s save our source code from the above example into a file named SimpleAddition.java and run this command from the directory where we’ve saved the file:

javac SimpleAddition.java

To execute the program, we simply run:

java SimpleAddition

This will produce the same output to the console as shown above:

10 + 5 = 15.0

9. Conclusion

In this tutorial, we’ve looked at some of the basic syntax of Java. Just like any other programming language, it gets simpler with constant practice.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

Partner – Microsoft – NPI EA (cat = Spring Boot)
announcement - icon

Azure Container Apps is a fully managed serverless container service that enables you to build and deploy modern, cloud-native Java applications and microservices at scale. It offers a simplified developer experience while providing the flexibility and portability of containers.

Of course, Azure Container Apps has really solid support for our ecosystem, from a number of build options, managed Java components, native metrics, dynamic logger, and quite a bit more.

To learn more about Java features on Azure Container Apps, visit the documentation page.

You can also ask questions and leave feedback on the Azure Container Apps GitHub page.

Partner – Orkes – NPI EA (cat = Spring)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

Partner – Orkes – NPI EA (tag = Microservices)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Course – Spring Sale 2025 – NPI EA (cat= Baeldung)
announcement - icon

Yes, we're now running our Spring Sale. All Courses are 25% off until 26th May, 2025:

>> EXPLORE ACCESS NOW

Course – Spring Sale 2025 – NPI (All)
announcement - icon

Yes, we're now running our Spring Sale. All Courses are 25% off until 26th May, 2025:

>> EXPLORE ACCESS NOW

eBook Jackson – NPI EA – 3 (cat = Jackson)