vendredi 27 juillet 2018

An introduction to Java syntax for Android development

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

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.

camelCase

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

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

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!

Learn How To Develop Your Own Android App

Get Certified in Android App Development!  No Coding Experience Required.

Android Authority is proud to present the DGiT Academy: the most detailed and comprehensive course covering every aspect of Android app development, run by our own Gary Sims. Whether you are an absolute beginner with zero coding knowledge or a veteran programmer, this course will guide you through the process of building beautiful, functional Android apps and bring you up to speed on the latest features of Android and Android Studio.

The package includes over 6 hours of high quality videos and over 60 different lessons. Reams of in-depth glossaries and resources, highly detailed written tutorials and exclusive access to our private slack group where you can get help  directly from Gary and our other elite developers.

AA readers get an additional 60% off today. That's a savings of over $150. Claim your discount now using exclusive promo code: SIXTYOFF. This is your ticket to a lucrative future in Android App Development. What are you wating for?
Enroll Today


from Android Authority https://ift.tt/2LHLMnd
via IFTTT

Aucun commentaire:

Enregistrer un commentaire