"You've already mastered the basics of Java syntax, so now I want to give you some more details."
"Today, we'll talk about primitive types and how much memory they occupy. This knowledge will come in handy, maybe even today. Here are the basic types:"
|Value range||Default value||Description|
|byte||1||-128 .. 127||0||The smallest integer, 1 byte|
|short||2||-32,768 .. 32,767||0||Short integer, 2 bytes|
|int||4||-2*109 .. 2*109||0||Integer, 4 bytes|
|long||8||-9*1018 .. 9*1018||0L||Long integer, 8 bytes|
|float||4||-10127 .. 10127||0.0f||Fractional number, 4 bytes|
|double||8||-101023 .. 101023||0.0d||Fractional number that is twice the size of a float, 8 bytes|
|boolean||1||true, false||false||Boolean type (only true or false)|
|char||2||0..65,535||'\u0000'||Characters, 2 bytes, all unsigned values|
|Object||4||Any reference or null.||null||Stores references to instances of Object or classes that descend from Object|
"Let me tell you more about each type."
"The byte type is the smallest integer type. Variables of this type occupy just 1 byte of memory. A byte can store values in the range between -128 and 127."
"Why do we need such a small type? Why can't we always use int?"
"We could. But if you're creating large arrays whose elements never need to store values greater than 100, why not use this type? Does that make sense?"
"A short is twice as long as a byte, and it also only stores integers. The biggest positive number it can store is 32,767. The biggest negative number it can store is -32,768."
"The int type you are already familiar with. It can store integers in the range ±2,000,000,000."
"The float type was created to store real (fractional) numbers. Its size is 4 bytes."
"Fractional numbers are stored in a rather interesting form."
"For example, the number 987654.321 can be represented as 0.987654321*106. This means that it can be represented as two numbers in memory: 0.987654321 (mantissa, or significand) and 6 (base-10 exponent)."
"What do we need that for?"
"This approach lets us use 4 bytes to store numbers much greater than what an int can store. To do this, we have to sacrifice accuracy. Only a portion of those bytes are used to store the mantissa, which means that these numbers only store 6-7 decimal places. Less significant decimal places are discarded."
"These numbers are also called floating-point numbers. This is where the name the float type came from."
"The double type is similar to float, but twice as long (hence the name), taking up 8 bytes. It can accommodate a larger mantissa and more significant digits. If you need to store real numbers, always try to use this type."
"char is a hybrid type. Its values can be interpreted both as numbers (which can be added or subtracted) and characters. This is possible because even if characters have a visual representation, the computer mainly sees them as numbers. And it's more convenient to treat them as numbers. One more thing: the char type is always positive. It can't hold negative values."
"The boolean type is a logical type that can store only two values: true or false ."
"Despite its presence in this chart, the Object type isn't a primitive type. It's the base class for all classes in Java. First, all classes are derived from it and therefore contain its methods. Second, an Object variable can store references to objects of any type, including null (a null reference)."
"I've learned a lot today. Thank you for the lesson, Rishi."
GO TO FULL VERSION