Learning programming and understanding Java Syntax

Coding Android apps means learning to use Java (unless you choose to use Kotlin, Unity, B4A, Xamarin, or another “alternative” method). Fortunately, there are plenty of tutorials available to walk you through common statements and what they all do. However, before you start decoding Java, it’s important to get a grasp of Java syntax.

Java needs to be written in a certain way — it includes a fair amount of “decoration” and some strange punctuation. This can be a little confusing for newbies, but when you figure out the basics you’ll be able to read and write new Java commands much more easily — everything will make much more sense. Let’s look at the grammar of Java and why certain things are laid out the way they are. Bearing this in mind can prevent mistakes further down the line, even if it seems a little much at first!

Java syntax basics

For now we’re not necessarily interested in how Java works as such, so much as its basic rules for writing a line of code.

Java code is case sensitive. Later on, you’ll learn how to name your own variables. You need to ensure consistent capitalization use — otherwise you’ll run into an error.

The most important rule of Java syntax: lines end with a semi colon. This punctuation tells Java (and you) the line of code is over and isn’t intended to run on to the next line, kind of like a full stop. Some languages (like Kotlin) make this optional, but leaving off the “;” in Java will get a red underline — the code won’t run!

Java syntax main activity code

The exception to this rule is when you open up new code blocks. If you end a line with an open curly bracket ({) you are grouping the following lines of code in some way. These will follow on from the preceding line, until the closing }.

Lines ending with curly brackets don’t need to be followed by semi colons, but the code inside them does and is formatted as normal. You might use this when writing methods — chunks of code you can call upon at any point during your program — and “conditional statements,” which only execute if certain conditions are met.

Code blocks should also be indented. You can have blocks within blocks within blocks (!), and indentations will quickly show us the logical grouping of our code at a glance. Some other programming languages (like Python) require these indents to run code at all, doing away with the curly brackets altogether.

Another exception (sorry) is a comment, which begin with two forward strokes and allows you to write messages for your future self or a colleague, explaining the purpose of the code segment

If some of that went over your head, don’t worry. Just keep it in mind when reading and writing future code. It can look pretty alien at first, but everything is there for a reason!

While it can look pretty alien at first, everything is there for a reason

For now, remember every line must end with a semi-colon, unless it ends with a curly bracket. Code blocks contained within curly brackets are indented, and comments are preceded by two forward slashes.


One more thing to keep in mind is the naming convention for your variables and methods. When you create a value to represent something like “Player Health,” you need to give it a name. Variable names can’t include spaces, which means you risk having run-on phrases like “playerhealth” or longer. If you had a whole page of text like that, it would get pretty hard to decipher pretty quickly! Legible code is always the best code.

Android syntax camelcase explained

Instead, we use a convention known as “camel case” where each new word begins with a capital letter in order to make it easier to understand. So “Player Health” becomes either “playerHealth'” or “PlayerHealth.” The former (playerHealth) is “lower camelCase'” and the latter (PlayerHealth) is “upper camelCase.”

How you go about using this is up to you (in a way, creating methods and variables lets you define your own Java syntax), but there are some best practices worth looking at. It’s good to follow guidelines like those, as it will keep you from getting confused between Java statements and your own variables, classes, and methods (that will all make sense later).

It’s very important to name things in a logical manner so everything’s function is easy to understand at a glance. Avoid using acronyms or random words — they’ll only make your code harder to understand if you take a break from it. If it’s ever unclear what something does, write a comment to explain it! Don’t trust your memory to be there for you months later.

Java programming syntax development

Even the main Java file in our program, MainActivity, is written in camelCase! You can’t have spaces in file names either. Meanwhile, activity_main.xml uses a different naming convention, as files in the resources folder aren’t allowed to have capitalization (I don’t make the rules!). It’s the same basic idea though.

All this might seem a little arbitrary, but the goal of Java syntax is to make code as readable as possible while avoiding preventable errors. Very smart people tested and experimented for decades before settling on this, so it’s probably pretty good!

Get into good habits now and you’ll save yourself countless hours down the line, trust me!

Save 60% on our Introduction to Android Development Course

Android Authority has created a course to help you learn how to develop Android Apps! No Coding Experience Required. Start Your Journey on Becoming an Android Developer today.
Visit DGiT Academy today. Enter discount code: SIXTYOFF at checkout to receive 60% off.


Read comments