Download No Slide Title

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
Lecture 3
February 8, 2000
Types
• Every variable has a type.
– Primitive Types (built into the language).
• boolean, short, int, long, float, double, char
– Classes
• Provided with the JDK.
– Core: All Java implementations have these.
– Extensions: Those in javax.swing, for example.
• Written by developers such as you.
Variables With Primitive Types
• The variable is the name for a location in
memory that contains a value.
• Example
int
myVar = 345;
myVar
345
Variables With Class Types
• The variable is the name of a location in
memory that holds a reference to an object
that is somewhere else in memory.
• Example
String
myStr = new String(“Hello”);
object
myStr
reference
Hello
Arrays
• Arrays are primitive types in the sense that
they are built into the Java language.
• But arrays are classes in the sense that array
variables hold references to objects.
• Array types are created by putting square
brackets ( [ ] ) after another type name when
declaring a variable.
An Array Of Primitives
int[] firstArray = new int[ 3 ];
firstArray
Three ints
An Array of Objects
• Each object in the array has to be created.
• Example
String[] strArray = new String[2];
strArray[0] = new String(“Hello”);
strArray[1] = new String(“Bye”);
an array object
Hello
strArray
Bye
Object references
About Arrays
• The number of elements in an array is its length,
which cannot change.
• The elements in an array must all be of the same
type.
• The values of the elements of an array may change
as the program runs.
• Array elements are next to each other in memory,
which makes accessing them with subscripts
efficient.
Declaring and Initializing Arrays
• Sample Code
– [ SimpleArray_1D_a.java ]
Java Checks Array Subscripts
• As the program runs, each subscript value is
checked by the Java Virtual Machine (JVM)
to make sure it is valid.
– If not , the JVM throws an
ArrayIndexOutOfBounds exception.
• Sample Code
– [ SimpleArray_1D_b.java ]
Two Dimensional Arrays
• An array can have any number of
subscripts.
• Example
boolean[][] truth = new boolean[3][4];
truth
Exercise 2
• Due February 15
• Write a Java application that prints each of
the command line arguments on a separate
line on the console.