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

  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.mzph.cn/bicheng/42020.shtml

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

相关文章

onclick和@click有什么区别,究竟哪个更好使?

哈喽小伙伴们大家好,我是爱学英语的程序员,今天来给大家分享一些关于vue中事件绑定相关的内容,希望对大家有所帮助. 场景是这样的:我要实现一个切换栏,默认激活的是第一个标签,当鼠标移动到第二个标签是,对应的内容让激活.起初,我第一时间想到的是用element plus的组件来实现这…

[leetcode hot 150]第一百一十七题,填充每个节点的下一个右侧节点

题目: 给定一个二叉树: struct Node {int val;Node *left;Node *right;Node *next; } 填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL 。 初始状态下&#x…

SpringSecurity6.x使用教程

SpringSecurity6.x使用 SpringSecurity版本 SpringSecurity目前支持的版本如下图所示&#xff0c;可以看到5.x的版本过几年就不会再维护了&#xff0c;6.x将成为主流。 入门 引入依赖 <dependency><groupId>org.springframework.boot</groupId><arti…

CMS Made Simple v2.2.15 远程命令执行漏洞(CVE-2022-23906)

前言 CVE-2022-23906 是一个远程命令执行&#xff08;RCE&#xff09;漏洞&#xff0c;存在于 CMS Made Simple v2.2.15 中。该漏洞通过上传头像功能进行利用&#xff0c;攻击者可以上传一个经过特殊构造的图片文件来触发漏洞。 漏洞详情 CMS Made Simple v2.2.15 中的头像上…

【C++/STL】优先级队列的介绍与模拟实现仿函数

✨ 万物与我皆是自由诗 &#x1f30f; &#x1f4c3;个人主页&#xff1a;island1314 &#x1f525;个人专栏&#xff1a;C学习 &#x1f680; 欢迎关注&#xff1a;&#x1f44d;点赞 &#x1f442;&#x1…

关于string的‘\0‘与string,vector构造特点加部分特别知识点的讨论

目录 前言&#xff1a; 问题一&#xff1a;关于string的\0问题讨论 问题二&#xff1a;C标准库中的string内存是分配在堆上面吗&#xff1f; 问题三&#xff1a;string与vector的capacity大小设计的特点 问题四&#xff1a;string的流提取问题 问题五&#xff1a;迭代器失…

04.ffmpeg打印音视频媒体信息

目录 1、相关头文件 2、相关结构体 3、相关函数 4、函数详解 5、源码附上 1、相关头文件 #include <libavformat/avformat.h> 包含格式相关的函数和数据结构 #include <libavutil/avutil.h> 包含一些通用实用函数 2、相关结构体 AV…

【PWN · ret2syscall | GoPwn】[2024CISCN · 华中赛区]go_note

一道GoPwn&#xff0c;此外便是ret2syscall的利用。然而过程有不小的曲折&#xff0c;参考 返璞归真 师傅的wp&#xff0c;堪堪完成了复现。复现过程中&#xff0c;师傅也灰常热情回答我菜菜的疑问&#xff0c;感谢&#xff01;2024全国大学生信息安全竞赛&#xff08;ciscn&am…

RabbitMQ快速入门 - 图像化界面的简单操作

目录 1、RabbitMQ的安装 2、RabbitMQ基本介绍 3、简单案例 4、数据隔离 1、RabbitMQ的安装 官网链接&#xff1a;rabbitmq官网 &#xff08;官网很详细&#xff0c;也可以在官网学习啦~&#xff09; 基础入门&#xff1a;自主学习&#xff1a;最新版本&#xff1a;安装我…

缓存-缓存的使用与基本详解

1.缓存使用 为了系统性能的提升&#xff0c;我们一般都会将部分数据放入缓存中&#xff0c;加速访问。而db承担数据落盘工作。 哪些数据适合放入缓存&#xff1f; 即时性、数据一致性要求不高的访问量大且更新频率不高的数据&#xff08;读多&#xff0c;写少&#xff09; …

