博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
从内存解释:方法传参(基本数据类型、引用数据类型(对象、数组、String类型)),原值是否改变?
阅读量:2056 次
发布时间:2019-04-28

本文共 877 字,大约阅读时间需要 2 分钟。

学习到宋红康String类型的一道面试题。

https://www.bilibili.com/video/BV1Kb411W75N?p=455
自己将其他的基本数据类型、对象作为参数,补充整理了一下。
代码改为:

public class StringTest {
int a = 0; String str = new String("good"); char[] ch = {
't', 'e', 's', 't' }; public void change(String str, char ch[], int a, Student stu) {
str = "test ok"; ch[0] = 'b'; a = 1; stu.score = 1; } public static void main(String[] args) {
StringTest ex = new StringTest(); Student stu1 = new Student(); ex.change(ex.str, ex.ch, ex.a, stu1); System.out.println(ex.str);//good System.out.println(ex.ch);//best System.out.println(ex.a);//0 System.out.println(stu1.score);//1 }}class Student{
int score = 0;}

内存解释

图上一目了然了,不多解释了。
另说明:
1、基本数据类型传的是数值,引用类型传的是地址。
2、①对象名存储在栈,形参名存储在栈,基本数据类型存储在栈。
②对象的属性存储在堆,如果该属性的类型是String,那么它还要指向方法区的常量池。

转载地址:http://minlf.baihongyu.com/

你可能感兴趣的文章
剑指offer 66. 和为S的两个数字
查看>>
leetcode 热题 Hot 100-5. 二叉树的最大深度
查看>>
leetcode 热题 Hot 100-2. 有效的括号
查看>>
leetcode 热题 Hot 100-3. 合并两个有序链表
查看>>
leetcode 热题 Hot 100-4. 对称二叉树
查看>>
Leetcode C++《热题 Hot 100-12》226.翻转二叉树
查看>>
Leetcode C++《热题 Hot 100-13》234.回文链表
查看>>
Leetcode C++《热题 Hot 100-14》283.移动零
查看>>
Leetcode C++《热题 Hot 100-15》437.路径总和III
查看>>
Leetcode C++《热题 Hot 100-16》448.找到所有数组中消失的数字
查看>>
Leetcode C++《热题 Hot 100-17》461.汉明距离
查看>>
Leetcode C++《热题 Hot 100-18》538.把二叉搜索树转换为累加树
查看>>
Leetcode C++《热题 Hot 100-19》543.二叉树的直径
查看>>
Leetcode C++《热题 Hot 100-21》581.最短无序连续子数组
查看>>
Leetcode C++《热题 Hot 100-22》2.两数相加
查看>>
Leetcode C++《热题 Hot 100-23》3.无重复字符的最长子串
查看>>
Leetcode C++《热题 Hot 100-24》5.最长回文子串
查看>>
Leetcode C++《热题 Hot 100-26》15.三数之和
查看>>
Leetcode C++《热题 Hot 100-27》17.电话号码的字母组合
查看>>
Leetcode C++《热题 Hot 100-28》19.删除链表的倒数第N个节点
查看>>