Java Programming for Android Developers For Dummies
Book image
Explore Book Buy On Amazon
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) <strong>{</strong>

<strong>}</strong>

void shout(String message) <strong>{</strong>

<strong>}</strong>

void shout() <strong>{</strong>

<strong>}</strong>

}

  • A Java statement ends with a semicolon.

Notice the semicolons in this excerpt:

super.onCreate(savedInstanceState)<strong>;</strong>

setContentView(R.layout.activity_main)<strong>;</strong>

textView = (TextView) findViewById(R.id.textView)<strong>;</strong>

textView.setText("")<strong>;</strong>

  • 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<strong>;</strong>

import android.support.v7.app.AppCompatActivity<strong>;</strong>

import android.os.Bundle<strong>;</strong>

import android.widget.TextView<strong>;</strong>

  • 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.

About This Article

This article is from the book:

About the book author:

Barry Burd, PhD, is a professor in the Department of Mathematics and Computer Science at Drew University in Madison, New Jersey. He has lectured at conferences in the United States, Europe, Australia, and Asia. He hosts podcasts and videos about software and other technology topics. He is the author of many articles and books, including Java For Dummies.

This article can be found in the category: