java中为什么只有char打印出不是地址值

前言

Java中共有八种基本数据类型:byte,int,short,long,float,double,char,boolean。

计算机中的基础数据单位是bit, 1byte=8bit。

数据类型 存储大小 举例 注释 包装类
byte 1byte 3 字节 Byte
int 4byte 4 整数 Integer
short 2bytes 5 短整数 Short
long 8bytes 6 长整数 Long
float 4bytes 1.3 单精度浮点型 Float
double 8bytes 1.2 双精度浮点型 Double
char 2bytes ‘a' 字符 Char
boolean 1bit true 布尔值 Boolean

这8种基本数据类型很简单,在示例中应用来看一下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

public class Test {

 public static void main(String[] args){

 System.out.println("8种基本数据类型");

 int a=5;

 System.out.println(a);

 char b='z';

 System.out.println(b);

 boolean d=false;

 System.out.println(d);

 byte e=3;

 System.out.println(e);

 short f=4;

 System.out.println(f);

 long g=32000000;

 System.out.println(g);

 float h=5;

 System.out.println(h);

 double i=6;

 System.out.println(i);

 }

}

一段简单的输出代码,看看打印结果:

?

1

2

3

4

5

6

7

8

9

8种基本数据类型

5

z

false

3

4

32000000

5.0

6.0

可以看到输出结果是没有问题的。

基本数据类型和对象引用

基本数据类型会一直在栈中创建,当声明基本类型时,不需要new。

?

1

int a=1;

栈的读取速度比堆快。基本类型一旦被声明,java将在栈上直接存储它,所以基本类型的变量表示的是数据本身。

假如调用基本类型的包装类来创建对象,那么将会在堆中创建。

?

1

Employee a=new Emploee(1.4);

等号右侧的new Double() 。这个new是在内存的堆中为对象开辟控件,保存对象的数据和方法。

等号左侧 Double a。a指代的是Double的一个对象,称为对象引用,这个对象引用是在栈中创建的。实际上a不是对象本身,它用来指向一个地址。

赋值=。这个就是把对象的地址赋给a。

此时输出a就是一个内存地址。有兴趣的同学自己试一试。

这个地方说明一个问题,假如你自定义的对象重写了.toString方法,此处就会显示你的自定义的重写方法的输出值。

在java的基本类型包装类中就重写了这个方法,所以调用print方法时会自动调用它的toString()方法。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

public class Wrapper {

 static class Employee{

 static int age;

 Employee(int a){

  age=a;

 }

 }

 static class Employer{

 static int year;

 Employer (int y){

  year=y;

 }

 @Override

 public String toString() {

  return "Employer's year="+year;

 }

 }

 public static void main(String[] args){

 Employee e=new Employee(4);

 System.out.println("e="+e);

 Employer f=new Employer(5);

 System.out.println("f="+f);

 }

}

在上边的例子中Employee的toString()方法没有被重写,Employer的toString()方法被重写了。

来看输出结果:

?

1

2

e=Wrapper$Employee@1b6d3586

f=Employer's year=5

前者仍然是内存地址,后者是我们重写的方法。

print方法在调用事,假如类中的toString()方法没有被重写,则会电泳String.valueof()方法(后边有讲),假如重写了就会调用toString方法。

所有的包装类(Integer,Boolean等)都已经重写了toString方法,所以不会输出内存地址,而是输出正确的值。

下面的是Double类中的方法:

?

1

2

3

4

private final double value;

public String toString() {

 return toString(value);

 }

整形数据类型取值范围

byte占据8位,则其取值范围应该是2的8次方,也就是-128~127,超过这个区间就会报错,例如:

?

1

byte a=128;

在编译器中会报错,提示不能将int转换为byte,因为128已经超出byte的范围了。

同样可以推得其他值的取值范围。

基本类型的数组输出值

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

public class TestOne {

 public static void main(String[] args) {

 int a=127;

 System.out.println(a);

 int[] b=new int[]{1,2,3};

 System.out.println(b);

 int[] c=new int[100];

 System.out.println(c);

 int[] d={1,2,3};

 System.out.println(d);

 boolean e=false;

 System.out.println(e);

 boolean[] f={false,false,true};

 System.out.println(f);

 char g='a';

 System.out.println(g);

 char[] h={'a','b','c'};

 System.out.println(h);

 char[] i=new char[]{'a','b','c'};

 System.out.println(i);

 float j=1.2f;

 System.out.println(j);

 float[] k={1.2f,1.3f,1.4f};

 System.out.println(k);

 }

}

看一下打印的结果:

?

1

2

3

4

5

6

7

8

9

10

11

127

[I@15db9742

[I@6d06d69c

[I@7852e922

false

[Z@4e25154f

a

abc

abc

1.2

[F@70dea4e

可以看到,在结果中,所有的基本类型都可以打印出来,数组类型只能打印出char数组,其他的都是内存地址。

来看一下源码,在print函数中

?

1

2

3

public void print(char c) {

 write(String.valueOf(c));

 }

这个char被转换为了String类型,然后进行wirte方法:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

private void write(String s) {

 try {

  synchronized (this) {

  ensureOpen();

  textOut.write(s);

  textOut.flushBuffer();

  charOut.flushBuffer();

  if (autoFlush && (s.indexOf('\n') >= 0))

   out.flush();

  }

 }

 catch (InterruptedIOException x) {

  Thread.currentThread().interrupt();

 }

 catch (IOException x) {

  trouble = true;

 }

 }

这里会立即发送缓冲流输出。

对于所有的基础类型都会打印出具体的值,这个没有问题,但是对于数组为什么只有char的数组类型打印出了正确的结果而没有输出内存地址?

带着这个问题我们来了解一下:

对于int型数组,java调用的是下面的方法:

?

1

2

3

4

5

6

7

public void println(Object x) {

  String s = String.valueOf(x);

  synchronized (this) {

   print(s);

   newLine();

  }

 }

此处数组被认为是Object类型,调用的是

?

1

2

3

public static String valueOf(Object obj) {

  return (obj == null) ? "null" : obj.toString();

 }

此处的三目表达式用来判空,然后看一下obj.toString()方法:

?

1

2

3

public String toString() {

  return getClass().getName() + "@" + Integer.toHexString(hashCode());

 }

相信看到此处应该可以看出来为什么输出会是[I@1b6d3586了,I代表的类的名称。

那么对于char数组类型的调用呢,次数室友玄机的:

?

1

2

3

4

5

6

public void println(char x[]) {

  synchronized (this) {

   print(x);

   newLine();

  }

 }

此处调用的是println(char x[])这个函数,那么这个char x[]是个什么鬼呢?

其实就是java中的数组初始化,相当于char[] x 

然后看看print(x)函数:

?

1

2

3

public void print(char s[]) {

  write(s);

 }

最后是write()函数:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

private void write(char buf[]) {

  try {

   synchronized (this) {

    ensureOpen();

    textOut.write(buf);

    textOut.flushBuffer();

    charOut.flushBuffer();

    if (autoFlush) {

     for (int i = 0; i < buf.length; i++)

      if (buf[i] == '\n')

       out.flush();

    }

   }

  }

  catch (InterruptedIOException x) {

   Thread.currentThread().interrupt();

  }

  catch (IOException x) {

   trouble = true;

  }

 }

到了这大家知道为什么会有区别了么,因为其他类型的数组都被认为是Object类型了,所以会输出内存地址。而char[]调用的方法是输出char这个数组中的每一个值,所以不是内存地址了。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

猜你喜欢

转载自blog.csdn.net/sulaymanyf/article/details/81232055
今日推荐