Download presentation
Presentation is loading. Please wait.
Published byBlaze Weaver Modified over 9 years ago
1
1 2. Program Construction in Java
2
01 Java basics
3
3 Variables A memory location with a name where the computer stores data. Think of them as named pigeon holes. Only one datum (data item) is held in that location at a time, but the contents can be changed by the user or the program.
4
4 Variables All variables must be declared before being used i.e named and their type stated. Variable names start with a lower case letter (never a digit) and have no spaces. Use camelCase to distinguish words e.g. totalScore, averageOf3Throws. ‣ other examples are NaCl, McInally and iPod.
5
5 Primitives The most basic variable types are called primitives: ‣ byte - small integers from -128 to +127, ‣ int - medium integers in the range of about + or -2 000 000 000, ‣ long - large integers in the range of about +/- 9 million million million,
6
6 Primitives ‣ double - real numbers (i.e. with fractional parts) from +/- 2 x 10 +/-10 000, e.g. 3.142 or - 0.00047, ‣ char - a single character e.g. 'A', '$' or '9', ‣ boolean - true or false. Two others ( short and float ) exist but will not be used in the exam.
7
7 Declaring variables A variable gets declared when first references along with its data type e.g. ‣ int count; ‣ char initial; ‣ double salesTaxRate; Note that every statement in Java ends with a semi-colon (;). A variable’s name is called its identifier.
8
8 Assignment The equals sign (=) in Java is used to change or initialise variables: ‣ char initial = ‘t’; ‣ int count; count = 4; ‣ boolean smoker = false; smoker = true;
9
9 Casting This is not allowed: ‣ char initial = 47.58; However, this is allowed: ‣ int p = (int) Math.round(3.142); even though Math.round() is a function that gives a long type of answer.
10
10 Arithmetic operators Addition (operator is +) Subtraction ( - ) Multiplication ( * ) Division ( / ) Modulus ( % ) [This means remainder e.g. 15%6 would be 3, since 15/6 is 2 remainder 3.]
11
11 Arithmetic operations Operations on ints give int results: ‣ int a = 12; int b = 6; int c = a / b; puts the value 2 into c Note that this is allowed ‣ int count = 4; count = count + 1;
12
12 Arithmetic operations A byte divided by a byte gives a byte, etc; When faced with an inexact integer division, Java rounds the result down e.g. dividing 10 by 3 produces 3. What would happen here? ‣ int count = 0; int answer = 758/count;
13
13 Arithmetic promotion When doing arithmetic on unlike types, Java widens the types involved so as to avoid losing information: ‣ double *( long, int or byte ) gives a double, ‣ long *( int or byte ) gives a long, ‣ int * byte gives an int.
14
14 Precedence %, * and / are done first from left to right. + and – are done later. Round brackets (around any section) means do it first whatever.
15
15 Functions Some of many examples: ‣ long y = Math.round(x); ‣ double y = Math.sin(x); ‣ double y = Math.abs(x); ‣ double z = Math.pow(x,y);
16
16 Spaces and cases Java is case sensitive so a variable called taxRate is not the same as taxrate. White space is not significant except inside “quotation marks”.
17
17 Comment Comments are ignored by the compiler Everything between /* and */ and on a single line after //. Use the /* */ style comments to comment out multiple lines for debugging (spotting problems in your code) purposes.
18
18 Blocks and indentation Parts of a program are divided into blocks, surrounded by {braces}. There must be an equal number of opening and closing braces in your programs. The other types of bracket, (...) and [...] are also used but for different purposes, and the same rule applies.
19
19 Blocks and indentation All lines apart from comments and those with braces must end with a semi-colon (;). Always start the contents of a new block with a tab (indent its contents) - otherwise it will be impossible to spot errors later.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.