Get startedGet started for free

Primitive number data types

1. Primitive number data types

In the previous chapter, we learned how to store information in variables. Now, let's explore the different kinds of values we can store.

2. Primitive types

Java has eight built-in types designed to handle different kinds of data. They are called primitives. These are eight core data types from which all other data is constructed.

3. Numerical primitive types

In this video, we'll focus on these six that are used for storing numerical data. We'll cover the other two non-numerical primitives in the next video.

4. Primitives for whole numbers - int

We've already seen int, which is used for whole numbers or integers, such as the number of employees.

5. long, short, and byte

However, Java provides other types for whole numbers and different storage needs. If we need to store very large whole numbers, we use long, and if we want to save memory with smaller numbers, we can use short. If we need to store very small numbers, we choose byte.

6. int is the primitive of choice

However, most of the time, developers use int.

7. double

Java provides double for decimal numbers. This is the most commonly used type for floating-point or decimal values like 3.14 or 99.99.

8. float

There's also float, which takes up less space but is less precise.

9. double is the primitive for decimal numbers

In most cases, we use double for decimals because it provides greater accuracy.

10. Declaring primitives - int

We already saw that we can declare an int using the equal sign and the value.

11. Declaring primitives - int, double, short, byte

The same applies to double, short, and byte. These four numerical data types can be easily declared with an equal sign and their value.

12. Declaring primitives - float and long

However, if we want to use the float or long data type, the syntax is slightly different. We need to add F or L at the end of the number we are assigning to the variable, either in lower or upper case. Without this, Java gives an error.

13. Numerical primitives in action

We can use all of these six data types in our simple calculator from earlier, more about that later in the course.

14. Let's practice!

Now, let's get some hands-on practice defining and assigning primitive number variables!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.