Data b;是声明一个Data类型的对象,指b可以指向一个Data对象 Data b=null,是不是等同于Data b; b=null,是不是指把null赋给了引用变量b;b被初始化了,只不过b存储的是一个null值,不指向任何Data对象。 这个理解对不对阿? String a="hello";和String a=new String("hello");什么区别阿?谁能帮我解答?
String a="hello",用到了字符串常量“hello”JAVA中会在内存中有该常量的存储,如果这样用String b="hello",那么比较(a==b)为TRUE,因为a和b同指向一个内存空间,这样是不正常的。 Data b 没有初始话,直接使用会通不过编译,Data b=null,这样就初始化了,JAVA不会报错。
这个面向对象有什么关系,java基础知识
Date d = null;其实包括了两个动作:声明、初始化(实例化)。它可以分解为:Date d; d = null;经过初始化的对象可以称之为一个实例,它可以在程序中参与操作和使用。 显然它跟Date d;是不一样的。关于第一个问题,说起来可能有点那个,看看这个测试程序: public class Mytest {
public static void main(String[] args) { String a = new String("hello"); String b = new String("hello"); String c = "hello"; String d = "hello";
Data b=null,是不是等同于Data b; b=null,是不是指把null赋给了引用变量b;b被初始化了,只不过b存储的是一个null值,不指向任何Data对象。
这个理解对不对阿?
String a="hello";和String a=new String("hello");什么区别阿?谁能帮我解答?
Data b 没有初始话,直接使用会通不过编译,Data b=null,这样就初始化了,JAVA不会报错。
显然它跟Date d;是不一样的。关于第一个问题,说起来可能有点那个,看看这个测试程序:
public class Mytest {
public static void main(String[] args) {
String a = new String("hello");
String b = new String("hello");
String c = "hello";
String d = "hello";
System.out.println("a==b : "+ (a==b) );
System.out.println("a.equals(b) : " + a.equals(b));
System.out.println("a==c : " + (a==c));
System.out.println("a.equals(c) : " + a.equals(c));
System.out.println("c==d : " + (c==d) );
System.out.println("c.equals(d) :" + c.equals(d));
System.out.println("toString : ----");
System.out.println("a.toString = " + a.toString() + " a = " + a);
System.out.println("b.toString = " + b.toString() + " b = " + b);
System.out.println("c.toString = " + c.toString() + " c = " + c);
System.out.println("d.toString = " + d.toString() + " d = " + d);
}}
结果 :
a==b : false
a.equals(b) : true
a==c : false
a.equals(c) : true
c==d : true
c.equals(d) :true
toString : ----
a.toString = hello a = hello
b.toString = hello b = hello
c.toString = hello c = hello
d.toString = hello d = hello
希望能够理解