知识库 知识库
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)

Jim FuckPPT

Java小学生
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)
  • Hyperskill - Java

  • 练习题

    • 选择题 & 填空题

      • What does the method print
        • Topic
        • Problem
        • Hint & Explain
      • Appending strings and ints
      • Varargs method invocation
      • Good reasons to use Java modules
      • Number generators
      • When the keyword cannot be used
      • Size and capacity
      • The number of copies
      • The number of insertions
    • 代码题

  • Frank - Java与生活

  • Frank - Java API进阶

  • 学习笔记

  • Java
  • 练习题
  • 选择题 & 填空题
Jim
2022-05-01
目录

What does the method print

# Topic

Arrays as parameters

# Problem

What does the method print

Here is a method that takes an array:

public static void method(int[] array) {
    array = new int[] { 1, 2, 3 };
}
1
2
3

We invoke this method inside another one:

int[] numbers = { 4, 5, 6 };


method(numbers);


System.out.println(Arrays.toString(numbers));
1
2
3
4
5
6
7

What does this code print to the standard output?

Select one option from the list

[x] [4, 5, 6]
[ ] The code can't be compiled.
[ ] [ ]
[ ] [1, 2, 3, 4, 5, 6]
[ ] It throws an exception.
[ ] [1, 2, 3]
1
2
3
4
5
6

Correct.

The correct answer is [4, 5, 6] .

Practice makes perfect. Good for you for not giving up easily!

# Hint & Explain

We pass the reference of 'numbers' to the method. Let's say it's XYZ999. The variable 'array' within the method get's a copy of it. So before any statement within the method is executed, we have 'numbers' with reference XYZ999 and 'array' (in scope of the method) with reference XYZ999. XYZ999 points to the place, where the actual values (4, 5, 6) are saved.

Then we instance 'array' with "array = new int[] { 1, 2, 3 }". With that, 'array' get's a new reference because we do instance it. Let's say ABC888. We have now 'array' with reference ABC888 which point to the place, where the actual values (1, 2, 3) are saved.

AND we still have 'numbers' with reference XYZ999 which points to (4, 5, 6). That's why printing 'numbers' print .....

编辑 (opens new window)
#Java#Array#Problem
上次更新: 2022/09/25, 10:41:23
Theory:Spring beans
Appending strings and ints

← Theory:Spring beans Appending strings and ints→

最近更新
01
《挪威的森林》
04-14
02
青钢影
04-14
03
Processing strings
02-18
更多文章>
Theme by Vdoing | Copyright © 2022-2023 Jim Frank | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式