Download Lecture-01-Java-Intro

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Programs and Models

Almost all computer programs model some artifact
–

Model: a simplified representation of something
–
–

Artifact: product of civilization, either concrete or abstract,
simple or complex
It includes features that are considered important to the
other
Neglects other features not considered important
Characteristics of a model
–
–
–
–
Elements of the model represent other, more complex things
Model elements exhibit consistent behavior
Model elements can be grouped into different categories,
depending on their behaviors
Actions external to the model element cause the behavior of
the model element
Objects, behavior, and classes



In Java programming, model elements are called objects
Objects that share common behavior are grouped into
classes
Defining a class in Java is writing code that specifies how
objects of the class behave or act
–
–

Once a class has been defined, objects of that class can be
created
Every object belongs to exactly one class and is an instance
of that class
Predefined objects and classes
–
–
Java comes with some classes already defined
We will also use classes created by other programmers
Thinking about classes and
objects


Think of a library
To model a library, what would the relevant
classes and objects be?
Modeling a monitor

A device upon which information can be
displayed
–
For computers, monitors are devices to display
information on

–
(computers -> monitors)
For people, monitors are devices to read information
from

(monitors -> people)
Some terminology




Java uses a references to identify an object
Messages are sent to references, specifying
behavior with supporting details
Reference: any phrase that is used to refer to
an object
Messages: a request for some desired behavior
from an object
Java monitor is predefined

A monitor object is an instance of the PrintStream class
(which is predefined)
–
–

Does not model all features of a monitor (color)
Used to display sequences of characters
A predefined PrintStream object
–
–
System.out refers to the predefined PrintStream object
To display information on the monitor object, the Java
language sends a message to the System.out reference
 println
 Specifies a desired bahavior (printing a line)
 Further details (the characters to be displayed) must also
be sent
Sending a message


To send a message we must specify the object
and the behavior for that object
In Java this means
–
–
–

A reference to the receiver object
A period
The message to be sent
To display “Hello World” on a monitor:
System.out.println(“Hello World”);
reference
message
A Java program

A Java program needs
–
A name (called an identifier)


–
Identifier: a sequence of letters, digits, or underscores
The first character of an identifier must be a letter
Surround it with additional notation
Download FirstProgram.zip demo
Mechanics of Java programs

Java programs are actually contained in a class
that has a main() method

Java source files have the same name as the
class they contain, with the .java extension
–

FirstProgram.java
In CodeWarrior, we create projects that are the
same name as the primary class
–
FirstProgram.mcp
Mechanics of Java Programs


Source files (FirstProgram.java) need to be translated
to a form that machines can understand
These text files are compiled into Java Byte Code (an
idealized machine language), which will be contained in a
file with the same name as the text file but with the .class
extendion (FirstProgram.class)
–

CodeWarrior actually bundles all these class files into one
package called AppClasses
The Java Byte Code is interpreted by the Java interpreter
for the particular machine you are working with
–
–
Can be executed by any type of machine that has a Java
interpreter
Relatively easy to imbed Java interpreters into other
programs (web browsers)
Exercise

Let us modify FirstProgram as an exercise
PrintStream messages

Message println():
–
Two versions


–
–
–
–
println(<sequence of characters>);
println(empty);
In the first, the <sequence of characters> will be
displayed. Next character to be displayed will start on the
next line
In the second, nothing will displayed, and the next character
to be displayed will appear on the next line
Example
System.out.println(“Hello”);
System.out.println(“World”);
Result
Hello
World
PrintStream messages

Message print()
–
One versions

–
–
–
–
print(<sequence of characters>);
The <sequence of characters> will be displayed. Next
character to be displayed will start on the same line,
immediately after the last character in the <sequence of
characters>
Example
System.out.print(“Hello”);
System.out.print(“World”);
Result
HelloWorld
Next character to be printed will follow ‘d’
The String class (2.5)


A Java predefined class that models a sequence
of characters
Any group of characters in double quotes is a
String object
–
“Hello World”

We have used these as arguments to println() and
print() messages

These are known as String Constants (cannot be
changed)
Messages for String objects



println() and print() are messages for
PrintStream objects
What messages can we send to String objects?
In other words
–
–


What behavior does the String class provide?
What methods do the String class provide?
A beginning list can be found on page of the
text
A complete listing can be found in the Java API
(Application Programming Interface)
String references

A String constant is a String reference
–
–
Models precisely the character sequence
References can be used as arguments to messages

–
In other words, sending an object to another object
Can also be used to send messages to objects they
refer to

Evoking the behavior of the object referred to
Example: toUpperCase()

To send a message to the object, use the form
reference.methodname(arguments);

To send the toUpperCase() message (which has no
arguments) to the String reference “java”
“java”.toUpperCase();

The receiver is the String object “java” refers to
–
–
–
This message does not change the receiver
A String method never changes the String object
But produces a new String object with the desired change
String messages create new
String objects

A new object is useless unless it can be
referenced
–
–

toUpperCase() creates a reference (return value) to
the new object
The message (“java”.toUpperCase(); ) becomes the
reference to the new object
What to do with this reference?
–
–
Send messages to the object referred to (not here)
As an argument in a message to another object
System.out.println(“java”.toUpperCase());
Methods, arguments, and return
values
Class
Method
Return value
Arguments
received
PrintStream
println
None
None
PrintStream
println
None
Reference to
a String
object
PrintStream
print
None
Reference to
a String
object
String
toUpperCase
Reference to
a String
object
None
Methods, arguments, and return
values

Signature of a method
–
–

Prototype of a method
–
–

Name
Description of arguments
Signature
Description of its return value
Table on preceding page gives us information
about four methods and their prototypes
Reference variables

Variable
–
–
–
An identifier that can be given a value
It can contain different values at different times
Form:
type identifier;
type identifier1, identifier2, …;

Reference variables
–
–
–
An identifier that is given a reference
To be used later or repeatedly
Form:
class identifier;
class identifier1, identifier2, …;
Assignment

Assignment statements
–
–
An assignment statement gives a variable a value
Form
identifier = value;


–
–
Variable on the left-hand side
Value on the right-hand side
Variable must have already been declared
Value must be consistent with the variable type or
class
Assignment and Equality
String t = “Some String”;

This is an imperative statement
–
–
–
Tells the computer that the variable t refers to the String
object “Some String”
This is not a statement of equality
Because t can be made to refer to another String object
t = “A different String”;
–
The previous object “Some String” is lost and no longer
accessible to the program
Multiple assignment of objects
String t = “Some String”;
String s = t;
t = “A different String”;
–
–
–
–
The variable t refers to the String object “Some String”
The variable s refers to the same String object “Some
String” as the variable t
The variable t refers to a new String object “A different
String”
Variables are independent of each other;
 Changing t did not affect s
Using String Methods

Strings are immutable (they cannot be changed)
–
String methods do not change the reference object



Some commonly used String methods
–
–
–
–

They provide information about the object
They create references to new String objects (such as
toUpperCase() )
toUpperCase()
length()
concat(String)
substring(number)
-
toLowerCase()
trim()
indexOf(character);
substring (number, number)
Look at the String API for each of these methods
Two demonstration programs
Download MiddleCharacter.zip demo
Download NameInitials.zip demo