Типы, переменные, управляющие инструкции. Примитивные типы. (Тема 2.2) презентация

Содержание

Слайд 2


Слайд 3

Примитивные типы

Примитивные типы

Слайд 4

Диапазоны значений и размер

public class RangeSizeDemo {
public static void main(String[] args) {

System.out.println("byte min: " + Byte.MIN_VALUE);
System.out.println("byte max: " + Byte.MAX_VALUE);
System.out.println("byte length: " + Byte.SIZE);
System.out.println("short min: " + Short.MIN_VALUE);
System.out.println("short max: " + Short.MAX_VALUE);
System.out.println("short length: " + Short.SIZE);
System.out.println("char min: " + (int) Character.MIN_VALUE);
System.out.println("char max: " + (int) Character.MAX_VALUE);
System.out.println("char length: " + Character.SIZE);
System.out.println("int min: " + Integer.MIN_VALUE);
System.out.println("int max: " + Integer.MAX_VALUE);
System.out.println("int length: " + Integer.SIZE);
System.out.println("long min: " + Long.MIN_VALUE);
System.out.println("long max: " + Long.MAX_VALUE);
System.out.println("long length: " + Long.SIZE);
System.out.println("float min approx: " + Float.MIN_VALUE);
System.out.println("float max approx: " + Float.MAX_VALUE);
System.out.println("float length: " + Float.SIZE);
System.out.println("double min approx: " + Double.MIN_VALUE);
System.out.println("double max approx: " + Double.MAX_VALUE);
System.out.println("double length: " + Double.SIZE);
}
}

