Java习题
2022/4/4 12:48:58
本文主要是介绍Java习题,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
public class ClassTest{ String str = new String("hello"); char[] ch = {'a','b','c'}; public void fun(String str, char ch[]){ str="world"; ch[0]='d'; } public static void main(String[] args) { ClassTest test1 = new ClassTest(); test1.fun(test1.str,test1.ch); System.out.print(test1.str + " and "); System.out.print(test1.ch); } }
结果:hello and dbc
分析:String被final修饰,即String被赋值之后是不可变的,调用fun方法时,传入的形参str在进行str="world"操作时实际上是new了一个新的String对象,并没有改变实参中str中的对象引用。形参ch与实参ch执行堆中的同一个地址,ch[0]='d'实际上也修改了实参ch中的数据
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"); } }
结果:An error at line 5 causes compilation to fall.
分析:Java和C的区别,Java条件判断语句中返回值只能是boolean类型,x=y是将y的值赋给x,再去判断if(1)的结果,这时类型不匹配,即int无法转换为boolean
public class Test2 { public void add(Byte b) { b = b++; } public void test() { Byte a = 127; Byte b = 127; add(++a); System.out.print(a + " "); add(b); System.out.print(b + ""); } }
结果:-128 127
分析:包装类但是final的,所以在add方法中只是新创建了一个对象,没有实际改变b的值,也就是说add方法没有对b造成任何改变,++a先自动拆箱得到byte类型数据127,再+1发生越界得到-128,最后自动装箱得到Byte类型数据-128
这篇关于Java习题的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-19永别了,微服务架构!
- 2024-05-15鸿蒙生态设备数量超8亿台
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?