当前位置: 移动技术网 > IT编程>开发语言>Java > Java中char[]输出不是内存地址的原因详解

Java中char[]输出不是内存地址的原因详解

2019年07月22日  | 移动技术网IT编程  | 我要评论
前言 java中共有八种基本数据类型:byte,int,short,long,float,double,char,boolean。 计算机中的基础数据单位是bit,

前言

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种基本数据类型很简单,在示例中应用来看一下:

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);
 }
}

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

8种基本数据类型
5
z
false
3
4
32000000
5.0
6.0

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

基本数据类型和对象引用

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

int a=1;

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

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

employee a=new emploee(1.4);

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

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

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

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

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

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

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()方法被重写了。

来看输出结果:

e=wrapper$employee@1b6d3586
f=employer's year=5

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

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

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

下面的是double类中的方法:

private final double value;
public string tostring() {
 return tostring(value);
 }

整形数据类型取值范围

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

byte a=128;

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

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

基本类型的数组输出值

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);
 }
}

看一下打印的结果:

127
[i@15db9742
[i@6d06d69c
[i@7852e922
false
[z@4e25154f
a
abc
abc
1.2
[f@70dea4e

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

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

public void print(char c) {
 write(string.valueof(c));
 }

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

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调用的是下面的方法:

public void println(object x) {
  string s = string.valueof(x);
  synchronized (this) {
   print(s);
   newline();
  }
 }

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

public static string valueof(object obj) {
  return (obj == null) ? "null" : obj.tostring();
 }

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

public string tostring() {
  return getclass().getname() + "@" + integer.tohexstring(hashcode());
 }

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

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

public void println(char x[]) {
  synchronized (this) {
   print(x);
   newline();
  }
 }

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

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

然后看看print(x)函数:

public void print(char s[]) {
  write(s);
 }

最后是write()函数:

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这个数组中的每一个值,所以不是内存地址了。

总结

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

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网