"Hi, Amigo!"

"Hi, Rishi!"

"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:"

Type Size,
bytes
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."

"I see."

"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."

undefined
10
Task
New Java Syntax, level 10, lesson 2
Locked
Cat carnage (1)
You have an empty Cat class created by students at the secret CodeGym lab. A cat (Cat class) must have a name (String name), age (int age), weight (int weight), and strength (int strength).
undefined
10
Task
New Java Syntax, level 10, lesson 2
Locked
Cat carnage (2)
Your job is to realize cats in the flesh — in the image and likeness of the Cat class, or more accurately, using it as a template. The number of cats shall be three. Fill these three with life, or in other words, specific data.
undefined
10
Task
New Java Syntax, level 10, lesson 2
Locked
Cat carnage (3)
Implement the boolean fight(Cat anotherCat) method: think up a mechanism for cat fighting depending on their weight, age, and strength. Compare each attribute separately so that the winner is the one with the best performance in regards to the most attributes. The method should determine whether the
undefined
10
Task
New Java Syntax, level 10, lesson 2
Locked
Cat carnage (4)
Using the three created cats, hold three fights between pairs of cats. To hold a fight, use the boolean fight(Cat anotherCat) method. Display the result of each fight on a new line.