int i=0; Integer j = new Integer(0); System.out.println(i==j); System.out.println(j.equals(i));

int i=0;
Integer j = new Integer(0);
System.out.println(i==j);
System.out.println(j.equals(i));

方法一考虑

一、

1、
==需要看情况,得看比较的是基本数据类型还是引用数据类型 1、若是比较的是基本数据类型,直接看值,若是值在【-128,127】且相等,则比较相等;若值不在【-128,127】或者两个数的值不相等,则比较为不等。
2、
若是比较的是引用数据类型(比如new与new)(或者new 与 基本数据类型),即使值相等,也会比较为不相等。

二、

equals比较是值,只要值相等即可

方法二考虑

一、比较的是值

1、基本数据类型与引用数据类型进行比较时,引用数据类型会进行拆箱,然后与基本数据类型进行值的比较

		举例: 
		int i = 9; 
		Integer j = new Integer(9); 
		i == j 返回的是true 

2、引用数据类型与基本数据类型进行比较,基本数据类型会进行自动装箱,与引用数据类型进行比较,Object中的equals方法比较的是地址,但是Integer类已经重写了equals方法,只要两个对象的值相同,则可视为同一对象,具体看API文档,所以这归根到底也是值的比较!

		举例: 
		int i = 2; 
		Integer j = new Integer(2); 
		j.equals(i) 返回的是true 

二、比较的是地址

1、如果引用数据类型是这样 Integer i = 12;直接从常量池取对象,这是如果数值是在-128与127之间,则视为同一对象,否则视为不同对象

		举例 
		Integer i = 9; Integer j = 9; 
		i == j 返回的是true 
		Integer i = 128; Integer j = 128;
	    i == j 返回的是false 

2、如果引用数据类型是直接new的话,不管值是否相同,这时两个对象都是不相同的,因为都会各自在堆内存中开辟一块空间

		举例: 
		Integer i =new Integer(9); 
		Integer j = new Integer(9); 
		i == j 这时返回的是false 

三、从常量池取对象跟new出来的对象也是不同的

	举例: 
	Integer i = 12; 
	Integer j = new Integer(12) 
	i == j 这时返回的是false
  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
习题2 3.使用"= ="对相同内容的字符串进行比较,看会产生什么样的结果。 答:首先创建一个字符串变量有两种方式: String str = new String<"abc">; String str = "abc"; 使用"= ="会因为创建的形式不同而产生不同的结果: String str1 = "abc"; String str2 = "abc"; System.out.println<str1==str2>; //true String str1 = new String<"abc">; String str2 = "abc"; System.out.println<str1==str2>; //false String str1 = new String<"abc">; String str2 = new String<"abc">; System.out.println<str1==str2>; //false 因此自符串如果是对内容进行比较,使用equals方法比较可靠。 ntln<str1==str2>; //true String str1 = new String<"abc">; String str2 = "abc"; System.out.println<str1.equals<str2>>; //true String str1 = new String<"abc">; String str2 = new String<"abc">; System.out.println<str1.equals<str2>>; //true 5.编写一个程序,把变量n的初始值设置为1678,然后利用除法运算和取余运算把变量的 每位数字都提出来并打印,输出结果为:n=1678。n的每位数字是1,6,7,8。若n为任意值 呢? 法一: public class Exercise5{ public static void main<String[] args>{ int n=1678; int unit; int decimal; int hundred; int thousand; int count; thousand=n/1000; count=n%1000; hundred=count/100; count=count%100; decimal=count/10; count=count%10; unit=count; System.out.println<"1678包含的数字分别是:"+thousand+','+hundred+','+decimal +','+unit>; } } //如果n为任意值 import java.io.*; public class Exercise51{ public static void main<String[] args> throws IOException{ System.out.print<"请输入一个整数:">; InputStreamReader isStream=new InputStreamReader<System.in>; BufferedReader bfReader=new BufferedReader<isStream>; String input=bfReader.readLine<>; int length=input.length<>-1; int n=new Integer<input>.intValue<>; while<length>=0>{ int divisor=<int> Math.pow<10,length>; length=length-1; int output=n/divisor; n=n%divisor; System.out.print<output+",">; } } } 法二:<建议使用> public class Exercise5{ public static void main<String[] args>{ int n=1678; int unit; int decimal; int hundred; int thousand; thousand=n/1000%10; hundred=n/100%10; decimal=n/10%10; unit=n%10; System.out.println<"1678包含的数字分别是:"+thousand+','+hundred+','+decimal +','+unit>; } } //如果n为任意值 import java.io.*; public class Exercise51{ public static void main<String[] args> t
java语言程序设计课后答案 作业参考答案 习题一 4、如何建立和运行Java程序, 首先启动文本编辑器,如记事本、UltraEdit等,编辑程序代码,并以.Java作为文件 扩展名保存程序源代码;然后进入dos环境利用javac编译源程序,生成扩展名为.class的 字节码文件;再利用命令java运行字节码文件,得到程序的运行结果。在集成开发环境J builder、Eclipse下,可以完成程序的编辑、编译、调试及运行等所有任务。 5、 public class LikeJava { public static void main(String [] args) { System.out.println("I Like Java Very much!"); } } 习题二 5、 (1) 45 (2) false (3) 14 (4) 14 (5),6 (6) true (7) 12 9、 public class Volume { public static void main(String [] args) { double r=0,v=0; r=double.parseDouble(args[0]); v=4*3.14159/3*r*r*r; System.out.println("球体积为:"+v); } } 习题三 8、 public class Factorials { public static void main(String args[]) { int i, j; long s=0, k; i=1; do //外循环开始 { k = 1; j=1; do{//内循环开始 k = k * j; //内循环体 j++; }while(j<=i);//内循环结束 System.out.println(i + "!=" + k); s = s + k; i++; }while(i<=20); //外循环结束 System.out.println("Total sum=" + s); } } 10、 public class Num { public static void main(String[]args) { int i,j,k,n; for (n=100;n<1000;n++) { i=n/100; j=(n-i*100)/10; k=n%10; if (i*i*i+j*j*j+k*k*k==n) System.out.print(n+" "); } } } 习题四 5、 import java.util.Scanner; class Factor{ long fac(int m) {if(m==0""m==1)return 1; else return m*fac(m-1); } public static void main(String [] args) {int i,n; long sum=0; String s=""; Scanner input=new Scanner(System.in); System.out.print("Please input n: "); n=input.nextInt(); Factor f=new Factor(); for(i=1;i<=n;i++) { System.out.println(f.fac(i)); sum=sum+f.fac(i); s=s+i+"!+"; } System.out.println(s.substring(0,s.length()-1)+"="+sum); } } 习题五 2、 import java.io.*; public class YangHuiOk { public static void main (String args[]) throws IOException { int max,a[][],i,j; char x; System.out.print("请输入杨辉三角要显示的行数: "); x=(char)System.in.read(); max = Integer.parseInt(String.valueOf(x)); a=new int[max][]; for (i=0;i<max;i++) { a[i]=new int[i+1]; } a[0][0]=1; for (i=1;i<max;i++) { a[i][0]=1; a[i][a[i].length-1]=1; for (j=1;j<a[i].length-1;j++) { a[i][j]=a[i-1][j-1]+a[i-1][j]; } } for(i=0;i<max;i++) { //for(j=0;j<=max-i;j++) System.out

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值