Anatomy of A Java Program (1) : School of Computer Science & Information Technology

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

School of Computer Science &

Information Technology
Anatomy of a Java Program (1)

class HiThere
G6DICP - Lecture 3 {
public static void main (String[] args)
{
System.out.println("Hello World!");
Statements, comments, & simple arithmetic }
}

Anatomy of a Java Program (2) Anatomy of a Java Program (3)

class HiThere class HiThere


{
 Reserved words
}
 class is a Java "reserved word".
 Identifier
 HiThere is an identifier.  Code Blocks
 This is a name we make up to identify a component of  Braces ( ie { } ) delimit an isolated block of code.
the program (in this case the program itself).  All programs have several (usually many) blocks.
 Identifiers must be a single word.  Braces must be balanced.
 Java is case sensitive.  Braces are often nested.

3 4

Anatomy of a Java Program (4) Anatomy of a Java Program (5)


class HiThere
class HiThere {
{ public static void main (String[] args)
public static void main (String[] args) {
{ System.out.println("Hello World!");
}
}
}
}  Statements
 The program contains a single statement (statements are terminated by a
semi-colon).
 Methods
 println
 Methods contain blocks of functional code.
 This statement calls a library method called System.out.println.
 Methods are named by an identifier.  Methods may be provided with an argument (data), which is contained in brackets.
 This is a method called main - applications execute their main
 The argument of println is a string
method on starting.
 A string is a sequence of characters.
 NB the syntax of main must be exactly as shown.  Java strings are delimited by double quotes.
5 6
Anatomy of a Java Program (6) Arithmetic
/*
A Simple Java Program
*/
class HiThere  Arithmetic is accomplished by "numeric operators".
{
public static void main (String[] args)
// This is the main method
{ +
System.out.println("Hello World!"); -
} /
}
*
 Style
 Line breaks and indents are ignored by the compiler, System.out.print("The answer is ");
but help greatly with readability. System.out.println( 2 + 2 );
 Comments
 Comments are ignored by the compiler.
 Comments may be delimited by /* */
7 8
 Comments may be delimited by // to the end of the line.

Numeric Data Types


 Integer
 Whole numbers (eg 1, 2, 9, 923)
 Floating Point
 Decimals (eg 1.0, 3.14, 99.238)
 NB Scientific Notation
 The letter 'E' means
Times 10 raised to the power ...
 Examples:
1E+01 = 10 (ie 1*101)
4.6E+03 = 4600 (ie 4.6*103)
4.6E-03 = 0.0046 (ie 6*10-3)
 Integer Division
 Operator / integer or floating point division
 Operator % modulus
9

You might also like