温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

在Java项目中使用char[]无法输出内存地址怎么解决

发布时间:2020-11-11 15:36:03 来源:亿速云 阅读:160 作者:Leah 栏目:编程语言

本篇文章为大家展示了在Java项目中使用char[]无法输出内存地址怎么解决,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

前言

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

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

数据类型存储大小举例注释包装类
byte1byte3字节Byte
int4byte4整数Integer
short2bytes5短整数Short
long8bytes6长整数Long
float4bytes1.3单精度浮点型Float
double8bytes1.2双精度浮点型Double
char2bytes‘a'字符Char
boolean1bittrue布尔值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这个数组中的每一个值,所以不是内存地址了。

上述内容就是在Java项目中使用char[]无法输出内存地址怎么解决,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI