Variables made easyFrom WikiJavabuy this book
the articleWe could write the following simple code: public class Interest1 { public static void main (String[] args) { System.out.print("Interest after a year is:"); System.out.println( 1234 * 0.025 ); } } but no one could spot the meaning of that "magic numbers" 1234 and 0.025. A common way to improve the comprehension of the source is using the variables: public class Interest2 { public static void main (String[] args) { double interest; double capital; double tax = 0.025; capital = 1234; interest = capital * tax; System.out.print("Interest after a year is:"); System.out.println(interest); } } 'interest', 'capital' and 'tax' are all variables of type 'double'. The Java syntax about variables: <type> <name>; // simple declaration <type> <name1>, <name2>, ...; // multiple declaration // declaration with initialization <type> <name> = <expression>; <type> <name1> = <exp1>, <name2> = <exp2>, ...; <type> could be a primitive type, a classe, an array type, ... A primitive type could be:
|
