溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

在Java項目中使用char[]無法輸出內(nèi)存地址怎么解決

發(fā)布時間:2020-11-11 15:36:03 來源:億速云 閱讀:175 作者:Leah 欄目:編程語言

本篇文章為大家展示了在Java項目中使用char[]無法輸出內(nèi)存地址怎么解決,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

前言

Java中共有八種基本數(shù)據(jù)類型:byte,int,short,long,float,double,char,boolean。

計算機(jī)中的基礎(chǔ)數(shù)據(jù)單位是bit, 1byte=8bit。

數(shù)據(jù)類型存儲大小舉例注釋包裝類
byte1byte3字節(jié)Byte
int4byte4整數(shù)Integer
short2bytes5短整數(shù)Short
long8bytes6長整數(shù)Long
float4bytes1.3單精度浮點(diǎn)型Float
double8bytes1.2雙精度浮點(diǎn)型Double
char2bytes‘a(chǎn)'字符Char
boolean1bittrue布爾值Boolean

這8種基本數(shù)據(jù)類型很簡單,在示例中應(yīng)用來看一下:

public class Test {
 public static void main(String[] args){
 System.out.println("8種基本數(shù)據(jù)類型");
 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);
 }
}

一段簡單的輸出代碼,看看打印結(jié)果:

8種基本數(shù)據(jù)類型
5
z
false
3
4
32000000
5.0
6.0

可以看到輸出結(jié)果是沒有問題的。

基本數(shù)據(jù)類型和對象引用

基本數(shù)據(jù)類型會一直在棧中創(chuàng)建,當(dāng)聲明基本類型時,不需要new。

int a=1;

棧的讀取速度比堆快。基本類型一旦被聲明,java將在棧上直接存儲它,所以基本類型的變量表示的是數(shù)據(jù)本身。

假如調(diào)用基本類型的包裝類來創(chuàng)建對象,那么將會在堆中創(chuàng)建。

Employee a=new Emploee(1.4);

等號右側(cè)的new Double() 。這個new是在內(nèi)存的堆中為對象開辟控件,保存對象的數(shù)據(jù)和方法。

等號左側(cè) Double a。a指代的是Double的一個對象,稱為對象引用,這個對象引用是在棧中創(chuàng)建的。實(shí)際上a不是對象本身,它用來指向一個地址。

賦值=。這個就是把對象的地址賦給a。

此時輸出a就是一個內(nèi)存地址。有興趣的同學(xué)自己試一試。

這個地方說明一個問題,假如你自定義的對象重寫了.toString方法,此處就會顯示你的自定義的重寫方法的輸出值。

在java的基本類型包裝類中就重寫了這個方法,所以調(diào)用print方法時會自動調(diào)用它的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()方法被重寫了。

來看輸出結(jié)果:

e=Wrapper$Employee@1b6d3586
f=Employer's year=5

前者仍然是內(nèi)存地址,后者是我們重寫的方法。

print方法在調(diào)用事,假如類中的toString()方法沒有被重寫,則會電泳String.valueof()方法(后邊有講),假如重寫了就會調(diào)用toString方法。

所有的包裝類(Integer,Boolean等)都已經(jīng)重寫了toString方法,所以不會輸出內(nèi)存地址,而是輸出正確的值。

下面的是Double類中的方法:

private final double value;
public String toString() {
 return toString(value);
 }

整形數(shù)據(jù)類型取值范圍

byte占據(jù)8位,則其取值范圍應(yīng)該是2的8次方,也就是-128~127,超過這個區(qū)間就會報錯,例如:

byte a=128;

在編譯器中會報錯,提示不能將int轉(zhuǎn)換為byte,因為128已經(jīng)超出byte的范圍了。

同樣可以推得其他值的取值范圍。

基本類型的數(shù)組輸出值

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

看一下打印的結(jié)果:

127
[I@15db9742
[I@6d06d69c
[I@7852e922
false
[Z@4e25154f
a
abc
abc
1.2
[F@70dea4e

可以看到,在結(jié)果中,所有的基本類型都可以打印出來,數(shù)組類型只能打印出char數(shù)組,其他的都是內(nèi)存地址。

來看一下源碼,在print函數(shù)中

public void print(char c) {
 write(String.valueOf(c));
 }

這個char被轉(zhuǎn)換為了String類型,然后進(jìn)行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;
 }
 }

這里會立即發(fā)送緩沖流輸出。

對于所有的基礎(chǔ)類型都會打印出具體的值,這個沒有問題,但是對于數(shù)組為什么只有char的數(shù)組類型打印出了正確的結(jié)果而沒有輸出內(nèi)存地址?

帶著這個問題我們來了解一下:

對于int型數(shù)組,java調(diào)用的是下面的方法:

public void println(Object x) {
  String s = String.valueOf(x);
  synchronized (this) {
   print(s);
   newLine();
  }
 }

此處數(shù)組被認(rèn)為是Object類型,調(diào)用的是

public static String valueOf(Object obj) {
  return (obj == null) ? "null" : obj.toString();
 }

此處的三目表達(dá)式用來判空,然后看一下obj.toString()方法:

public String toString() {
  return getClass().getName() + "@" + Integer.toHexString(hashCode());
 }

相信看到此處應(yīng)該可以看出來為什么輸出會是[I@1b6d3586了,I代表的類的名稱。

那么對于char數(shù)組類型的調(diào)用呢,次數(shù)室友玄機(jī)的:

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

此處調(diào)用的是println(char x[])這個函數(shù),那么這個char x[]是個什么鬼呢?

其實(shí)就是java中的數(shù)組初始化,相當(dāng)于char[] x

然后看看print(x)函數(shù):

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

最后是write()函數(shù):

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

到了這大家知道為什么會有區(qū)別了么,因為其他類型的數(shù)組都被認(rèn)為是Object類型了,所以會輸出內(nèi)存地址。而char[]調(diào)用的方法是輸出char這個數(shù)組中的每一個值,所以不是內(nèi)存地址了。

上述內(nèi)容就是在Java項目中使用char[]無法輸出內(nèi)存地址怎么解決,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI