在开始看我画小狗之前,咱们先来看道很简单的题目: 下面程序的输出是什么? Dog myDog = new Dog("旺财"); changeName(myDog); System.out.println(myDog.getName()); public void changeName(dog) { dog.setName("小强"); } 如果你的回答是“小强”,好,恭喜你答对了.下面我们改一下代码: Dog myDog = new Dog("旺财&quo…
小方法大门道 小瓜瓜作为一个Java初学者,今天跟我说她想通过一个Java方法,将外部变量通过参数传递到方法中去,进行逻辑处理,方法执行完毕之后,再对修改过的变量进行判断处理,代码如下所示. public class MethodParamsPassValue { public static void doErrorHandle() { boolean a = false; int b = 5; passBaseValue(a, b); if (a == true || b == 10) { S…
head first java里写到“java是通过值传递的,也就是通过拷贝传递”,由此得出结论,方法无法改变调用方传入的参数.该怎么理解呢? 看例子: public class Test1 { public static void main(String[] args) { int x = 7; System.out.println("传入方法之前:x="+x); addOne(x); System.out.println("传入方法之后:x="+x);}priv…
在Java中返回值定义为int类型的 方法return 1:中返回的是Integer值,在返回的时候基本类型值1被封装为Integer类型. 定义一个Test类,在异常处理try中和finally中分别return : public class Test { public static void main(String[] args) { System.out.println(new Test().test()); } int test() { try { return func1(); }fi…
package org.jimmy.autosearch20180821.test; public class TestStringArr { public static void main(String[] args) { String[] strArr = new String[]{ "1", "2" }; System.out.println(strArr[0]); test(strArr); System.out.println(strArr[0]); te…
StringBuilder.StringBuffer.String类之间的关系 java中String.StringBuffer.StringBuilder是编程中经常使用的字符串类,在上一篇博文中我们已经熟悉String字符串的特性和使用,而StringBuffer.StringBuilder又是怎么样的字符串类呢??他们之间的区别和关系又是什么呢??这问题经常在面试中会问到,现在总结一下,看看他们的不同与相同. 1.可变与不可变 1)String类中使用字符数组保存字符串,如下就是,因为有"…
public class Example { String testString = new String("good"); char[] testCharArray = {'a','b','c'}; public static void main(String[] args){ Example ex = new Example(); ex.change(ex.testString,ex.testCharArray); System.out.println(ex.testString)…
一.为什么要有volatilekeyword 预计非常多java刚開始学习的人都被volatile这个keyword迷惑过.尽管网上有非常多讨论volatile的文章,但它们有的过于讲述底层原理,而没有说明其应用场景,让刚開始学习的人看后还是一头雾水:有的过于使用类比解说.造成了一定的错误.这种文章更害人.以下,小弟试着分析下volatilekeyword的作用及使用方法.希望能给大家带来一定的启示.文中错误之处,请各位大神指正. 我们知道,在多线程编程中,多个线程在訪问共享变量时,必须进行必要…
public class TestExtends { public static void main(String[]args){ int s = 10; System.out.println(System.identityHashCode(s)); s=3*s; int num = 30; System.out.println(System.identityHashCode(s)); System.out.println(System.identityHashCode(num));} 输出 4…
public class Example { String testString = new String("good"); char[] testCharArray = {'a','b','c'}; public static void main(String[] args){ Example ex = new Example(); ex.change(ex.testString,ex.testCharArray); System.out.println(ex.testString)…