Transcript Java notes
Java Programming
Goals:
• Illustrate the basic concepts of
programming languages and programs.
• Gain some experience with a high-level
language.
• Teach some basic programming concepts.
Machine Language
Each primitive step is a machine language
instruction.
Complicated steps become several primitive
instructions. eg. Adding a pair of numbers
requires:
Move data to a register
Move other data to a register
Add the contents of one register to the other
Move the result to a memory location
Assembly Language
Slightly abstracted from machine language.
More easily understood than machine language.
00101001 1010100
Becomes
LDX
$#84
Both mean: “load the register X with the contents of
memory location 84.”
High Level Language
Programs are usually written in a language closer to
natural language (ie. English).
Then another program is used to translate the
program into machine language.
In high level language instead of numbers symbols
are used to represent locations in memory.
One of the steps in the translation is to convert a
symbol to a memory address.
Translators: Compiler verses interpreter.
Interpreter vs Compiler
Interpreter: line-by-line translation and
execution.
Compiler: entire translation before execution.
Execution is often a separate step.
Java Language
•
•
•
•
•
•
•
general-purpose
high-level
object-oriented
architectural-neutral
portable
dynamic
supports multiprocessing
• Java program
• Java platform:
Java API (Application
Programming Interface)
Java VM (Virtual
Machine)
• Hardware specific platform
Java API
• Software components organized into
libraries.
• Eg. StringBuffer
Types of Programs
• Applications
Stand alone programs
Run from a command line or IDE
Include a main method
• Applets
Run from a HTML document
Usually graphical images or interaction with the
user
Include a paint or run method
Object-oriented Programming
• Programs a composed of classes and objects
(instances of classes).
• Classes include data (fields) and operations
(methods).
Inheritance
Objects are organized into class relationships.
There is a parent-child relationship between
related classes.
A child class is a subclass of the parent.
The child class extends the parent class by
adding more data or more operations.
Simple Application Java Syntax
import statements
class class name {
public static void main {
main method contents
}
}
Class Syntax
import statements
modifiers class class name {
any instance variable declarations
any method definitions
may include constructor definitions
}
Method Syntax
modifiers return type method name( list of
parameters ) {
statements
}
(A constructor is a special method used to
create an object.)
Statement Syntax
statement;
Block of Statements
{
statements
}
Modifiers
Modifiers describe or restrict a class, a
method or a field.
• public
• private
• protected
• static
Return Type
Methods can return a value.
The type of the value returned is listed in the
header of the method.
If the method does not return a value, its
return type is void.
Parameters
Parameters are a list of variables used to
represent information that is passed to a
method from a calling program.
Program Style
• Meaningful Identifiers
• Paragraphing
• Comments
Naming Conventions
• Classes
• Constants
• Others
Variables
Variables represent locations in memory that
store information.
Each variable has a data type.
Primitive Data Types
•
•
•
•
boolean
char
int
double
Declaration Statements
type name identifier;
int value;
boolean isGreater;
class name identifier;
String name;
Assignment Statements
variable = expression;
value = 27;
isGreater = true;
Instance of a Class
class variable = new class name( list of
arguments );
account = new Account( name,
accountNumber, balance );
Using Object and Class members
Static members
class name.variable name
class name.method name( arguments )
Instance members
object name.variable name
object name.method name( arguments )
Arguments
List of constants or variables in a method call
statement.
The values are passed to the method when the
method is called.
this and super
this refers to the object currently being
executed.
super refers to the parent of the current object.
Imports
Import statements tell a program where to find
information needed in the program.
Wrapper classes
Each primitive class has a corresponding
wrapper class.
Wrapper classes include methods for
converting from one type to another.
Exceptions
An event that occurs during execution that
makes continued execution impossible.
Exceptions are used to catch such an event
and do something appropriate.
Input and Output
Applications
Input and output are in the from of Strings.
Applets
Input can be button clicks or menu selections,
etc.
Output can be graphics, etc.
Graphics
The Graphics class includes methods to
display shapes and text.
Eg
public drawLine( int x1, int y1, int x2, int y2 )
Draws a line from one point to another, from
(x1, y1) to (x2, y2).
Operators
Numeric
+ add
- subtract
etc.
Assignment
= assign
etc.
Boolean
&& and
|| or
String operations
String operator
+ concatenate (join)
String methods
public int length()
public int indexOf( String s )
public String( int s, int l )
public boolean equals( String s )
Expressions
Use operators to combine constants, values
stored in variables and values returned from
method calls.
Eg
count / 2
count < 0 && value – 5 != 0
surname + “Fred”
Precedence of operators
Order of evaluation of operators in
expressions.
Selection statements
Single branch
if( boolean expression )
statement or block of statements
Two-way branch
if( boolean expression )
statement or block of statements
else
statement or block of statements
Repetition
while loop
while( boolean expression )
statement or block of statements
Counted loop
for( initialization; termination; change )
statement or block of statements
Errors
•
•
•
•
Lexical errors
Syntax errors
Run-time errors
Intent errors