Presentation is loading. Please wait.

Presentation is loading. Please wait.

Statements, Comments & Simple Arithmetic

Similar presentations


Presentation on theme: "Statements, Comments & Simple Arithmetic"— Presentation transcript:

1 Statements, Comments & Simple Arithmetic
4 Statements, Comments & Simple Arithmetic

2 Previously What’s Java? Some history Characteristics Components
Type of programs Development JVM OS Application Storage Library (y) Web Server Storage Browser Library (y) OS Servlet Applet applet Interpreted language, … JSE, JEE and JME JDK JVM, OS Type: Applications, Applets, Servlets and Libraries Development environment Eclipse

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

4 Finally - a Java Program!
class HiThere { /** * This is the entry point for an application. 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

5 Reserved words & Identifiers
class HiThere { public static void main(String[] astrArgs) { System.out.println("Hello World!"); } // main() } // end class HiThere 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 We represent reserved words in red text. Methods with the static keyword belong to the class when the keyword is not present then the method belong to the object (object is an instantiation of a class, holds the data with its values)

6 Reserved words & Identifiers
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
The name of a classes always start in uppercase class HiThere The name of methods always start in lowercase public static void main(String[] astrArgs) The name of variables always start in lowercase The name of static final variables (‘constants’) always in uppercase private static final int SEC_PER_MINUTE = 60;

8 Reserved words & 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 Control flows use also braces to delimit the block

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 The argument of println is a string
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 indents Line breaks and indents are ignored by the compiler, but help greatly with readability Use meaningful names

14 Comments class HiThere { public static void main(String[] astrArgs) {
/** * This is the entry point for an application. 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 Arithmetic is accomplished by "numeric operators“
Binary Operators + addition - subtraction * multiplication / division % remainder int iNum = 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

17 Arithmetic 9 5 14 3 1 Arithmetic Unary Operators += addition
-= subtract *= multiply /= divide %= remainder iNum += 2; iNum -= 2; iNum *= 2; iNum /= 2; iNum %= 2; int iNum = 7; 9 5 14 3 1

18 Arithmetic System.out.print("The answer is "); System.out.println(2 + 2); class Arithmetic { /** * Adds two to two and displays the result. astrArgs 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) { int iNum = 7;

20 Operators Assignment Operator Conditional Operators
&& conditional-AND || conditional-OR Type Comparison Operator instanceof compares an object to a specified type int iNum = 7; int iCount = 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 ? : ternary (shorthand for if-then-else statement) <condition> ? <statement if true> : <statement if false> if (iNum == 7 || iCounter == 0) {

21 Documentation Statement and Blocks Operators
Operators Sun Certified Programmer for Java 5 - Study Guide

22 Lets look at HelloWorld again!
Eclipse Lets look at HelloWorld again!


Download ppt "Statements, Comments & Simple Arithmetic"

Similar presentations


Ads by Google