如何配置 PostgreSQL 以实现高可用性和故障转移?

文章目录 一、高可用性和故障转移的概念&#xff08;一&#xff09;数据复制&#xff08;二&#xff09;监控和检测&#xff08;三&#xff09;快速切换 二、实现高可用性和故障转移的技术方案&#xff08;一&#xff09;流复制&#xff08;Streaming Replication&#xff09;&…

轻松创建对象——简单工厂模式(Java实现)

1. 引言 大家好&#xff0c;又见面了&#xff01;在上一篇文章中&#xff0c;我们通过Python示例介绍了简单工厂模式&#xff0c;今天&#xff0c;我们继续深入这个话题&#xff0c;用Java来实现简单工厂模式。 2. 什么是简单工厂模式 简单工厂模式&#xff08;Simple Facto…

idea部署war包成功,但是接口404

场景 项目结构 xxx-xxx-app xxx-xxx-service xxx-xxx-webappapp/webapp依赖service&#xff0c;service中写了各种api&#xff0c;先别管它合不合理&#xff0c;正式环境用webapp发布。 本地配置tomcat启动&#xff0c;但是发现每次部署成功&#xff0c;但是service中的接口…

【TB作品】脉搏测量,ATMEGA8单片机,Proteus仿真,ATmega8控制脉搏测量与显示系统

硬件组成&#xff1a; LCD1602脉搏测量电路&#xff08;带灯&#xff09;蜂鸣器报警按键设置AT24C02 功能&#xff1a; &#xff08;1&#xff09;LCD1602主页显示脉搏、报警上限、报警下限&#xff1b; &#xff08;2&#xff09;五个按键&#xff1a;按键1&#xff1a;切换设…

baomidou多数据源切换注解@DS没有效果

baomidou多数据源切换注解DS没有效果 <dependency><groupId>com.baomidou</groupId><artifactId>dynamic-datasource-spring-boot-starter</artifactId><version>3.1.1</version> </dependency> ##原因 方法上有Transaction…

Docker学习笔记(二)镜像、容器、仓库相关命令操作

一、docker镜像操作 列出镜像列表 我们可以使用 docker images 来列出本地主机上的镜像。 各个选项说明: REPOSITORY&#xff1a;表示镜像的仓库源 TAG&#xff1a;镜像的标签 IMAGE ID&#xff1a;镜像ID CREATED&#xff1a;镜像创建时间 SIZE&#xff1a;镜像大小 查…

昇思25天学习打卡营第9天|静态图模式的深度剖析与应用指南

目录 背景介绍 动态图模式 静态图模式 静态图模式的使用场景 静态图模式开启方式 基于装饰器的开启方式 基于context的开启方式 静态图的语法约束 JitConfig配置选项 静态图高级编程技巧 背景介绍 AI 编译框架主要包含两种运行模式&#xff0c;即动态图模式与静态图模…

19C 单机文件系统安装文档

准备工作 1)查看系统版本、内核参数 more /etc/redhat-release more /etc/redflag-releaseuname -a2)查看当前系统是否配置了HugePages。在下面的查询中&#xff0c;HugePages的几个相关值都为0&#xff0c;表明当前未配值HugePages&#xff0c;其次可以看到该版本的大页大小为…

Java之网络面试经典题(一)

目录 ​编辑 一.Session和cookie Cookie Session 二.HTTP和HTTPS的区别 三.浅谈HTTPS为什么是安全的&#xff1f; 四.TCP和UDP 五.GET和Post的区别 六.forward 和 redirect 的区别&#xff1f; 本专栏全是博主自己收集的面试题&#xff0c;仅可参考&#xff0c;不能相…

转发服务器实验

首先先克隆一个虚拟机并完成ip地址的修改 nmcli connection modify ens160 ipv4.addresses 192.168.209.128/24 nmcli connection modify ens160 ipv4.method manual nmcli connection modify ens160 connection.autoconnect yes nmcli connection up ens160 nmcli connection…