1 / 22

Statements, Comments & Simple Arithmetic

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

gautam
Download Presentation

Statements, Comments & Simple Arithmetic

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 4 Statements, Comments & Simple Arithmetic

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

  3. Overview Hello World! Reserved words & Identifiers Code Blocks Methods Statements Style & Comments Simple Arithmetic & Operators

  4. 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!

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

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

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

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

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

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

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

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

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

  14. 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 } }

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

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

  17. 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;

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

  19. 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;

  20. 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) {

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

  22. Eclipse Lets look at HelloWorld again!

More Related