Join Peggy Fisher for an in-depth discussion in this video Understanding data types, part of Learning Java (2015).
- Before we can start making more advanced programs, we need to capture and store data in our programs. Java is known as a strict data typing language. That means that every variable that we define must be declared with a data type. And you can not change the type of data stored in that variable. For example, if you declare a variable to store numbers, you can not try to store letters in that same variable. Let's get started by reviewing the basic types of data we will encounter. The first is characters.
The character data type allows us to store a single character, a single number, or a symbol. The syntax in Java is char, sometimes referred to as char or char. A variable name, such as letterA = 'a' ;. Take particular notice of the single quotes. This is different than the literal or the strings we've used before that had double quotes. Next, we have a variable type called Boolean, which is named after a famous mathematician, George Boole.
It allows us to store a value of true or false. The Java syntax is bool, the variable name, such as result = either true or false. We have several data types that are used for storing numbers. They include integer data types, such as byte, short, int, and long. These data types are used for numbers that do not contain any fractional parts. For example, if you were counting the number of cars in a parking lot, you're not gonna have a half a car.
There are four different types of integers to store different sizes of numbers. The smallest one is the byte, which can hold signed values from minus 127 to positive 127 and unsigned values from zero to 255. The short, the int, and long allow for much larger numbers. To store rational numbers, which are numbers that include a fractional portion, such as 1.75, we use float and double.
For most of the exercises in these videos, I will be using int and double data types. Choose your data types carefully. For example, if you wanna keep track of an hourly wage, it is important to capture the fractional portion of the number. If the hourly wage is $10.95, and you worked 10 hours, you wanna get paid $109.50. But, if you store the hourly wage in an integer variable by mistake, you would only get paid $100 since the 95 cents would be lost.
The data types we just discussed are called primitive data types. The other type of data is known as structured data. And we will discuss that when we get to classes and objects. Primitive data types store their values directly in memory with the variable names. Structured data is stored by reference or by the address of the location and memory where the data exists. Let's think about a few more examples. I'll give you an example, and you tell me the correct data type. Again, how about the number of cars in a parking lot? If you said int, you're correct.
Now, how about the price of a grocery item? This should be double or float since it contains a decimal point. The vowel "a"? We use the data type character or char. The value "true" is boolean. And the number of students in classroom would be an integer. Again, you can't have half a student.
- Downloading and exploring NetBeans
- Understanding Java basics: data types, strings, arrays, and more
- Controlling flow with functions and loops
- Creating classes
- Sorting and searching arrays
- Manipulating files
- Handling errors
- Building GUIs