Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Lectures / lecture4

.pdf
Скачиваний:
32
Добавлен:
14.10.2016
Размер:
746.02 Кб
Скачать

APPLICATION DEVELOPMENT

SDP-2

Lecture 4.

Brodyagina Mariya

double d = 6.02E23; float f = 6.02e23f;

Литералы типа double можно тоже обозначать суффиксом d или D, но это особо не имеет смысла, так как вещественные литералы всегда по умолчанию double.

Overflow and Underflow in Java

floating point operators

While using java floating point operators, overflow will result in Infinity and underflow will result 0.0 As a general rule here also Java doesn’t throw an error or exception for overflow and underflow.

1.double inf = 1/0; // Infinity

2.double neginf = -1/0; // -Infinity

3.double negzero = -1/inf; // Negative zero

4.double NaN = 0/0; // NaN

Арифметические операции с плавающей точкой никогда не генерируют исключений, даже при выполнении недопустимых операций

Overflow and Underflow in Java int operators

Arithmetic integer operations are performed in 32-bit precision. When the resultant value of an operation is larger than 32 bits (the maximum size an int variable can hold) then the low 32 bits only taken into consideration and the high order bits are discarded. When the MSB (most significant bit) is 1 then the value is treated as negative.

Example for byte

0111 1111 = 127 // max byte value

0111 1111 + 1 = 1000 0000

1000 0000 = -128

1 bit = sign bit:

0 = “+”

1 = “-”

Using the instanceof Keyword

The Java language provides the instanceof keyword to determine an object’s class type at run time.

1public class EmployeeRequisition {

2public boolean canHireEmployee(Employee e) {

3if (e instanceof Manager) {

4

return true;

5

} else {

6

return false;

7

}

8}

9}

Cast in Java. Primitive types

Casting between primitive types enables you to convert the value of one type to another primitive type.

You must use an explicit cast to convert a value in a large type to a smaller type. Explicit casts take the following form:

(typename) value

A conversion from a type to that same type is permitted for any type.

Widening primitive conversions

19 specific conversions on primitive types:

byte to short, int, long, float, or double

short to int, long, float, or double

char to int, long, float, or double

int to long, float, or double

long to float or double

float to double

Narrowing primitive conversions

22 specific conversions on primitive types:

short to byte or char

char to byte or short

int to byte, short, or char

long to byte, short, char, or int

float to byte, short, char, int, or long

double to byte, short, char, int, long, or float

Соседние файлы в папке Lectures