Diego De La Puente

Variables and Types

Although Java is object oriented, not all types are objects. It is built on top of basic variable types called primitives. There are a total of eight data types; six are numeric, one is Boolean and one is character based.

https://www.learnjavaonline.org/en/Variables_and_Types

 

Numeric Primitives

To understand the relationship between space and numeric ranges clearly understand bit & byte clearly, below:

 

https://www.computerhope.com/jargon/b/bit.htm

 

Character sets

Character sets used today in the US are generally 8-bit sets with 256 different characters.

One bit can have 2 possible states.  21=2.  0 or 1.
Two bits can have 4 possible states.  22=4.  00,01,10,11. (i.e. 0-3)
Four bits can have 16 possible states.  24=16.  0000,0001,0010,0011, etc. (i.e. 0-15)
Seven bits can have 128 possible states.  27=128.  0000000,0000001,0000010, etc. (i.e. 0-127).

Eight bits can have 256 possible states.  28=256.  00000000,00000001,00000010, etc. (i.e. 0-255).

Eight bits are called a byte.  One byte character sets can contain 256 characters. The current standard, though, is Unicode which uses two bytes to represent all characters in all writing systems in the world in a single set.

https://web.cortland.edu/flteach/mm-course/characters.html#:~:text=(i.e.%200%2D255).,world%20in%20a%20single%20set.

My Example to myself:

int can store 4 bytes (8 bits per byte x 4 = 32 total bits of storage space). To calculate the maximum range of values take 2 to the power of 32. The reason we are using 2 as our base is because a bit can only have one of two states; either ON or OFF.

232 = 4,294,967,296 possible different combinations

The creators of the Java programming language wanted to give an integer range (meaning positive and negative whole numbers). So they took that amount (4,294,967,296 possible different combinations) and they halved it to have the possible range be half positive of the 0 and the other half negative of the 0.

Half value = 2,147,483,648

 

In a perfect world the negative value would be a max of 2,147,483,648 and the positive value would be 2,147,483,648. But since even the negative symbol takes up a position of memory the creators of the Java Programming language decided to borrow a possible position from the positive side and use it as the negative symbol, for that reason the maximum positive value is 2,147,483,648 vs. 2,147,483,647

 

This is how int has a storage space of 4 bytes with a range between -2,147,483,648 to 2,147,483,647.

 

Six Numeric Primitives

There are six numeric primitive data types four integer and two floating point:

·        byte (storage space = 1 byte)  (Numeric Range = -128 to 127)

·        short (storage space = 2 bytes)  (Numeric Range = -32,768 to 32,767)

·        int (storage space = 4 bytes) (Numeric Range = -2,147,483,648 to 2,147,483,647)

·        long (storage space = 8 bytes) (Numeric Range = -9,223,372,036,854,775,808 to 9,223,372,036,854,775,80)

·        float (storage space = 4 bytes) (Numeric Range = 7 decimal digits)

·        double (storage space = 8 bytes) (Numeric Range = 16 decimal digits)

https://www.i-programmer.info/ebooks/134-modern-java/5423-java-data-types-numeric-data.html

 

 

Floating Point Numeric Primitives

The two floating point primitives require further clarification.

*This is something we have to accept blindly for the numeric counts

*Also know that there is a tradeoff between whole number and decimals; meaning the greater the whole number the smaller the decimal precision becomes. And vice versa, meaning the greater the decimal precision the small the whole number that you can allocate.

*Long story short, the numeric value must be within this limit:

Float: 7 to 8 possible numeric positional values – it doesn’t matter how many values we have before or after the decimal overall it is a maximum of Float is 7 to 8 positions of Precision.

 

 

Double: 16 possible numeric positional values – it doesn’t matter how many values we have before or after the decimal overall it is a maximum of Double is 16 positions of Precision.

 

 

Note: Both float and double data types were designed especially for scientific calculations, where approximation errors are acceptable. If accuracy is the most prior concern then, it is recommended not to use these data types and use BigDecimal class instead.

https://www.geeksforgeeks.org/data-types-in-java/

 

Default Primitive Data Type Values:

Type                                           Default Value

byte                                            0

short                                           0

int                                               0

long                                            0

float                                            0.0f

double                                        0.0d

char                                            '\u0000'

boolean                                      false

String or other object                   null

 

https://cs-fundamentals.com/java-programming/java-primitive-data-types

 

Non-Numeric Primitive Data Types:

 

 

Oracle Primitive Data Type Documentation:

·        byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of documentation.

·        short: The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with byte, the same guidelines apply: you can use a short to save memory in large arrays, in situations where the memory savings actually matters.

·        int: By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of -231 and a maximum value of 231-1. In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 232-1. Use the Integer class to use int data type as an unsigned integer. See the section The Number Classes for more information. Static methods like compareUnsigneddivideUnsigned etc have been added to the Integer class to support the arithmetic operations for unsigned integers.

·        long: The long data type is a 64-bit two's complement integer. The signed long has a minimum value of -263 and a maximum value of 263-1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264-1. Use this data type when you need a range of values wider than those provided by int. The Long class also contains methods like compareUnsigneddivideUnsigned etc to support arithmetic operations for unsigned long.

·        float: The float data type is a single-precision 32-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead. Numbers and Strings covers BigDecimal and other useful classes provided by the Java platform.

·        double: The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.

·        boolean: The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its "size" isn't something that's precisely defined.

·        char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html