java自動封裝功能

來源:互聯網
上載者:User

java基本類型具有封裝器類,使得可以在堆中建立一個非基本對象,用來表示對應的基本類型。

Java的自動封裝功能就是自動的將基本類型轉換為封裝類型,並可以反向轉換。

練習:編寫程式,證明自動封裝功能對所有的基本類型和封裝器類型都起作用。

Exercise 9: (2) Write a program that demonstrates that autoboxing works for all the primitive types and their wrappers.

public class AutoboxTest {public static void main(String[] args) {boolean b = false;char c = 'x';byte t = 8;short s = 16;int i = 32;long l = 64;float f = 0.32f;double d = 0.64;Boolean B = b;System.out.println("boolean b = " + b); System.out.println("Boolean B = " + B); Character C = c;System.out.println("char c = " + c);System.out.println("Character C = " + C);Byte T = t;System.out.println("byte t = " + t);System.out.println("Byte T = " + T);Short S = s;System.out.println("short s = " + s);System.out.println("Short S = " + S);Integer I = i;System.out.println("int i = " + i);System.out.println("Integer I = " + I);Long L = l;System.out.println("long l = " + l);System.out.println("Long L = " + L);Float F = f;System.out.println("float f = " + f);System.out.println("Float F = " + F);Double D = d;System.out.println("double d = " + d);System.out.println("Double D = " + D);}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.