byte:- Size: 8 bits (1 byte)
- Range:
to ( ) - Use case: Low-level binary data handling, reading raw streams, or saving memory when working with huge arrays of small numbers.
short:- Size: 16 bits (2 bytes)
- Range:
to ( ) - Use case: Rarely used in modern code, occasionally for legacy data formats.
char:- Size: 16 bits (2 bytes)
- Range:
0to65,535(Unsigned) - Use case: Represents a single UTF-16 character (e.g.,
'a'). Under the hood, it is actually a numeric integer type, which is why you can do arithmetic on characters likechar c = 'a' + 1;.
boolean:- Size: JVM-dependent (typically 1 byte or 4 bytes in arrays).
- Range:
trueorfalse. - Note: Not a numeric primitive, but the final primitive type in Java.
1. Why does 2 ^ 32 output 34?
In Java (and most programming languages like C, C++, and Python), the caret symbol ^ is not an exponentiation operator. It is the bitwise XOR (Exclusive OR) operator.
When you write 2 ^ 32, Java evaluates the bitwise XOR of the numbers 2 and 32:
2in binary:00000000 00000000 00000000 0000001032in binary:00000000 00000000 00000000 00100000Because none of the bits overlap, XORing them results in adding their values together:32 + 2 = 34.
How to do exponents in Java
Floating-point exponentiation:
Math.pow(2, 32)(returns adouble).Bitwise shifting (for integers/longs):
1L << 32(efficiently shifts the bits left by 32 positions).
2. How Min and Max Values Are Calculated
The minimum and maximum limits for Java primitives depend on how they are stored in binary memory.
A. Integer Types (int and long)
int uses 32 bits (4 bytes), and long uses 64 bits (8 bytes). Java uses Two’s Complement representation to store signed integers (both positive and negative numbers).
- The Sign Bit: The leftmost (most significant) bit is reserved for the sign:
0for positive,1for negative. - Remaining Bits for Magnitude:
- For an
int, you havebits left for data ( ). - For a
long, you havebits left for data ( ). Calculations:
- For an
Integer.MAX_VALUE(or 2,147,483,647): The largest positive number you can make usingbits (since one bit is reserved for the 0sign). We subtractbecause zero ( 0) takes up one of the positive bit patterns.Integer.MIN_VALUE(or -2,147,483,648): The most negative number. Two’s complement allows one extra negative value because the bit pattern for-0is repurposed to represent-2,147,483,648.Long.MAX_VALUE(): Maximum value for a 64-bit signed integer. Long.MIN_VALUE(): Minimum value for a 64-bit signed integer.
B. Floating-Point Types (double)
Floating-point numbers (float and double) do not use simple two’s complement. They follow the IEEE 754 standard, which splits the bits into three components: a sign bit, an exponent, and a fraction (mantissa).
Because of this complex scientific notation structure:
Double.MAX_VALUE(): Represents the largest finite positive number the 64-bit double structure can hold before overflowing to Infinity.Double.MIN_VALUE(): Surprise! Unlike integer types, Double.MIN_VALUEis not negative. It represents the closest positive value greater than zero that adoublecan represent (denormalized minimum). The maximum negative number is simply-Double.MAX_VALUE.
Floating Point
Decimal to Binary
Add the fraction to itself and retain the integer until the entire sequence becomes 0
- https://youtu.be/SPw9f2eXh8Y?si=j9KcWX9Zus0mFbpL&t=418 https://www.electronics-tutorials.ws/binary/binary-fractions.html https://www.youtube.com/watch?v=K16C6736hh4
Normalization
Normalization is a technique used in computer architecture to standardize the representation of floating-point numbers.
There are two main types :
-
Explicit
- Move the radix point (dot) to the LHS of MSB.
- Move the radix point (dot) to the LHS of MSB.
-
Implicit
- Move the radix point (dot) to the RHS of MSB.
- Better one wrt precision