Диапазоны значений и размер public class RangeSizeDemo { public static void main(String[] args)

Слайд 5

Диапазоны значений и размер
byte min: -128
byte max: 127
byte length: 8
short min: -32768
short max:

32767
short length: 16
char min: 0
char max: 65535
char length: 16
int min: -2147483648
int max: 2147483647
int length: 32
long min: -9223372036854775808
long max: 9223372036854775807
long length: 64
float min approx: 1.4E-45
float max approx: 3.4028235E38
float length: 32
double min approx: 4.9E-324
double max approx: 1.7976931348623157E308
double length: 64

Диапазоны значений и размер byte min: -128 byte max: 127 byte length: 8

Слайд 6

Примитивные переменные
 [byte|short|char|int|long|float|double|boolean] variable [ = literal | = expression ];

Примитивные переменные [byte|short|char|int|long|float|double|boolean] variable [ = literal | = expression ];

Слайд 7

Примитивы и литералы

public class LiteralDemo {
public static void main(String[] args) {
String

name = "Harry Hacker";
char gender = 'm';
boolean isMarried = true;
byte numChildren = 2;
short yearOfBirth = 1987;
int salary = 30000;
long netAsset = 8234567890L;
double weight = 88.88;
float gpa = 4.58f;
System.out.println("Name: " + name);
System.out.println("Gender: " + gender);
System.out.println("Is married: " + isMarried);
System.out.println("Number of children: " + numChildren);
System.out.println("Year of birth: " + yearOfBirth);
System.out.println("Salary: " + salary);
System.out.println("Net Asset: " + netAsset);
System.out.println("Weight: " + weight);
System.out.println("GPA: " + gpa);
}
}
Name: Harry Hacker
Gender: m
Is married: true
Number of children: 2
Year of birth: 1987
Salary: 30000
Net Asset: 8234567890
Weight: 88.88
GPA: 4.58

Примитивы и литералы public class LiteralDemo { public static void main(String[] args) {

Слайд 8


Булевский тип

Булевский тип

Слайд 9

Булевские литералы

Булевские литералы

Слайд 10

Булевы логические операторы

Булевы логические операторы

Слайд 11

Таблица истинности

public class TruthTableDemo {
public static void main(String[] args) {
System.out.println("L\tR\tAND\tOR\tXOR\tNOT");
printLine(true,

true);
printLine(true, false);
printLine(false, true);
printLine(false, false);
}
static void printLine(boolean l, boolean r) {
System.out.println(l + "\t" + r + "\t" + (l & r) + "\t" + (l | r) + "\t" + (l ^ r));
}
}
L R AND OR XOR
true true true true false
true false false true true
false true false true true
false false false false false

Таблица истинности public class TruthTableDemo { public static void main(String[] args) { System.out.println("L\tR\tAND\tOR\tXOR\tNOT");

Слайд 12


Целочисленные типы

Целочисленные типы

Слайд 13

Целочисленные литералы

Целочисленные литералы

Слайд 14

Символьные литералы

Символьные литералы

Слайд 15

Преобразования целочисленных типов

Преобразования целочисленных типов

Слайд 16

Преобразование типов

public class ByteIntConversionDemo {
public static void main(String[] args) {
int xInt;

byte xByte;
System.out.println("Implicit conversion byte to int ...");
xByte = 100;
xInt = xByte;
System.out.println("xByte variable's value: " + xByte);
System.out.println("xInt variable's value: " + xInt);
System.out.println("\nNow explicit conversion int to byte ...");
xInt = 150;
xByte = (byte) xInt;
System.out.println("xInt variable's value: " + xInt);
System.out.println("xByte variable's value: " + xByte);
}
}
Implicit conversion byte to int ...
xByte variable's value: 100
xInt variable's value: 100
Now explicit conversion int to byte ...
xInt variable's value: 150
xByte variable's value: -106

Преобразование типов public class ByteIntConversionDemo { public static void main(String[] args) { int

Слайд 17

Арифметические операторы

Арифметические операторы

Слайд 18

Результат арифметических операций int

public class ByteDemo {
public static void main(String[] args) {

byte a = 10;
byte b = 20;
byte c = (byte) (a + b);
System.out.println("And the result is: " + c);
}
}
And the result is: 30

Результат арифметических операций int public class ByteDemo { public static void main(String[] args)

Слайд 19

Маскировка переполнения целых чисел

public class SilentOverflowDemo {
public static void main(String[] args) {

int posint = 2147483647;
System.out.println("number is: " + posint);
System.out.println("number + 1 is: " + (posint + 1));
System.out.println();
int negint = -2147483648;
System.out.println("number is: " + negint);
System.out.println("number - 1 is: " + (negint - 1));
}
}
number is: 2147483647
number + 1 is: -2147483648
number is: -2147483648
number - 1 is: 2147483647

Маскировка переполнения целых чисел public class SilentOverflowDemo { public static void main(String[] args)

Слайд 20

Маскировка переполнения целых чисел

public class SilentOverflowDemo {
public static void main(String[] args) {

int posint = 2147483647;
System.out.println("number is: " + posint);
System.out.println("number + 1 is: " + (posint + 1));
System.out.println();
int negint = -2147483648;
System.out.println("number is: " + negint);
System.out.println("number - 1 is: " + (negint - 1));
}
}
number is: 2147483647
number + 1 is: -2147483648
number is: -2147483648
number - 1 is: 2147483647

Маскировка переполнения целых чисел public class SilentOverflowDemo { public static void main(String[] args)

Слайд 21

Маскировка переполнения целых чисел

public class SilentOverflowDemo {
public static void main(String[] args) {

int posint = 2147483647;
System.out.println("number is: " + posint);
System.out.println("number + 1 is: " + (posint + 1));
System.out.println();
int negint = -2147483648;
System.out.println("number is: " + negint);
System.out.println("number - 1 is: " + (negint - 1));
}
}
number is: 2147483647
number + 1 is: -2147483648
number is: -2147483648
number - 1 is: 2147483647

Маскировка переполнения целых чисел public class SilentOverflowDemo { public static void main(String[] args)

Слайд 22

Маскировка переполнения целых чисел

public class SilentOverflowDemo {
public static void main(String[] args) {

int posint = 2147483647;
System.out.println("number is: " + posint);
System.out.println("number + 1 is: " + (posint + 1));
System.out.println();
int negint = -2147483648;
System.out.println("number is: " + negint);
System.out.println("number - 1 is: " + (negint - 1));
}
}
number is: 2147483647
number + 1 is: -2147483648
number is: -2147483648
number - 1 is: 2147483647

Маскировка переполнения целых чисел public class SilentOverflowDemo { public static void main(String[] args)

Слайд 23

Маскировка переполнения целых чисел

public class SilentOverflowDemo {
public static void main(String[] args) {

int posint = 2147483647;
System.out.println("number is: " + posint);
System.out.println("number + 1 is: " + (posint + 1));
System.out.println();
int negint = -2147483648;
System.out.println("number is: " + negint);
System.out.println("number - 1 is: " + (negint - 1));
}
}
number is: 2147483647
number + 1 is: -2147483648
number is: -2147483648
number - 1 is: 2147483647

Маскировка переполнения целых чисел public class SilentOverflowDemo { public static void main(String[] args)

Слайд 24

Результат арифметических операций int или long

public class ResultOverflowDemo {
public static void main(String[]

args) {
long yearMilliseconds = 1000 * 365 * 24 * 60 * 60;
System.out.println("Wrong number of milliseconds per year: " + yearMilliseconds);
yearMilliseconds = 1000L * 365 * 24 * 60 * 60;
System.out.println("Correct number of milliseconds per year: " + yearMilliseconds);
}
}
Wrong number of milliseconds per year: 1471228928
Correct number of milliseconds per year: 31536000000

Результат арифметических операций int или long public class ResultOverflowDemo { public static void

Слайд 25

Результат арифметических операций int или long

public class ResultOverflowVarDemo {
public static void main(String[]

args) {
int millis = 1000;
int days = 365;
int hours = 24;
int minutes = 60;
int seconds = 60;
long yearMilliseconds = (long)(millis * days * hours * minutes * seconds);
System.out.println("Wrong conversion int to long variable : " + yearMilliseconds);
yearMilliseconds = (long)(millis) * days * hours * minutes * seconds;
System.out.println("Correct conversion int to long variable : " + yearMilliseconds);
}
}
Wrong conversion int to long variable : 1471228928
Correct conversion int to long variable : 31536000000

Результат арифметических операций int или long public class ResultOverflowVarDemo { public static void

Слайд 26

Деление на ноль

public class ArithmeticExceptionDemo {
public static void main(String[] args) {
int

a = 10;
int b = 0;
System.out.println("We will try to divide " + a + " by " + b);
System.out.println(a / b);
}
}
We will try to divide 10 by 0
Exception in thread "main" java.lang.ArithmeticException: / by zero
at primitives.ArithmeticExceptionDemo.main(ArithmeticExceptionDemo.java:11)

Деление на ноль public class ArithmeticExceptionDemo { public static void main(String[] args) {

Слайд 27

Операторы сравнения и упорядоченности

Операторы сравнения и упорядоченности

Слайд 28

Битовые целочисленные операторы

Битовые целочисленные операторы

Слайд 29


Вещественные типы

Вещественные типы

Слайд 30

Вещественные литералы

Вещественные литералы

Слайд 31

Максимальные и минимальные по модулю значения

Максимальные и минимальные по модулю значения

Слайд 32

Специальные значения

Специальные значения

Слайд 33

Специальные значения

public class SpecialValuesDemo {
public static void main(String[] args) {
double max

= Double.MAX_VALUE;
double min = Double.MIN_VALUE;
System.out.println("Maximum double value approximately is: " + max);
System.out.println("Minimum positive double value approximately is: " + min);
System.out.println("Positive infinity is: " + max * 2);
System.out.println("Positive zero is: " + min / 2);
System.out.println("Negative infinity is: " + (-max * 2));
System.out.println("Negative zero is: " + (-min / 2));
System.out.println("Not a number: " + Math.sqrt(-1));
}
}
Maximum double value approximately is: 1.7976931348623157E308
Minimum positive double value approximately is: 4.9E-324
Positive infinity is: Infinity
Positive zero is: 0.0
Negative infinity is: -Infinity
Negative zero is: -0.0
Not a number: NaN

Специальные значения public class SpecialValuesDemo { public static void main(String[] args) { double

Слайд 34

Преобразования

Преобразования

Слайд 35

Преобразование типов

public class IntDoubleConversionDemo {
public static void main(String[] args) {
int xInt;

double xDouble;
System.out.println("Implicit conversion int to double ...");
xInt = 120;
xDouble = xInt;
System.out.println("xInt variable's value: " + xInt);
System.out.println("xDouble variable's value: " + xDouble);
System.out.println("\nNow explicit conversion double to int ...");
xDouble = 3.8547;
xInt = (int) xDouble;
System.out.println("xDouble variable's value: " + xDouble);
System.out.println("xInt variable's value: " + xInt);
}
}
Implicit conversion int to double ...
xInt variable's value: 120
xDouble variable's value: 120.0
Now explicit conversion double to int ...
xDouble variable's value: 3.8547
xInt variable's value: 3

Преобразование типов public class IntDoubleConversionDemo { public static void main(String[] args) { int

Слайд 36

Арифметические операторы

Арифметические операторы

Слайд 37

Неточность вещественной арифметики

public class ImprecisionDemo {
public static void main(String[] args) {
System.out.println(0.1);

System.out.println(0.1 + 0.1);
System.out.println(0.1 + 0.1 + 0.1);
}
}
0.1
0.2
0.30000000000000004

Неточность вещественной арифметики public class ImprecisionDemo { public static void main(String[] args) {

Слайд 38

Как хранятся числа с плавающей точкой

Как хранятся числа с плавающей точкой

Слайд 39

Пример хранения float 0.8125 = 0.5+0.25+0.0625

(-1)s × (1+m) × 2(e - 127)

0.8125

= (-1)0 × (1 + 0.5 + 0.125) × 2-1
bits: 31 30-23 22-0
binary: 0 01111110 10100000000000000000000
decimal: 0 126 5242880

2e-127(1 + m / 223) =
2-1(1 + (0.5 + 0.125)) =
2-1(1 + 5242880/8388608) =
2-1(1 + 0.625) = 0.8125

Пример хранения float 0.8125 = 0.5+0.25+0.0625 (-1)s × (1+m) × 2(e - 127)

Слайд 40

Пример хранения float 0.085

(-1)s × (1+m) × 2(e - 127)

0.085:
bits:

31 30-23 22-0
binary: 0 01111011 01011100001010001111011
decimal: 0 123 3019899

2e-127(1 + m / 223) =
2-4(1 + (0.25 + 0.0625 + 0.03125 + ...)) =
2-4(1 + 3019899/8388608) =
11408507/134217728 =
0.085000000894069671630859375

Пример хранения float 0.085 (-1)s × (1+m) × 2(e - 127) 0.085: bits:

Слайд 41

Операторы сравнения и упорядоченности

Операторы сравнения и упорядоченности

Имя файла: Типы,-переменные,-управляющие-инструкции.-Примитивные-типы.-(Тема-2.2).pptx
Количество просмотров: 69
Количество скачиваний: 0