Working With Data Types
Every Java variable has a data type that decides what it can hold (whole numbers, decimals, a single character, text, or true and false), and each type has its own way to write a literal value.
Novice · Java · Updated
A note about comments
Comments are lines of text in code that are ignored by the compiler. They are useful to add short explanations into the code.
There are 2 types of comments in Java.
// A single-line comment will comment the entire line
/* <-- begin block
A block comment will ignore all text
between the begin and end block
end block --> */
Data Types in Java
Java is a strongly typed language. This means that when you declare a variable, you must specify its data type. The specified data type determines what type of calculations you can perform with the variable. It also determines how much memory is allocated for that variable.
All data types fall into 5 general categories.
| Category | Java types |
|---|---|
| Whole numbers | byte, short, int, long |
| Floating-point numbers: numbers with decimal places, including very large or very small ones for science and math | float, double |
| A single character | char |
| Text: a word or a sentence | String |
| True or false | boolean |
All other Java code is based on these basic data types.
Working with Integers
In mathematics, an integer is a whole number. But in programming, there are several types of whole number. As a developer you must decide which data type to use. This is generally determined by how large or small the number is that you are working with.
| Type | Range |
|---|---|
byte |
-128 to 127 |
short |
-32,768 to 32,767 |
int |
-2,147,483,648 to 2,147,483,647 (the most common whole-number type) |
long |
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
Practice
In your java-basics project, create a class named DataTypes01 and print the range of each whole-number type, then run it:
System.out.println("byte: " + Byte.MIN_VALUE + " to " + Byte.MAX_VALUE);
System.out.println("short: " + Short.MIN_VALUE + " to " + Short.MAX_VALUE);
System.out.println("int: " + Integer.MIN_VALUE + " to " + Integer.MAX_VALUE);
System.out.println("long: " + Long.MIN_VALUE + " to " + Long.MAX_VALUE);
Working with Floating Point Numbers
A floating point number allows you to perform calculations with very large or small numbers. These floating point numbers support scientific notation.
| Type | Range (positive or negative) |
|---|---|
float |
1.4E-45 to 3.4028235E+38 |
double |
4.9E-324 to 1.7976931348623157E+308 |
double is the most commonly used floating-point numeric datatype.
Working with Text
Char
In Java, a single character is represented by surrounding it with a single quote. You may not place more than one character inside of a single quote.
char letterA = 'a';
String
A string is text that is made up of one or more characters. Strings are surrounded by double quotes, as seen in the example below.
String name = "Jane";
String poet = "Robert Frost";
String poem = "Two roads diverged in a yellow wood...";
The char data type cannot do much on its own. Its purpose is simply to store the value of a single character. A String, on the other hand, can hold many characters. There is a lot that we can do with strings. We will have an entire discussion just on string manipulation soon.
The boolean data type
Sometimes, you need to store the answer to a true/false or yes/no question in a variable. The boolean data type gives us this ability. It is the most basic of the data types we have discussed and is the correct data type for this situation.
boolean isAdult = true;
boolean hasDriversLicense = false;
Since programming is all about making decisions, the boolean data type is one that we will use frequently.
Type Literals
When declaring variables, you may need to hard-code the initial values. Java treats literal values as follows:
| Literal | Java treats it as |
|---|---|
12 |
int (a whole number is an int) |
12.50 |
double (a number with a decimal point is a double) |
'A' |
char |
"Jose" |
String |
true |
boolean |
If you need to use other data types, such as long or float, you must use a literal suffix to tell Java to treat the value as that data type. A suffix letter is added to the end of the value. Use an uppercase L for long: a lowercase l looks too much like the digit 1.
| Literal | Java treats it as |
|---|---|
3000000000L |
long |
12.50f |
float |
Examples
double price = 12.5;
// float requires the f suffix
float weight = 12.5f;
int age = 35;
// long requires the L suffix
long distance = 12000000000L;
String name = "Gary";
char letter = 'b';
boolean isExpensive = true;
Practice
Create a class named DataTypes03. Declare a variable, with the right data type, for each of these: a product's name, its price, how many are in stock, its size letter (S, M or L), and whether it's on sale. Print each one.
Do it: give each fact on a pizza ticket its type
Build-It Pizzeria's order ticket keeps four facts about each pizza. A new developer stored all four as String, because they all look like text on the ticket. But text can't be added up or used as true or false, so each fact needs the type made for it.
The four variables, the type each one needs, and the value it holds on this ticket:
| Variable | What it holds | Type | Value |
|---|---|---|---|
toppingCount |
how many toppings, a whole number | int |
3 |
price |
the price, a number with decimal places | double |
12.75 |
sizeLetter |
the size, one letter | char |
L |
isDelivery |
whether it's delivered, yes or no | boolean |
true |
main() prints the ticket. With the right types, it still prints exactly:
Size L, 3 toppings, $12.75, delivery: true
The ticket looks the same either way, which is why the mistake is easy to miss. The tests check each variable's type.
Your job: change the four declarations so each variable has the type and the value in the table.
- Change each variable's type from
Stringto the type in its row. - Write each value the way its type needs it, so it is no longer text.
- Leave the names and
main()as they are.
The box holds a short Java file: the variables and a main() method with nothing around them. Since Java 25, a small program can be written this way, and main() runs when the program starts.
Practiced live on the lesson page: write the Java right there and check it.