Primitive Data Types, Study notes of Programming Paradigms

The Integer class has 2 special "int" values: Integer.MIN_VALUE is the minimum value of "int" type. Integer.MAX_VALUE is the maximum value of "int" type.

Typology: Study notes

2021/2022

Uploaded on 09/27/2022

picoo
picoo 🇮🇳

4.5

(13)

235 documents

1 / 41

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Primitive Data Types
James Brucker
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29

Partial preview of the text

Download Primitive Data Types and more Study notes Programming Paradigms in PDF only on Docsity!

Primitive Data Types

James Brucker

Primitive Data Types

 A primitive data type has only a value, such as a number.  Primitive types are things the CPU can directly manipulate. Example: 2 + 3 (cpu can add int)  Java has 8 primitive types, such as: boolean char int long double

int Data Type

  1. what values can the int type store?

"int" can store integer values in the range

- 2,147,483,648 to +2,147,483,

int Operations

Arithmetic (result is int) a + b a - b a * b a / b a % b a modulo b Comparison (result boolean) a < b a > b a <= b a >= b a == b a != b Operations that shift bits a <>n shift right with sign a >>>n shift right w/o sign Bit mask operations a | b bitwise "or" of a, b a & b bitwise "and" of a, b a ^ b bitwise exclusive or

int Special Values

The Integer class has 2 special "int" values: Integer.MIN_VALUE is the minimum value of "int" type. Integer.MAX_VALUE is the maximum value of "int" type.

Rules for int operations

  1. If the result is TOO BIG for "int" type, the higher order bits are lost. The result will be incorrect: 1,000,000,000 + 1,000,000,000 is 2,000,000, 2,000,000,000 + 1,000,000,000 is -1,294,967,
  2. On division of int/int the remainder is discarded. 28 / 10 is 2 -28 / 10 is - 1 / 2 is 0 even 999999 / 1000000 is 0 1 / 0 is error. Throws DivisionByZero exception.
  3. Modulo (%): m = a % b is such that b*(a/b) + m == a 7 % 3 is 1, -7 % 3 is -1 but 7 % -3 is 1

Primitive Data Types: values

Data Type Size in Memory Range of Values boolean 1 byte true false char 2 bytes 0 (null) - \t'uFFFF (Unicode) byte 1 byte -128 to 127 short 2 bytes -32,768 to 32, int int 4 bytes -2,147,483,648 to 2,147,483, long 8 bytes -9,223,372,036,854,775,808L 9,223,372,036,854,775,807L float 4 bytes ±3.402823E+ double 8 bytes ±1.797693134623157E+

double

  1. Any number written with "." or exponential is automatically of type double (not float).

double: 1.0 3.14159 2.99E+8 3e-

  1. If you do +, -, *, / with int and double, the result is a double. The "int" value is promoted to double first. 2 * 7.0 --> 14.0 (double) 10.0 * 2 / 5 --> 4.0 (double) but: 2 / 5 * 10.0 -- > 0 ("2/5" is done first as int/int)
    • , / , and % are always done before + and - 1.5 + 10 * 7.0 --> 71.

Double class has special values

Java has a class named Double -- not same as primitive type double. Double (class) has some special values: Double.POSITIVE_INFINITY Double.NEGATIVE_INFINITY Double.NaN Double.MAX_VALUE = 1.7976931348523E+ Double.MIN_VALUE = 4.9E- and some useful static methods: Double.parseDouble("2.14") // returns primitive 2. Double.toString(2.14) // returns String "2.14"

What Data Type?

______________ 1234, -

______________ 6010541234 (in Java: 6010541234L) ______________ 3.14159 ( what is this? ) ______________ 3E+ ______________ 3000.0F ______________ true ______________ '2' ______________ "2" ______________ 'ด' ______________ 3 == 4

Type Conversion

If your code contains: 2+ then Java sees that you are adding int + int and produces an int result ( 5 ). But, if your code contains: 2+3. it means to add "int" + "double" values. In this case, Java will convert 2 to a double (2.0) and add 2.0+3.0. The result is a double. Type conversion may also occur when you call a method. For example: Math.sqrt(2) The sqrt method requires a double parameter, so Java "promotes" 2 (int) to 2.0 (double).

Automatic Type Promotion

If you do arithmetic on different data types, Java "promotes" one argument to the type with widest range. Example Promotion Result 2 + 4L 2 -> (long)2L 6L (long) 2 * 4.0 2 -> (double)2.0 6.0 (double) 2F + 3 3 -> (float)3F 5.0F (float) 2.0 * 3 3 -> (double)3.0 5.0 (double) Weird: 'a'+1 'a' -> int (97) 98 double float long int short,char byte

What about boolean?

boolean type (true, false) cannot be converted to

any other type!

This is done to prevent accidental errors. A classic error in C programming is: int n = 1; if (n = 2) printf("its true!"); // set n=2, result is true! should be: if (n == 2)... ;

Common Type Errors

Here are some common errors. What is the mistake? How to correct it? // Compute typing speed in words/minute int wordsTyped = 38; // number of words typed int time = 45; // time in seconds double speed = wordsTyped/time * 60.0; // speed = 0 // The midterm exam has a maximum of 90 points. // "Normalize" the score to be 0-100 (e.g. 90 -> 100%). int midtermScore = 85; double score = 100.0 * (midtermScore / 90);