Fichas
Java Primitive Types
Fichas del conjunto (8)
- 1how many bits N do we need to represent a positive integer m?N = floor(log2 m) + 1
- 2how many bits are a byte?8 bits = byte
- 3name the Java primitive types and their number of bytesboolean (1), byte (1), char (2), short (2), int (4), long (8), float (4), and double (8)
A boolean variable uses a byte (8 bits), even though it needs just 1 of these 8 bits. The reason it uses 8 bits is that memory locations in the computer are indexed by bytes, not bits. - 4What can be used to represent an integer? these N-bits codes represent the integers in what range? that leads to what behavior?byte (8 bits)
short (16 bits)
int (32 bits)
long (64 bits)
range {-2^(N-1), ..., 0, 1, 2, ..., 2^(N-1) -1}
overflow and underflow - 5The binary representation of fractional numbers (float and double) is more complicated. what gives an error? int x = 3.0; int x = 3; float y = 3.0; float y = 3.0f; double z = 3.0; double x = 1; int x = 1.0/2; double y = 1/4; double y = 1.0/4;int x = 3.0; // gives a compiler error
int x = 3;
float y = 3.0; // gives a compiler error
float y = 3.0f; // note the ’f’
double z = 3.0;
double x = 1; // legal, but bad style!
int x = 1.0/2; // compiler error!
double y = 1/4; // no compiler error, but it stores 0 (not .25)
double y = 1.0/4; // gives the correct answer - 6A char variable is represented by ... bytes. How to assign a char primitive type?2 bytes
The first 128 unicode characters are encoded as 000000000******* (ASCII)
char c = 'a';
These are called character literals. - 7what's the widest to narrowest types?double (8) widest
float (4)
long (8)
int (4)
short (2)
char (2)
byte (1) narrowest - 8how do conversions happen in Java?Widening conversions occur automatically. Narrowing conversions require an explicit cast in the code; otherwise you will get a compiler error.
e.g. i = (int) d; // narrowing (by casting)
Información
- Descripción
- Computer Science Fichas sobre Java Primitive Types, creado por Virginie Gagné el 17/04/2022.
- Creado por
- Virginie Gagné
- Visitas
- 0
- Valoración