Java 1.5 Tiger A Developers Notebook [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Java 1.5 Tiger A Developers Notebook [Electronic resources] - نسخه متنی

David Flanagan, Brett McLaughlin

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید








4.6 Control Statements and Unboxing


There are several control statements

in Java that take as an argument a
boolean value, or an expression that results to a boolean value. It
shouldn't be much of a surprise that these expressions now also take Boolean values. Additionally, the switch statement has an array of new
types it will accept.


4.6.1 How do I do that?


if/else, while, and do all are affected by Tiger's ability to unbox Boolean
values to boolean values. By now, this shouldn't require much explanation:


Boolean arriving = false;
Boolean late = true;
Integer peopleInRoom = 0;
int maxCapacity = 100;
boolean timeToLeave = false;
while (peopleInRoom < maxCapacity) {
if (arriving) {
System.out.println("It's good to see you.");
peopleInRoom++;
} else {
peopleInRoom--;
}
if (timeToLeave) {
do {
System.out.printf("Hey, person %d, get out!%n", peopleInRoom);
peopleInRoom--;
} while (peopleInRoom > 0);
}
}

NOTE

You might want to
be cautious running
this codeit's
actually an
infinite loop.

There are several boxing and unboxing operations going on here, in several
control statements. Browse through this code, and work mentally
through each operation.

Another statement that benefits from unboxing is switch. In pre-Tiger
JVMs, the switch statement accepts int, short, char, or byte values.
With unboxing in play, you can now supply it with Integer, Short, Char,
and Byte values as well, in addition to the introduction of enums.

NOTE

Enums are
covered in
Chapter 3.


/ 131