Professional Documents
Culture Documents
Development
CT010-3-1
Understanding Programming
Language Constructs
Prepared by: SZK First Prepared on:30 th July 2005 Last Modified on:14th December 2005
Quality checked by: GTK
Copyright 2005 Asia Pacific University College of Technology and Innovation
Learning Outcomes
At the end of this topic, you should be able to:
Define and differentiate the various data types in Java
Create , edit, compile and run simple procedural based
Java programs using variables and data types
Use operators in Java programs and subsequently
write expressions that make up a Java program
Plan and implement program testing strategies
Debug programs using appropriate debugging tools
Key Terms
If you have mastered this topic, you should be able to use
the following terms correctly in your assignments and
exams:
identifiers
primitive data types
derived data types
program testing strategies
debugging
Identifiers/Variables
Variables
int temperature; // The Fahrenheit
temperature
Think of variable like a container for a value :
32
temperature
temperature = 32;
Identifiers/Variables
To declare > 1 variable :
int fahrTemp, centTemp;
int is the type name
Legal variable name must consists of a letter (upper- or
lowercase) followed by any number (including zero) of letters
& digits.
Illegal variable names : 4.7 !%-Legal variable names :
temperature TEMP23 T $temp_1 T$$1
CT010-3-1 Introduction to Software Development
Identifiers/Variables
int Temp;
temp=3;
cause Java to give the error
Undefined variable; temp
To declare a constant value :
final double PI = 3.14159;
Scope of Variables
A variable's scope is the block of code within which the
variable is accessible and determines when the variable is
created and destroyed. The location of the variable
declaration within your program establishes its scope and
places it into one of these 4 categories:
Member variable
Local variable
Method parameter
Exception-handler
parameter
Keywords
Keywords ~ words that may seems to be legal variable
names but they are not because they are reserved by
the language for special uses.
List of Keywords in Java :
abstract
catch
do
finally
if
interface
outer
return
this
var
boolean
char
double
float
implements
long
package
short
throw
void
break
class
else
for
import
native
private
static
throws
volatile
byte
const
extends
future
inner
new
protected
sure
transient
while
case
continue
false
generic
instanceof
null
public
switch
true
cast
default
final
goto
int
operator
rest
synchronized
try
Data types
Data types
Java language is rich in its data types.
The variety of data types available allow the
programmer to select the type appropriate to
the needs of the application.
Two major categories of data type :
Primitive (Built in types)
Reference / Object (Derived Types)
Data types
Data types in Java
Primitive
Numeric
Derived
Non-numeric
Classes
Arrays
Interface
Integer
Floating point
Character
Boolean
Slide 12 (of 34)
Size/Format
Description
byte
Byte-length integer
short
Short integer
int
Integer
long
Long integer
float
double
char
A single character
boolean
true or false
A boolean value
integers
Real numbers
others
<=>
2.829281e8
2. 13898121 x 10-15
<=>
2. 13898121e-15
The Debugger
Debugger = program to run your program,
interrupt it, and inspect variables
Three key commands:
Set Breakpoint
Single Step
Inspect Variable
Debugging
IDE Debugger tool helps programmers debug their
programs:
control program execution: step through the code line-byline, or run to a given point
set breakpoints in the program
monitor data values with watches and inspectors, etc.
Debugging a Program
Debugging is the major activity performed
by programmers during the testing phase
Testing determines if there is an error,
debugging determines the cause of it
Debugging is like detective work
Inspect carefully the information displayed by
your program
Insert additional diagnostic output statements
in the method to determine more information
CT010-3-1 Introduction to Software Development
Using a Debugger
Debuggers often are included with IDEs
A debugger can execute your program
incrementally rather than all at once
Single-step execution executes in increments as
small as one program statement
Breakpoints are used to traverse large portions
of code before stopping
The actual mechanics of using a debugger
depend on the IDE that you are using
CT010-3-1 Introduction to Software Development
Program Trace
Output statements in your program for diagnostic purposes
eg. if (status == SINGLE)
{
System.out.println("status is SINGLE");
...
}
...
Stack trace tells you the contents of the call stack
eg. Throwable t = new Throwable();
t.printStackTrace(System.out);
Looks like exception report:
java.lang.Throwable
at TaxReturn.getTax(TaxReturn.java:26)
at TaxReturnTest.main(TaxReturnTest.java:30)
Drawback of trace messages: Need to remove them when testing is
complete, stick them back in when another error is found
CT010-3-1 Introduction to Software Development
Why do Testing?
Normally testing is done by
The programmer
Other members of the software team who did not code the
module being tested
Final users of the software product
Next Lesson
Structured Programming Control Structures
Operators and expressions
Conditional constructs
if..else constructs
nested if..else constructs
switch..case
break and continue statements