CS61B Data Structure-Jonathan Lecture2 using objects - OBJECTS METHODS

news2025/4/21 13:28:40

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.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1902098.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

电量监测与电量计基础知识

硬件之路学习笔记 ​-----前文导读----- ①、公众号主页点击发消息 ②、点击下方菜单获取系列文章 -----本文简介----- 主要内容包括: ①:简介 ②:省成本方式-电阻分压 ③:精确方式-电量计与阻抗跟踪技术 ----- 正文 ----…

线程同步66666

1. 概述 当有多个线程访问同一个共享资源(临界资源)时,且不允许同时访问,那么就需要线程同步。常见的线程同步方式:互斥锁、读写锁、条件变量、信号量。 2. 互斥锁 互斥锁的方式可以简单概括为:锁定操作…

第一天(点亮led灯+led灯闪烁)——Arduino uno R3 学习之旅

​ 常识: 一般智能手机的额定工作电流大约为200mA Arduino Uno板上I/0(输入/输出)引脚最大输出电流为40 mA Uno板控制器总的输出电流为200 mA 点亮LED灯 发光二极管介绍 发光二极管(Light Emitting Diode,简称LED)是一种能够将电能转化为光能的固态的半导体器件…

好消息!Stable Diffusion 3 允许商业化,很快开源更大版本模型

7月6日凌晨,著名开源大模型平台Stability AI修改了社区许可协议,最新发布的文生图模型Stable Diffusion 3 Medium允许商业化(以下简称“SD3-M”)。 如果企业、个人开发者每年收入低于100万美元(大约726万元人民币&…

【漏洞复现】禅道——未授权登入(QVD-2024-15263)

声明:本文档或演示材料仅供教育和教学目的使用,任何个人或组织使用本文档中的信息进行非法活动,均与本文档的作者或发布者无关。 文章目录 漏洞描述漏洞复现测试工具 漏洞描述 禅道(Zentao)是一款开源的项目管理和协作…

AI文本转语音,再也不用担心视频配音了.

文章目录 简介代码实现调用开通百度付费包 简介 背景 我想要将文本,转为语音,然后配上图片,这样就可以很快生成一个视频. 可以说是配音吧,我还是比较喜欢通过代码来自动化.所以今天就来实现一下,同时做一下分享和记录.目标 通过python代码,自动将文本转为配音.平台 我选择了百…

MySQL:如何在已经使用的数据表中增加一个自动递增的字段

目录 一、需求 二、实现步骤 (一)数据表students (二)添加整型字段 (三)更新SID字段的值 1、使用用户定义的变量和JOIN操作 2、用SET语句和rownum变量 (1)操作方法 &#x…

法向量 - 平面上的法向量和曲面上的法向量

法向量 - 平面上的法向量和曲面上的法向量 flyfish 平面上的法向量 : 定义了一个平面 z 0 z 0 z0。 法向量是 (0, 0, 1),表示垂直于平面的向上方向。 使用 quiver 函数在平面上绘制法向量。 曲面上的法向量 : 定义了一个曲面 z x 2…

硬件开发工具Arduino IDE

招聘信息共享社群 关联上篇文章乐鑫ESPRESSIF芯片开发简介 Arduino IDE(集成开发环境)是为Arduino硬件开发而设计的一款软件,它提供了一个易于使用的图形界面,允许用户编写、编辑、编译和上传代码到Arduino开发板。Arduino IDE的…

封装日历uniapp,只显示年月不显示日

默认展示最新日期 子组件 <template><view class"date-picker"><picker mode"date" fields"month" change"onDateChange" :value"selectedDate"><view class"picker">{{ selectedDate…

SQL-DCL(三)

一.DCL介绍 DCL英文全称是Data Control Language(数据库控制语言),用来管理数据库 用户,控制数据库的访问权限。 二.两个方面 1.数据库可以由那些用户访问 2.可以访问那些内容 三.DCL-管理用户 1.查询用户 USE mysql SELECT * FROM user 2.创建用户 CREATE USER…

Raylib 坐标系

draftx 符号调整为正数 发现采样坐标系原点0&#xff0c;0 在左上角&#xff0c;正方向 右&#xff0c;下 绘制坐标系 原点0&#xff0c;0 在左下角&#xff0c;正方向 右&#xff0c;上 拖拽可得 #include <raylib.h> // 重整原因&#xff1a;解决新函数放大缩小之下…

代码随想录算法训练营第23天|LeetCode 39. 组合总和、40.组合总和II、131.分割回文串

1. LeetCode 39. 组合总和 题目链接&#xff1a;https://leetcode.cn/problems/combination-sum/description/ 文章链接&#xff1a;https://programmercarl.com/0039.组合总和.html 视频链接&#xff1a;https://www.bilibili.com/video/BV1KT4y1M7HJ 思路&#xff1a; 本题和…

【算法笔记自学】第 6 章 C++标准模板库(STL)介绍

6.1vector常见用法详解 #include <cstdio> #include <vector> using namespace std;int main() {int n, x;scanf("%d", &n);vector<int> v;for (int i 0; i < n; i) {scanf("%d", &x);v.push_back(x);}for (int i 0; i <…

方法引用详解

什么是方法引用&#xff1f;&#xff1a;针对于函数式接口中的抽象方法 为什么用方法引用&#xff1f;&#xff1a;避免代码的重复&#xff0c;简便书写&#xff0c;提高效率 在使用Lambda表达式的时候&#xff0c;我们实际上传递进去的代码就是一种解决方案&#xff1a;拿参数…

第15章 奇异值分解:习题解答及其案例

这一章初学建议看该视频学习&#xff0c;讲得歪瑞古德&#xff1a; 《统计学习方法》李航 第15章奇异值分解 1.矩阵的奇异值分解是指将 m n m \times n mn实矩阵 A A A表示为以下三个实矩阵乘积形式的运算 A U Σ V T A U \Sigma V ^ { T } AUΣVT 其中 U U U是 m m m阶正…

文件读写操作之c语言、c++、windows、MFC、Qt

目录 一、前言 二、c语言文件读写 1.写文件 2.读文件 三、c文件读写 1.写文件 2.读文件 四、windows api文件读写 1.写文件 2.读文件 五、MFC文件读写 1.写文件 2.读文件 六、Qt文件读写 1.写文件 2.读文件 七、总结 一、前言 我们在学习过程中&#xff0c…

图像分类-数据驱动方法

K近邻算法&#xff08;K-Nearest Neighbors&#xff0c;简称KNN&#xff09; KNN算法通过比较新样本与训练集中的样本的距离&#xff0c;然后根据最近的K个邻居的投票结果来决定新样本的分类。 如图所示&#xff0c;K越大的边界会更加平滑&#xff0c;本质上是根据某一样本最近…

从0到1:培训老师预约小程序开发笔记二

背景调研 培训老师预约小程序&#xff1a; 教师和学生可以更便捷地安排课程&#xff0c;并提升教学质量和学习效果&#xff0c;使之成为管理和提升教学效果的强大工具。培训老师可以在小程序上设置自己的可预约时间&#xff0c;学员可以根据老师的日程安排选择合适的时间进行预…

对接海康sdk-linux下复制jar包中resource目录的文件夹

背景 在集成海康sdk时,需要将一些组件放到项目中作为静态资源,并且海康的sdk初始化也需要加载这些静态资源,在windows下,使用一些File路径的方式是可以正确加载的,但是在linux上就会加载失败。 首先我是将海康的sdk组件放到resource下的,并且按照windows和linux设置了两…