日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

java char 打印_Java中char[]输出不是内存地址的原因详解

發(fā)布時間:2023/12/15 java 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java char 打印_Java中char[]输出不是内存地址的原因详解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

前言

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

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

數(shù)據(jù)類型

存儲大小

舉例

注釋

包裝類

byte

1byte

3

字節(jié)

Byte

int

4byte

4

整數(shù)

Integer

short

2bytes

5

短整數(shù)

Short

long

8bytes

6

長整數(shù)

Long

float

4bytes

1.3

單精度浮點型

Float

double

8bytes

1.2

雙精度浮點型

Double

char

2bytes

‘a(chǎn)'

字符

Char

boolean

1bit

true

布爾值

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)建的。實際上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類型,然后進行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ù)室友玄機的:

public void println(char x[]) {

synchronized (this) {

print(x);

newLine();

}

}

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

其實就是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)存地址了。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

總結(jié)

以上是生活随笔為你收集整理的java char 打印_Java中char[]输出不是内存地址的原因详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。