Upload
erling
View
40
Download
0
Tags:
Embed Size (px)
DESCRIPTION
G6DICP - Lecture 3. Statements, comments, & simple arithmetic. Anatomy of a Java Program (1). class HiThere { public static void main (String[] args) { System.out.println("Hello World!"); } }. Anatomy of a Java Program (2). Reserved words - PowerPoint PPT Presentation
Citation preview
School of Computer Science & Information Technology
G6DICP - Lecture 3G6DICP - Lecture 3
Statements, comments, & simple arithmeticStatements, comments, & simple arithmetic
2
Anatomy of a Java Program (1)
class HiThere { public static void main (String[] args) { System.out.println("Hello World!"); } }
3
Anatomy of a Java Program (2)
class HiThere
Reserved wordsReserved words classclass is a Java "reserved word". is a Java "reserved word".
IdentifierIdentifier HiThereHiThere is an identifier. is an identifier. This is a name we make up to identify a component ofThis is a name we make up to identify a component of
the program (in this case the program itself). the program (in this case the program itself). Identifiers must be a single word.Identifiers must be a single word.
Java is case sensitive.Java is case sensitive.
4
Anatomy of a Java Program (3)
class HiThere { }
Code BlocksCode Blocks Braces Braces ( ie { } ) delimit an isolated block of code.( ie { } ) delimit an isolated block of code. All programs have several (usually many) blocks.All programs have several (usually many) blocks. Braces Braces mustmust be balanced. be balanced. Braces are often nested.Braces are often nested.
5
Anatomy of a Java Program (4)
class HiThere { public static void main (String[] args) {
} }
MethodsMethods Methods contain blocks of functional code.Methods contain blocks of functional code. Methods are named by an identifier.Methods are named by an identifier. This is a method called This is a method called mainmain - applications execute their main - applications execute their main
method on starting.method on starting. NB the syntax of main NB the syntax of main mustmust be exactly as shown. be exactly as shown.
6
Anatomy of a Java Program (5)class HiThere { public static void main (String[] args) { System.out.println("Hello World!"); }}
StatementsStatements The program contains a single statement (statements are terminated by a semi-colon).The program contains a single statement (statements are terminated by a semi-colon).
printlnprintln This statement calls a library method called System.out.println.This statement calls a library method called System.out.println. Methods may be provided with an argument (data), which is contained in brackets.Methods may be provided with an argument (data), which is contained in brackets.
The argument of println is a The argument of println is a stringstring A string is a sequence of characters.A string is a sequence of characters. Java strings are delimited by double quotes.Java strings are delimited by double quotes.
7
Anatomy of a Java Program (6)/* A Simple Java Program*/class HiThere { public static void main (String[] args) // This is the main method { System.out.println("Hello World!"); }}
StyleStyle Line breaks and indents are ignored by the compiler,Line breaks and indents are ignored by the compiler,
but help greatly with readability. but help greatly with readability. CommentsComments
Comments are ignored by the compiler.Comments are ignored by the compiler. Comments may be delimited by /* */Comments may be delimited by /* */ Comments may be delimited by // to the end of the line.Comments may be delimited by // to the end of the line.
8
Arithmetic
Arithmetic is accomplished by "numeric operators".Arithmetic is accomplished by "numeric operators".
+-/*
System.out.print("The answer is ");System.out.println( 2 + 2 );
9
Numeric Data Types IntegerInteger
Whole numbers (eg 1, 2, 9, 923)Whole numbers (eg 1, 2, 9, 923) Floating PointFloating Point
Decimals (eg 1.0, 3.14, 99.238)Decimals (eg 1.0, 3.14, 99.238) NB Scientific NotationNB Scientific Notation
The letter 'E' means The letter 'E' means Times 10 raised to the power ...Times 10 raised to the power ...
Examples:Examples:1E+01 = 10 (ie 1*101)1E+01 = 10 (ie 1*101)4.6E+03 = 4600 (ie 4.6*103)4.6E+03 = 4600 (ie 4.6*103)4.6E-03 = 0.0046 (ie 6*10-3)4.6E-03 = 0.0046 (ie 6*10-3)
Integer DivisionInteger Division Operator / integer Operator / integer or or floating point division floating point division Operator % modulusOperator % modulus