220 likes | 362 Views
4. Statements, Comments & Simple Arithmetic. Previously. What’s Java? Some history Characteristics Components Type of programs Development. Application. Library (y). Library (y). Library (y). JVM. OS. Applet. Servlet. Storage. Storage. Library (y). Library (y). Library (y).
E N D
4 Statements, Comments & Simple Arithmetic
Previously • What’s Java? • Some history • Characteristics • Components • Type of programs • Development Application Library (y) Library (y) Library (y) JVM OS Applet Servlet Storage Storage Library (y) Library (y) Library (y) Web Server Browser applet OS
Overview Hello World! Reserved words & Identifiers Code Blocks Methods Statements Style & Comments Simple Arithmetic & Operators
classHiThere { /** * This is the entry point for an application. * @param astrArgs the command line arguments. */ publicstaticvoidmain(String[] astrArgs) { // Starting of the main method System.out.println("Hello World!"); }// main() }// end class HiThere Finally - a Java Program!
Reserved words & Identifiers Reserved words or keywords are part of the language class used to define a class public access keyword - public, private, protected and ‘default’ static type related to belonging void special return type Java is case sensitive class HiThere { public static void main(String[] astrArgs) { System.out.println("Hello World!"); }// main() }// end class HiThere
Reserved words & Identifiers • Identifier • HiThere is an identifier • This is a name we make up to identify a component of the program (in this case a class) • Must be a single word (no blank characters) • Must start with a letter, ‘_’, ‘£’ or ‘$’ • Following characters can also be numbers • Cannot contain any white space
Reserved words & Identifiers • Identifiers • The name of a classes always start in uppercase classHiThere • The name of methods always start in lowercase public static void main(String[] astrArgs) • The name of variables always start in lowercase public static void main(String[] astrArgs) • The name of static final variables (‘constants’) always in uppercase privatestaticfinalintSEC_PER_MINUTE=60;
Reserved words & Identifiers • Identifiers • A format to build identifiers • Camel case – no word separator space, underscore • Variable names prefix with their type (lowercase) public static void main(String[] astrArgs) array array of Strings array of Strings meaning word
Code Blocks class HiThere { public static void main(String[] astrArgs) { System.out.println("Hello World!"); }// main() }// end class HiThere • Braces ( i.e. { } ) delimit an isolated block of code • All programs have several (usually many) blocks • Braces must be balanced • Braces are often nested
Methods class HiThere { public static void main(String[] astrArgs) { System.out.println("Hello World!"); }// main() }// end class HiThere • Methods contain blocks of functional code • Methods are named by an identifier • This is a method called main - applications execute their main method on starting (application entry point) • NB the syntax of main must be exactly as shown
Statements class HiThere { public static void main(String[] astrArgs) { System.out.println("Hello World!"); }// main() }// end class HiThere • The program contains a single statement • Statements are terminated by a semi-colon • println • This statement calls a library method called System.out.println • Methods may be provided with an argument (data), which is contained in brackets
Statements class HiThere { public static void main(String[] astrArgs) { System.out.println("Hello World!"); }// main() }// end class HiThere • The argument of println is a string • A string is a sequence of characters • Java strings are delimited by double quotes
Style class HiThere { public static void main(String[] astrArgs) { System.out.println("Hello World!"); }// main() }// end class HiThere • Line breaks and indents are ignored by the compiler, but help greatly with readability • Use meaningful names indents
Comments class HiThere { /** * This is the entry point for an application. * @param astrArgs the command line arguments. */ public static void main(String[] astrArgs) { // Starting of the main method System.out.println("Hello World!"); }// main() }// end class HiThere } }
Comments • Comments are ignored by the compiler • Comments may be delimited by /* */ • For several lines of comment • A type of multiline comment is the documentation comments which start with /** and end with */ • Comments may be delimited by // to the end of the line • Single line comment
Arithmetic is accomplished by "numeric operators“ Binary Operators + addition - subtraction * multiplication / division %remainder Arithmetic intiNum = 0; iNum = 7+ 2; iNum = 7 – 2; iNum = 7 * 2; iNum = 7 / 2; iNum = 7 % 2; 9 5 14 3 1 3 1 • 2 • 3 • 1 3 1
Arithmetic • Arithmetic Unary Operators += addition -= subtract *= multiply /= divide %= remainder iNum+= 2; • iNum-= 2; • iNum*= 2; • iNum/= 2; • iNum%= 2; 9 5 14 3 1 intiNum = 7;
Arithmetic System.out.print("The answer is "); System.out.println(2 + 2); class Arithmetic { /** * Adds two to two and displays the result. * @paramastrArgs the command line arguments, not used. */ public static void main(String[] astrArgs) { System.out.print("The answer is "); System.out.println(2 + 2); }// main() }// end class Arithmetic The answer is 4
Operators • Equality and Relational Operators == equal to if(iNum == 1) { != not equal to if (iNum != 1) { > greater than if (iNum > 1) { >= greater than or equal to if (iNum >= 1) { < less than if (iNum < 1) { <= less than or equal if (iNum == 1) { intiNum = 7;
Operators • Assignment Operator = assignment • Conditional Operators && conditional-AND || conditional-OR • Type Comparison Operator instanceof compares an object to a specified type intiNum= 7; intiCount= 0; if (iNum == 7 && iCounter == 0) { true&&true true true&&false false false&&true false false&&false false true||true true true||false false false||true true false||false false if (iNum == 7 || iCounter == 0) {
Documentation • Statement and Blocks http://download.oracle.com/javase/tutorial/java/nutsandbolts/expressions.html • Operators http://download.oracle.com/javase/tutorial/java/nutsandbolts/operators.html Sun Certified Programmer for Java 5 - Study Guide
Eclipse Lets look at HelloWorld again!