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 , and the smallest is -2147483648, which is . Here's an example of using the int number:
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:
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:
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:
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.
What's interesting to note is the immutability of String in Java. Once a String is created, its value can't be changed.
