Primitive datatypes are types of data built-in to the Java system.
Declaring a variable requires that we specify the type and name:
// datatype variableName
int age;
double salaryRequirement;
boolean isEmployed;
These variables don’t have any associated value. To assign a value to a variable, we use the assignment operator =:
int year = 1995;
int
ints hold positive numbers, negative numbers, and zero. They do not store fractions or numbers with decimals in them.
The int data type allows values between -2,147,483,648 and 2,147,483,647, inclusive.
double
The double primitive data type can hold decimals as well as very large and very small numbers. The maximum value is 1.797,693,134,862,315,7 E+308, which is approximately 17 followed by 307 zeros. The minimum value is 4.9 E-324, which is 324 decimal places!
boolean
true false
char
How do we answer questions like: What grade did you get on the test? What letter does your name start with?
The char data type can hold any character, like a letter, space, or punctuation mark.
It must be surrounded by single quotes, '
String
So far, we have learned primitive data types, which are the simplest types of data with no built-in behavior. Our programs will also use Strings, which are objects, instead of primitives. Objects have built-in behavior.
Strings hold sequences of characters. We’ve already seen instances of a String, for example when you printed out "Hello World".
Just like with a primitive, we declare the variable by specifying the type first:
String greeting = "Hello World";
Static Checking
The Java programming language has static typing. Java programs will not compile if a variable is assigned a value of an incorrect type. This is a bug, specifically a type declaration bug.
Bugs are dangerous! They cause our code to crash, or produce incorrect results. Static typing helps because bugs are caught during programming rather than during execution of the code.
The program will not compile if the declared type of the variable does not match the type of the assigned value:
int greeting = "Hello World";
The String "Hello World" cannot be held in a variable of type int.
For the example above, we see an error in the console at compilation:
error: incompatible types: String cannot be converted to int
int greeting = "Hello World";
When bugs are not caught at compilation, they interrupt execution of the code by causing runtime errors. The program will crash.
Java’s static typing helps programmers avoid runtime errors, and thus have much safer code that is free from bugs.
Review
Creating and filling variables is a powerful concept that allows us to keep track of all kinds of data in our program.
In this lesson, we learned how to create and print several different data types in Java, which you’ll use as you create bigger and more complex programs.
We covered:
-
int, which store whole numbers
-
double, which store bigger whole numbers and decimal numbers
-
boolean, which store true and false
-
char, which store single characters using single quotes.
-
String, which store multiple characters using double quotes.
-
Static typing, which is one of the safety features of Java
-
Variable naming conventions.