Recall
String s1; // Step 1: declare a String variable
s1 = new String(); // Step 2: assign it a value, a new empty string objectString s2 = new String(); // 1&2 combined
今日知识点
situation: pointing to the same object
s1 = "Yow!";s2 = s1;
// take ehatever is dtored in the s1 box
// the old reference in s2 got overwritten, and replaced by new reference//Java did not create a new object, s1 and s2 are now pointing to the same object.
It is important for you to distinguish when two variables are pointing to the same object
from the case, that they are pointing at two different objects, that happen to have the same
data inside them.
copy a object
make a copy of this object, instead of having the pointers point to the same object
Java has a constructor that lets us do that
s2 = new String(s1);
//constructor
//Now 2 different identical objects
parentheses
Questions and Answer:
in java, you can not change string object directly
in java, equal sign not mean mathematical equality,
equal sign mean assignment, means take a copy of something stored in one box, and store that copy in another box.
Java does not allow to access random memory locations. C does.
3 String constructors:
new String( )
constructs empty string ---- string contains no charactors- “
whatever
” new String(s1)
takes a parameter s1;
// Makes copy of object that s1 references.
parameter means argument, same thing
Constructors always have same name as their class, except “stuffin quotes”
s2 = s1.toUppercase();
s1 point object does not change, it creates a new object do the uppercase change and reference to s2.
s2 will abandon old pointed object, and updated, pointing to this one.
Q&A
Java has garbage collection, which erases objects that can no longer be accessed from any variable at all.
running order, to run a method, you need to have the object to call on
–Next Part–
the object “Yow!” did not change, s2 changed.
Strings are immutable: their content never change, as Java did not give you a way to change.
In java, most object you can change their contents, but strings are an exception.
Java I/O Classes
java built in classes and methods to help you to do this
Objects in System class for interacting with a user.
System.out
is a PrintStream
object that outputs to the screen.
System.in
is a InputStream
object reads from the keyboard.
readLine
method is defined on BufferedReader
objects
- How do we construct a
BufferedReader
? With anInputStreamReader
- How do we construct an
InputStreamReader
? We need anInputStream
- How do we construct an
InputStream
?System.in
is one.
(Figure this out From Online Java Libraries API, java.io library)
separate the three tasks, as modularity, you can take any one to completely reimplemented it from scratch, without affecting or changing the other two tasks.
// read a line from keyboard and print it
import java.io.*;class SimpleIO{public static void main(String[] org) throws Exception{
//create a BufferedReader Object, that we can read stuff from,
/*It takes System.in, pass to InputStreanReder constructor, that is create InputStreamReader object, we do not need to store that in variable, we take that and pass it directly to a constructor, that constructs a bufferedreader object, which internally uses the inputstreamReader object
Once we have that, the variable keyboard is used to reference that bufferedreader object.
with keybd object, we can call readLine constructor on the BufferedReader, which read a line of the text from keyboard
finally that line of text/ string gets passed to the prints line method, which prints out on the screen.
*/BufferedReader keybd = new BufferedReader(new InputStreamReader(System.in));System.out.println(keybd.readLine());}
}
To use the Java libraries, other than java.lang
, you need to “import” them.
java.io
has ISR,BReader
.
A Java program always begins at a method “main”.
补充
Reference (引用)definition(from research)
definition : a variable holds the address of an object in memory.
- Object Reference:
create an object usingnew
keyword, Java allocates memory for the object and returns a reference to it.
This reference is then stored in a variable.
MyClass obj = new MyClass();
// obj is a reference to an instance of MyClass
instance(实例) definition
‘‘instance’’ refers to a specific realization of a class.
use new
to create an object of a class. The object is instance(实例).
Class Definition :
A class is a template, that defines the properties (fields) and behaviors (methods) that the objects created from the class will have.
public class MyClass {int x;int y;void display() {System.out.println("x: " + x + ", y: " + y);}
}
constructor definition
a constructor is a special method that is used to initialize objects.
The constructor is called when an instance of a class is created.
It has the same name as the class and does not have a return type, not even void
.