SCJP_104——题目分析(5)

18.


public class Test
{
public static void add3(Integer i)
{
int val=i.intvalue();
val+=3;
i=new Integer(val);
}

public static void main(String args[])
{
Integer i=new Integer(0);
add3(i);
System.out.println(i.intvalue());
}
}

what is the result? b
A. compile fail B.print out "0" C.print
out "3"
D.compile succeded but exception at line 3

这里考察的是 Java 中的值传递

在 Java 中,方法的参数的传递究竟是按值传递还是按引用传递,一直以来都是有争论的。争论的原因是因为 Java
中有两种数据类型,一种是基本数据类型,一种是引用数据类型。
先讲基本数据类型的参数传递。与 C
语言不同,将实际参数传递给方法后,对参数的改变不会影响到原来的参数。这是因为 Java
传递的是原来参数的拷贝,不会影响到原来的数值。
比如下面的例子


 1 public class Test
2 {
3 public int value = 0;
4
5 public void add3(int value)
6 {
7 value++;
8 }
9
10 public static void main(String[] args)
11 {
12 Test test = new Test();
13 System.out.println("value 的初始值为 " + test.value);
14
15 System.out.println("调用 add3 方法后");
16 test.add3(test.value);
17 System.out.println("value 的值为 " + test.value);
18 }
19 }

输出结果为

value 的初始值为 0
调用 add3 方法后
value 的值为 0

接着说引用数据类型。引用数据类型变量存放的是内存地址,将引用变量拷贝一份传入方法内部,那么方法内部的变量和方法外部的变量指向的是同一个内存地址。方法内部做出的改变可能会影响到外部的数值。

将上面的例子改一下,传入Test对象的引用,如下


 1 public class Test
2 {
3 public int value = 0;
4
5 public void add3(Test t)
6 {
7 t.value++;
8 }
9
10 public static void main(String[] args)
11 {
12 Test test = new Test();
13 System.out.println("value 的初始值为 " + test.value);
14
15 System.out.println("调用 add3 方法后");
16 test.add3(test);
17 System.out.println("value 的值为 " + test.value);
18 }
19 }

输出结果为:

value 的初始值为 0
调用 add3 方法后
value 的值为 1

但是,如果传入的引用不再指着原来的对象,而是转而指向了别的对象,外部的对象不会也随着指向另外的对象,因为 Java
是值传递的,传入的是值的拷贝。
题目的例子就是这样:


 1 public class Test
2 {
3 public static void add3(Integer i)
4 {
5 int val=i.intvalue();
6 val+=3;
7 i=new Integer(val);
8 }
9
10 public static void main(String args[])
11 {
12 Integer i=new Integer(0);
13 add3(i);
14 System.out.println(i.intvalue());
15 }
16 }

i 指向 Integer 的对象,拷贝一份 i 传入方法后,就有两个 i 指向 Integer 对象,在方法内对 i
的属性的改变会影响到方法外部。

i = new Integer(val) 这句话创建了一个新的 Integer 对象,这时候方法内的 i 不再指向原来的 Integer
对象,而是指向新的 Integer 对象,而方法外部的 i 还是指向原来的 Integer 对象。

所以正确答案很明显了 B:print out
"0"

SCJP_104——题目分析(5),布布扣,bubuko.com

时间: 2024-10-13 18:58:36

SCJP_104——题目分析(5)的相关文章

SCJP_104——题目分析(3)

11. what is reserved words in java?A. run B. default C. implement D. import Java 中,给标识符取名的时候,不能使用关键字和保留字. 在 Java 中常用的关键字有: 1.访问控制符: public.protected.private 2.数据类型 byte.short.int.long.float.double.char.boolean 3.与类.方法有关的 new.class.extends.implements.

SCJP_104——题目分析(2)

3. public class IfTest{ public static void main(String args[]){ int x=3; int y=1; if(x=y) System.out.println("Not equal"); else System.out.println("Equal"); } } what is the result? 这一题考察的是 if 语句.if 语句的写法如下: if (boolean expression) { st

SCJP_104——题目分析(1)

1.1) public class ReturnIt{2) returnType methodA(byte x, double y){3) return (short)x/y*2;4) }5) }what is valid returnType for methodA in line 2? 这一题考的是Java中的类型转换. 在这里,自需要看第(3)据代码就行了.分析以下这一行代码 (short)x / y * 2 这一行代码对 x 进行类型转换,将 byte 类型的 x 转换成 short 类

SCJP_104——题目分析(4)

14. which three are valid declaraction of a float? ADFA. float foo=-1; B. float foo=1.0; C. float foo=42e1; D. float foo=2.02f; E. float foo=3.03d; F. float foo=0x0123; 这一题考的是 Java 的基本数据类型和类型转换. 在 Java 中规定,整数字面值默认为 int 类型,浮点数字面值默认为 double 类型. A:-1 是

最长英文单词串题目分析

题目来源: 这是题目来源 序: 简述一下事情的经过 一天邹欣老师在群里向我提出了一个问题: "把这个题目分析一下,主要是从工程上考虑,假如50个学生提交了程序,你如何写一个测试的系统,判断对错,和测量学生程序的对错" 一.首先分析一下这道题的核心 "最长的能首尾相连的英语单词链,每个单词最多只能使用一次" 这里题目的描述有一点不明确,关于"最长",题目并没有给出详细定义. 因此可能有两种情况. 单词链中单词数最多 单词链中字符数最多 (看到这里我

MySQL索引题目分析

1.之前看视频呢的时候,里面提到一道索引题目:假设某个表有一个联合索引(c1,c2,c3,c4)-只能使用该联合索引的c1,c2,c3部分 a.where c1=x and c2=x and c4>x and c3=x b.where c1=x and c2=x and c4=x order by c3 c.where c1=x and c4=x group by c3,c2 d.where c1=? and c5=? order by c2,c3 e.where c1=? and c2=? a

3Sum 等类似题目分析

3Sum 题目描述:给定一个整数数组,找出其中的三个数之和为0的所有组合(排除相同的组合). 分析:利用TwoSum 中两指针的思路,我们可以先将数组排序.要找到3个数之和为0,我们可以先固定一个数num[i],将i+1和len-1分别作为头指针和尾指针,当num[i].num[i+1]与num[len-1]的和大于0时,移动尾指针:小于0时,移动头指针:等于0则找到一组数.在循环查找下一组解时,当下一个值与当前值相等会出现重复的解,此时可以跳过当前循环来去除重复.具体代码如下: 1 vecto

SCTF 2014 pwn题目分析

因为最近要去做ctf比赛的这一块所以就针对性的分析一下近些年的各大比赛的PWN题目.主防项目目前先搁置起来了,等比赛打完再去搞吧. 这次分析的是去年的SCTF的赛题,是我的学长们出的题,个人感觉还是很符合套路的:一道栈溢出.一道格式化字符串.一道堆溢出. pwn200 一个栈溢出. 题目给出了libc.保护只有nx. 拿到题后的基本思路就是leak出got表中函数的地址,然后拿libc算偏移算出system的地址.然后用这个地址去覆盖一个函数的got表. pwn300 一个明显的格式化字符串漏洞

二分查找总结及部分Lintcode题目分析 2

Search in a big sorted array,这个比之前的二分法模板多了一个很不同的特性,就是无法知道一个重要的条件end值,也是题目中强调的重点 The array is so big so that you can not get the length of the whole array directly~所以这里单独分析这个问题~ 一般涉及到sorted array,肯定可以的解决方法是for loop,然而for loop的复杂度是O(n),而且对于这个长度无法衡量的big