Lesson 3

Java Basics: Exploring Primitive Data Types and Strings

Introducing Numerical Data Types

In Java, we use numerical data types to represent numbers. Specifically, in this lesson, we're focusing on int and float. The int data type is used to represent whole integer numbers, and the float data type is used to represent decimal numbers - numbers with a decimal point.

The largest value an int can store is 2147483647, which is 23112^{31} - 1, and the smallest is -2147483648, which is 231-2^{31}. Here's an example of using the int number:

Java
1int daysInWeek = 7; 2System.out.println(daysInWeek); // This will print: 7 3 4int maximalInteger = 2147483647; 5System.out.println(maximalInteger); // This will print: 2147483647 6 7int tooBigInteger = 2147483648; // Oops! Error, the number is too large

Now, let's move on to the float data type. float is used when dealing with numbers that have decimal points, also known as floating-point numbers. A float can hold up to 7 decimal digits of precision. Consider the following example:

Java
1float pi = 3.1415926f; 2System.out.println(pi); // This will print: 3.1415926

Ensure that the f at the end of the number is present; it differentiates a float from a double - another type for decimal numbers, with higher precision.

Discovering Boolean and Char Data Types

Now, let's move on to the boolean and char data types.

The boolean data type in Java can hold one of two possible values: true or false. This data type is widely used in logical expressions and decision-making. Here's a simple example:

Java
1boolean isEarthRound = true; 2System.out.println(isEarthRound); // This will print: true 3 4boolean isEarthFlat = false; 5System.out.println(isEarthFlat); // This will print: false

The char data type is used to store a single character. Java uses Unicode, allowing a char to store any character! Here's how it's done:

Java
1char firstLetterOfAlphabet = 'A'; // A character is always surrounded by single quotes 2System.out.println(firstLetterOfAlphabet); // This will print: A
Exploring String Data Type

You'll find that String is as common in Java as stars in the cosmos. Java treats String as a basic data type and uses it to store a sequence of characters - just a text. The string is always surrounded by double quotes.

Java
1String welcome = "Welcome to Java!"; 2System.out.println(welcome); // This will print: Welcome to Java!

What's interesting to note is the immutability of String in Java. Once a String is created, its value can't be changed.

Understanding null

As we conclude this journey, we will discuss a very special value: the null value. null means "no value" or "nothing", or "unknown". It's not equivalent to an empty string ("") or 0.

Here's how you assign null to a variable:

Java
1String unknown = null; 2System.out.println(unknown); // This will print: null

Note: as null is nothing, you can't perform any operations on it, even though it seems to be a String in the example above. You can still print the null variable or reassign it to an actual value, but you can't perform any other operations on it; they will cause an error known as NullPointerException. But no worries, we will cover this in detail in the next lessons!

Lesson Recap and Practice Announcement

Bravo! You've successfully navigated the basic data types in Java. You can now use int and float for numerical computations, boolean for decision-making, char to represent single characters, String to handle texts, and null to represent an unknown value.

Although we've covered a lot, more practice is on the horizon. It's meant to further consolidate your understanding. So, gear up for the exercises designed to put your newfound knowledge to the test!

Enjoy this lesson? Now it's time to practice with Cosmo!

Practice is how you turn knowledge into actual skills.