Punctuating the Java Code in Your Android App - dummies

By Barry A. Burd

In English, punctuation is vital. Just ask the copy editor of a book or a high school English teacher. It is equally important when coding your Android app. Anyway, punctuation is also important in a Java program. This list lays out a few of Java’s punctuation rules:

  • Enclose a class body in a pair of curly braces.

For example, the MainActivity class’s body is enclosed in curly braces.

public class MainActivity extends AppCompatActivity {

TextView textView;

...

void shout() {

textView.append("!!!!!!!");

}

}

The placement of a curly brace (at the end of a line, at the start of a line, or on a line of its own) is unimportant. The only important aspect of placement is consistency. The consistent placement of curly braces throughout the code makes the code easier for you to understand. And when you understand your own code, you write far better code.

When you compose a program, Android Studio can automatically rearrange the code so that the placement of curly braces (and other program elements) is consistent. To make it happen, click the mouse anywhere inside the editor and choose Code →   Reformat Code.

  • Enclose a method body in a pair of curly braces.

Here, the onCreate method’s body is enclosed in curly braces, and the bodies of the two shout methods are enclosed in curly braces.

public class MainActivity extends AppCompatActivity {

TextView textView;

@Override

protected void onCreate(Bundle savedInstanceState) {

}

void shout(String message) {

}

void shout() {

}

}

  • A Java statement ends with a semicolon.

Notice the semicolons in this excerpt:

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

textView = (TextView) findViewById(R.id.textView);

textView.setText("");

  • A package declaration ends with a semicolon. An import declaration also ends with a semicolon.

Here, each of the first four lines ends with a semicolon.

package com.allyourcode.methoddemo;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.TextView;

  • In spite of the previous two rules, don’t place a semicolon immediately after a closing curly brace (}).
  • Use parentheses to enclose a method’s parameters, and use commas to separate the parameters.

Here are some examples:

shout("Help");

shout("I'm trapped inside a smartphone");

shout("Put down the phone and start living life", "*");

shout();

Use double quotation marks (“”) to denote strings of characters.

The previous bullet contains four strings — namely, “Help”, “I’m trapped inside a smartphone”, “Put down the phone and start living life”, and, finally, “*”.

  • Use dots to separate the parts of a qualified name.

The fully qualified name of a class might be something like com.allyourcode.a04_03.MainActivity.

The setText and append methods belong to a component named textView. So, you write textView.setText and textView.append.

  • Use dots within a package name.

The most blatant consequence of a package name’s dots is to determine a file’s location on the hard drive. Fortunately, Android Studio creates all these folders for you and puts the code in the right place. You don’t have to worry about a thing.

java-programming-for-android-developers-2e-java-folders
The folders containing a Java program.