These quick tips discuss some of the most common Java shorthand techniques you’ll come across when you’re getting started in Android development.
You’ll find these little code tidbits—which we are calling Java shorthand—used in the Android SDK sample code, and just about every Android development book published at this time, not to mention online tutorials and developer forums. None of these tips are Android-specific; they are simply Java techniques that routinely confound beginners new to Java and Android, based upon the emails we receive from our readers.
In other words,
Instead, Java developers frequently just use the statement to be evaluated as the “resulting” value itself. This is seen often when it comes to return statements, but you’ll also see it in other places as well. For example, the following verbose method uses a "temp" variable called sum to store the sum of two integers and then returns this value:
Here’s an example of a ternary operator in use:
You’ll find these little code tidbits—which we are calling Java shorthand—used in the Android SDK sample code, and just about every Android development book published at this time, not to mention online tutorials and developer forums. None of these tips are Android-specific; they are simply Java techniques that routinely confound beginners new to Java and Android, based upon the emails we receive from our readers.
Tip #1:Java’s Unary Operators (Increment/Decrement Variable Shorthand)
Many developers like their code short and easy to read. Like some other programming languages, Java includes unary operators for easily incrementing and decrementing variable values by 1.In other words,
int counter = 1;This code is equivalent to:
counter++;
counter--;
int counter = 1;These unary operators can appear before (prefix) or after (postfix) the variable. The location of the operator dictates whether the increment/decrement operation happens before or after the rest of the expression is evaluated. For example, the following code shows how unary operators work by manipulating a variable called counter using Android logging:
counter = counter + 1;
counter = counter – 1;
int counter = 0;
Log.i(DEBUG_TAG, "The counter value is ="+counter++); // prints 0
Log.i(DEBUG_TAG, "The counter value is ="+counter); // prints 1
Log.i(DEBUG_TAG, "The counter value is ="+counter--); // prints 1
Log.i(DEBUG_TAG, "The counter value is ="+counter); // prints 0
Log.i(DEBUG_TAG, "The counter value is ="+(++counter)); // prints 1
Log.i(DEBUG_TAG, "The counter value is ="+--counter); // prints 0
Tip #2:Skipping Temporary Variables (Unnecessary Variables Shorthand)
Java developers generally avoid creating variables they don’t really need. This is especially true of temporary, ortemp ,variables that are used once to store the result of a statement, only to be abandoned.Instead, Java developers frequently just use the statement to be evaluated as the “resulting” value itself. This is seen often when it comes to return statements, but you’ll also see it in other places as well. For example, the following verbose method uses a "temp" variable called sum to store the sum of two integers and then returns this value:
int sumVerbose(int a, int b)Many Java developers would simply skip the overhead and hassle of creating the temp variable, and just evaluate the statement as part of the return statement, like this:
{
int temp = a + b;
return temp;
}
int sum(int a, int b)This style holds true for cases where the temp variable is only used once. If the method included further operations on that value, it is usually prudent to use a well-named variable for code readability. In addition, you'll often see more “verbose” coding style in code that has a lot of debugging features.
{
return (a+b);
}
Tip #3: The Java "this" Keyword and Chaining Methods
You will often see Java methods chained together. Frequently, these methods are called on the instance of the current class (thus, the this keyword). Similar to the tip discussed above, the return values of each method are only being used to access an underlying method. Therefore, the return value is not stored in a container value, instead the underlying method is just called. For example:InputStream is = getResources().openRawResource(R.drawable.icon);This code is logically equivalent to the following:
Resources myAppResources = this.getResources();
InputStream is = myAppResources.openRawResource(R.drawable.icon);
Tip #4: Java’s Ternary Operators (If-Else Shorthand)
One conditional statement you will likely see will use Java’s ternary operator support. This is a shorthand way of designing a simple If-Else statement using a conditional statement (which may or may not be encapsulated in parentheses), followed by a question mark (?), then a statement to occur if the conditional is true, then a colon (:) and another statement to occur if the conditional is false.Here’s an example of a ternary operator in use:
int lowNum = 1;This is the logical equivalent of the following, much longer, code snippet:
int highNum = 99;
int largerNum = lowNum < highNum ? highNum : lowNum;
int largerNum;This sort of Java shorthand is really only appropriate when your If-Else statement is simple. You’ll sometimes see developers cram a lot of logic into one of these statements; we do not recommend this. Only use ternary operators when they make your code easier to read, not harder.
if(lowNum < highNum)
{
largerNum = highNum;
} else {
largerNum = lowNum;
}
Tip #5: Empty Statements (Infinite Loop Shorthand)
In Java, you can have empty statements simply by terminating a blank line of code with its semicolon. This trick is often used to specify for() loop conditionals to create an infinite loop, like this:for (;;) {Each of the for() loop components is an empty statement. This evaluates to be true and therefore the loop continues indefinitely. As with any code design, make sure any infinite loops you create have reasonable exit cases.
//Do something over, and over, and over again.
}
No comments:
Post a Comment