CS61B Data Structure-Jonathan Lecture2 using objects - OBJECTS & METHODS

Recall

String s1; // Step 1: declare a String variable
s1 = new String(); // Step 2: assign it a value, a new empty string object


String 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:

  1. new String( ) constructs empty string ---- string contains no charactors
  2. whatever
  3. 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.inis a InputStream object reads from the keyboard.

readLine method is defined on BufferedReader objects

  • How do we construct a BufferedReader? With an InputStreamReader
  • How do we construct an InputStreamReader? We need an InputStream
  • 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.

  1. Object Reference:
    create an object using new 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.

  • 30
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值