• 15 hours
  • Easy

Free online content available in this course.

course.header.alt.is_video

course.header.alt.is_certifying

Got it!

Last updated on 3/28/24

Understand data types to store your values

Why do we need data types?

We explored a little bit about data types in the last chapter, but there's so much more to know! Why do you need data types in the first place, though? 

First off, the only way to declare a variable in Java is to specify its type directly. Let's look at the following declaration of the variable count :

Declaring a variable
Declaring a variable

Start with the type, then the name, then the value. If you don't have a value to assign to your variable at the time of declaration, you still have to use the keyword. If you want to count words in a sentence, but don't know which sentence yet, indicate the type and declare the variable for future use. However, when you declare a variable by specifying its type only, the variable cannot be used until it is assigned a value. In programming, it's often the case that you need to declare a variable knowing that you'll assign a value to it later in the program.

If we can't use a variable without a value, why would we need to specify the type at all? Why not just the value? 

When running a program, your CPU (central processing unit) will need to know how much space to reserve for your variable. Declaring it with a type allows your CPU to save that space in memory for your variable depending on what type it is.

We will be exploring primitive data types which are types that exist on their own, kind of like atoms. The simplest types, they are used as a foundation for all computer operations. You can also combine them to construct more complex types, as we will see in the next chapter. We often use numeric types, and strings. Let's check them out!

Numeric types

These are:

  • Integers, which are whole numbers, like the numbers you use to count (1, 2, 3).

  • Decimals, like those you may use to store money values (2.50 or 5.99).

Let's start with one you've gotten a little familiar with: integers! Integers are declared like any other variable, with the type, then the name, then, if you have it, the value: 

int count = 10;

You recognize that type keyword  int  from last chapter, right? That's because you declare integers with the type keyword int . That's right, your variable count has an integer type.  As you can see, its value is 10, which is, of course, an integer. 😉  

For decimals, Java uses two types:

  • float

  • double

They both serve the same purpose. The difference is that double is twice as precise as float, meaning that it will record far more place values of a decimal number. If you needed to store something large, like a 12 digit number  1876.79797657 , this would fit exactly into double, while float could only hold half of that. The rest of it would be truncated and lost forever!

These are declared and initialized as follows:

float length = 1876.79;
double width = 1876.79797657;

What happens if you supply larger values?  For example:

double a = 1876.79797657765609870978709780987;
float b = 1876.79797657765609870978709780987;

They will both lose some precision, but not the to the same degree:

// a -> 1876.797976
// b -> 1876.79

With smaller values, it makes no difference:

float length = 10.0;
double width = 10.0;

In the example above, both variables hold small values. We are just using double instead of  float in the second statement. As it doesn't make any functional difference, in most languages you would be encouraged to use the smaller one, since using a bigger box to store small values wastes memory! 

Mixing numeric types

An important thing to keep in mind is how the types mix together and what consequences it may have. For example, if you divide two numbers: 5/2, you would logically expect the result to be 2.5. To the computer, however, it's not that obvious and you might unexpectedly get 2 instead!

Let's review a few examples in Java:

int a = 10;
int b = 4;
int c = a/b; 

Guess what the result of the division will be?

Well, it's 2 - not necessarily what you'd expect. Variables a and b were assigned integer numbers (whole numbers), so the division operation can only provide an integer as an answer.

Let's explore this concept further.

What do you think the expected result of this variation would be ?

int a = 10;
int b = 4;
double c = a/b; 

 This time around, c is going able to store a decimal number. However, the a/b expression still divides an int with another int . So c will be assigned the result of the integer division. That's 2, again.

The next thing you need to know is how to work with variables of different types that need to be used together for an action. For example:

int a = 10;
double b = 4;
int c = a/b;

See how one variable has been declared an int and the other a double ? 

  1. The result of the a/b expression will be  2.5 

  2. However, c is declared as an int and cannot be assigned a decimal value. This code just won't work at all!

What if you change your mind and want to make b perform like  an int variable in the middle of the program?

You can make a variable of one type act like another type. This is called type casting. To solve the problem we had in the last example, make variable b pretend that it's an integer by assigning it to 'c' like below:

int a = 10;
double b = 4;
int c = a/ (int) b; //-> c contains 2, as a/(int) b is an integer division

See how we made variable b act like an integer? You can also make an integer variable b pretend that it's value is a  double :

int a = 10;
int b = 4;
double c = a/(double) b; //-> c contains 2.5, as the value in b is casted to a double 

Casting one variable to double lets you perform a floating point division even if you are using variables with an int type.

Strings

Next, we are going to check out a more sophisticated type: string. Strings allow for storing text, or in other words, a set of characters. Here's how to declare a string variable in Java:

String city = "New York";
String movie = "Best ever";
String pet;
String emptyString = "";

One of the common actions you can perform on strings is merging one or more of them together. Let's join some strings together:

String firstFavoriteCity = "New York";
String secondFavoriteCity = "Buenos Aires";

String favorites = firstFavoriteCity + secondFavoriteCity; // -> "New YorkBuenos Aires"

Notice, there is no space in between. Let's make it more readable by concatenating strings and variables:

String firstFavoriteCity = "New York";
String secondFavoriteCity = "Buenos Aires";

String favorites = "My favorite cities are " +firstFavoriteCity+ " and "+secondFavoriteCity; // -> "My favorite cities are New York and Buenos Aires"

Much better now! You can also concatenate other data types with strings, such as numbers. How do you do that?

String favoriteCity = "Buenos Aires";
int numberOfTrips = 5;

String story = "I've traveled to " +favoriteCity+ " " +numberOfTrips+ " times!"; // -> "I've traveled to Buenos Aires 5 times!"

In the last section, we used the+ operator to add two numbers. Here, with strings, the  +  operator can be used to concatenate strings and integers together. Concatenation refers to joining strings or numbers and strings together. 

Regulations

We've mentioned this before, but it bears repeating: variables always have a type.  Java requires the type to be specified when you declare a variable. If you need to use the content of a variable as if it was of another type, you can use type casting.

Summary

In this chapter, you've learned the specifics of a few simple data types:

  • integers ( keyword  int )

  • decimals (keywords  float  and  double )

  • strings (keyword  string )

You've also learned how to manipulate these types:

  • You can perform numeric operations on numbers of the same type.

  • To use numbers of different types in operations together, cast them to behave like the needed type. 

  • Strings can be joined together. This is called concatenation. 

Example of certificate of achievement
Example of certificate of achievement