With the ability of Java to concatenate strings using the plus (+) sign, a common mistake that new programmers may face is the mix up of adding integers and strings. Let me show you a sample.
Consider the following code:
public class StringConcatTest {
public static void main(String[] args){
int firstNum = 10;
int secNum = 5;
System.out.println(firstNum + secNum + " total number of apples.");
System.out.println("Total number of apples: " + firstNum + secNum);
}
}
OUTPUT:
15 total number of apples.
Total number of apples: 105
Clearly, the second sentence is incorrect.
Thursday, 10 January 2013
Tuesday, 1 January 2013
Java: Handling Braces
It is good programming practice to maintain a format when coding. These formats can be anything from indentation, white spaces, line wrapping, separation of code and braces handling. Why is it important? - To make the code readable.
There are two distinct way of handling braces:
1. K&R style
while(a!=b){
doMethod();
}
2. Allman Style
while(a!=b)
{
doMethod();
}
Personally I prefer the K&R and I practice it on my code. Always remember, good coding practice makes a good programmer. :)
Subscribe to:
Posts (Atom)