Lecture 02
Constructors
The procedure done by constructor executing the line
s2 = new String(s);
- Look where s points
- Follows reference to string object
- Reads string
- constructs new string w/ copy of characters
- make s2 reference new string
3 String constructors now
new String()
"Yow!"
new String(s)
Constructor always has same name as their class, except the special constructor "stuffinquotes". That's the only exception.
Methods
s2 = s.toUppercase(); // Create a string like s, but in all upper case.
--- ----------
s2 |.+---->| YOW! |
--- ----------
String s3 = s2.concat("!!"); // Also written: s3 = s2 + "!!";
--- ------------
s3 |.+---->| YOW!!! |
--- ------------
String s4 = "*".concat(s2).concat("*"); // Also written: s4 = "*" + s + "*";
--- ------------
s4 |.+---->| *YOW!* |
--- ------------
Important facts
- When Java executed the line
s2 = s.toUppercase();
, the String object "Yow!" did not change. Java wrote a new "pointer" into the variable s2 so that s2 points to a different object. - Unlike C, in Java Strings are immutable -- once constructed, their contents never change.
I/O Classes and Objects in Java
Object in System
class for interacting with a user:
System.out
is a PrintStream object that outputs to the screen. (System.out references to the PrintStream object.)System.in
is an InputStream object that reads the keyboard.- A method called
readLine
to read a line, which is defined onBufferedReader
- construct an InputStream object and use a BufferedReader to read on top of it
- A method called
- How do we construct a
BufferedReader
? With anInputStreamReader
. - How do we construct a
InputStreamReader
? With anInputStream
. - How do we construct a
InputStreamReader
? With anSystem.in
is one.
- How do we construct a
More on Java API - java.io.
Why all this fuss?
InputStream
objects (like System.in) read raw data from some source (like the keyboard), but don't format the data.InputStreamReader
objects compose the raw data into characters (which are typically two bytes long in Java).BufferedReader
objects compose the characters into entire lines of text.
Why break down into so many classes?
- Object-oriented encourages you to write your code into decomposable pieces, so that each piece can be easily replaced.
Here's a complete Java program that reads a line from the keyboard and prints it on the screen.
import java.io.*;
class SimpleIO {
public static void main(String[] arg) throws Exception {
BufferedReader keybd =
new BufferedReader(new InputStreamReader(System.in));
System.out.println(keybd.readLine());
}
}
Classes for Web Access
Let's say we want to read a line of text from the White House Web page. (The line will be HTML, which looks ugly. You don't need to understand HTML.)
How to read a line of text? With readLine on BufferedReader. How to create a BufferedReader? With an InputStreamReader. How to create a InputStreamReader? With an InputStream. How to create an InputStream? With a URL.
import java.net.*;
import java.io.*;
class WHWWW {
public static void main(String[] arg) throws Exception {
URL u = new URL("http://www.whitehouse.gov/");
InputStream ins = u.openStream();
InputStreamReader isr = new InputStreamReader(ins);
BufferedReader whiteHouse = new BufferedReader(isr);
System.out.println(whiteHouse.readLine());
}
}
HFJ Reading
- Garbage-collection heap. Objects are created in an area of memory called The Heap.
- Many classes? Java Archive
.jar
file - based on pkzip format; can include a manifest that defines which class in that jar holds the main() method that should run.
Java API
- Java API, classes are grouped into packages.
[java.util].(ArrayList)
: [package name].[class name]
- Two ways to use
- import
- type
- eg.
java.util.ArrayList<Dog> list = new java.util.Arraylist...
- eg.
An
import
saves you from typing -- it's not the same as aninclude
in C.You get the
java.lang
package "pre-imported" for free.
Immutability
- Strings in Java are immutable.
- JVM puts a new String into memory "String Pool" - if there is already one, it doesn't create a duplicate. [Why? They are immutable so it's safe.]
- Garbage Collector doesn't go there. [Why?]
String and StringBuffer/StringBuilder Methods
- p. 669
string.charAt(i);
string.substring(i, j);
string1.concat(string2);
string.toUpperCase();
string1.equals(string2);
string1.equalsIgnoreCase(string2);
string.indexOf(substring); // cannot find: returns -1