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.

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

安装 tesseract

安装 tesseract 1. Ubuntu-24.04 安装 tesseract2. Ubuntu-24.04 安装支持语言3. Windows 安装 tesseract4. Oracle Linux 8 安装 tesseract 1. Ubuntu-24.04 安装 tesseract sudo apt install tesseract-ocr sudo apt install libtesseract-devreference: https://tesseract-…

【算法专题】模拟算法题

模拟算法题往往不涉及复杂的数据结构或算法,而是侧重于对特定情景的代码实现,关键在于理解题目所描述的情境,并能够将其转化为代码逻辑。所以我们在处理这种类型的题目时,最好要现在演草纸上把情况理清楚,再动手编写代…

关于用户咨询华为擎云L410笔记本安装Windows系统的说明

同样也是单位购买的华为擎云L410 KLVU-WDU0笔记本电脑,国产UOS系统某些软件用着不是很方便,用户咨询是否能够安装Windows10或者Windows7? 带着种种疑问也做了一些查询,之前也给一些国产设备更改过操作系统,之前的国产设…

【MySQL】事务四大特性以及实现原理

事务四大特性 原子性(Atomicity) 事务中的所有操作要么全部完成,要么全部不执行。如果事务中的任何一步失败,整个事务都会被回滚,以保持数据的完整性。 一致性(Consistency) 事务应确保数据库…

Hack The Box -- Blazorized

一、准备工作 端口扫描 详细扫描 Starting Nmap 7.94SVN ( https://nmap.org ) at 2024-06-30 21:39 EDT Nmap scan report for 10.10.11.22 Host is up (0.26s latency).PORT STATE SERVICE VERSION 53/tcp open domain Simple DNS Plus 80/tcp op…

C++--partition库函数

介绍 在C中,partition函数通常是指STL(Standard Template Library)中的std::partition算法,它用于对一个序列进行分区操作。具体来说,std::partition接受一个范围和一个谓词(predicate)作为参数…

encrypt decrypt CA

encrypt & decrypt & CA 加密解密证书

基于PHP技术的在线校园美食攻略程序设计与实现

基于PHP技术的在线校园美食攻略程序设计与实现 摘 要 网络技术正在以空前持续的速度在改变着我们的生活。利用互联网技术,人们对网上食物共享越来越关注。基于此,本文利用 PHP技术,对网上大学饮食指南应用软件进行了研究。 整个系统的设计&a…

树莓派4B学习笔记17:RBG_LED全色域灯的驱动模块编写

今日继续学习树莓派4B 4G:(Raspberry Pi,简称RPi或RasPi) 本人所用树莓派4B 装载的系统与版本如下: 版本可用命令 (lsb_release -a) 查询: ​ Python 版本3.7.3: ​ 今日学习:RBG_LED全色域灯的驱动模块编写…

并发编程(多线程)带来了哪些问题?

前面我们了解到多线程技术有很多好处,比如说多线程可以充分利用多核 CPU 的计算能力,那多线程难道就没有一点缺点吗? 有。 多线程很难掌握,稍不注意,就容易使程序崩溃。我们以在路上开车为例: 在一个单向行驶的道路上,每辆汽车都遵守交通规则,这时候整体通行是正常的…

win10使用小技巧一

1. 查看电脑IP地址 步骤:按WinR打开运行框 → 输入cmd点确定 → 输入ipconfig回车 → 查看IP地址。 2. 解决网页文字不能复制 步骤:按F12 → 调试框里点击设置 → 向下滑找到 禁用 JavaScript → 勾选 → 复制文字。 3. 解决电脑不能上网 方法一&…

Sentinel限流算法总结

文章目录 一、线程隔离二、滑动窗口算法三、令牌桶算法四、漏桶算法 一、线程隔离 线程隔离有两种方式实现: 线程池隔离:给每个服务调用业务分配一个线程池,利用线程池本身实现隔离效果信号量隔离:不创建线程池,而是…

Qt Creator配置以及使用Git

Qt Creator配置以及使用Git 引言一、Qt Creator配置git二、Qt Creator使用git2.1 创建git仓库 or git项目导入Qt2.2 配置远端,拉代码 or 上传代码2.3 查看更改2.4 更多细节可参考官方文档 三、参考的博客以及文档 引言 Qt Creator配置Git之后,可以看作是…

最新CorelDRAW2024设计师的必备神器!

Hey,各位创意小能手和设计爱好者们,今天要跟大家安利一个超级给力的设计软件——CorelDRAW 2024!如果你还在用那些老旧的设计工具,那你就OUT啦!🎉🎨 CorelDRAW全系列汉化版下载网盘分享链接&am…

新书速览|UML 2.5基础、建模与设计实践

《UML 2.5基础、建模与设计实战》 本书内容 UML是以面向对象图形的方式来描述任何类型的系统,应用领域非常广泛,其中常用的是建立软件系统的模型。《UML 2.5基础、建模与设计实践》基于draw.io开源免费软件,全面讲解UML 2.5的基本概念和建模…

【C++】 解决 C++ 语言报错:Double Free or Corruption

文章目录 引言 双重释放或内存破坏(Double Free or Corruption)是 C 编程中常见且严重的内存管理问题。当程序尝试多次释放同一块内存或对已经释放的内存进行操作时,就会导致双重释放或内存破坏错误。这种错误不仅会导致程序崩溃&#xff0c…

移动应用开发课设——原神小助手文档(1)

2023年末,做的移动应用开发课设,分还算高,项目地址:有帮助的话,点个赞和星呗~ GitHub - blhqwjs/-GenShin_imp: 2023年移动应用开发课设 本文按照毕业论文要求来写,希望对大家有所帮助。 xxxx大学课程设计报…

SpringBoot项目练习

文章目录 SpringBootVue后台管理系统所需软件下载、安装、版本查询Vue搭建一个简单的Vue项目 Spring项目1项目架构 SpringBootVue后台管理系统 学习视频: https://www.bilibili.com/video/BV1U44y1W77D/?spm_id_from333.337.search-card.all.click&vd_sourcec…

基于SpringBoot的休闲娱乐代理售票系统

本系统主要包括管理员和用户两个角色组成;主要包括:首页、个人中心、用户管理、折扣票管理、分类管理、订单信息管理、退票信息管理、出票信息管理、系统管理等功能的管理系统。 💕💕作者:Weirdo 💕&#x…

机器学习——无监督学习(k-means算法)

1、K-Means聚类算法 K表示超参数个数,如分成几个类别,K值就取多少。若无需求,可使用网格搜索找到最佳的K。 步骤: 1、随机设置K个特征空间内的点作为初始聚类中心; 2、对于其他每个点计算到K个中心的距离,…