2.1. Data Types¶
2.1.1. Static vs. Dynamic Typing¶
Java is a statically typed language. When a variable or parameter is declared in a statically typed language, the data type for the value must be specified. Once the declaration is made, the variable or parameter cannot refer to a value of any other type.
1 2 3 4
let dynamicVariable = "dog"; console.log(typeof(dynamicVariable)); dynamicVariable = 42; console.log(typeof(dynamicVariable));
After line 1 executes,
dynamicVariable holds a
string data type. After
line 3 runs,
dynamicVariable becomes a
is allowed to hold values of different types, which can be reassigned as
needed when the program runs.
However, the corresponding code in Java will result in a compiler error:
String staticVariable = "dog"; staticVariable = 42;
error: incompatible types: int cannot be converted to String
The compiler error occurs when we try to assign
42 to a variable of type
Take-home lesson: We must declare the type of every variable and parameter in
a statically typed language. This is done by declaring the data type for the
variable or parameter BEFORE its name, as we did in the example above:
String staticVariable = "dog".
We only need to specify the type of a variable or parameter when declaring it. Further use of the variable or parameter does not require us to identify its type. Doing so will result in an error.
Dynamic and static typing are examples of different type systems. The type system of a programming language is one of the most important high-level characteristics that programmers use when discussing the differences between languages. Here are a few examples of popular languages falling into these two categories:
- Static: Java, C, C++, C#, TypeScript
Because we need to give plenty of attention to types when writing Java code, let’s begin by exploring the most common data types in this language.
2.1.2. Strings and Single Characters¶
Strings in Java are immutable, which means that the characters within a string cannot be changed.
126.96.36.199. Single vs. Double Quotation Marks¶
Java syntax requires double quotation marks when declaring strings.
Java has another variable type,
char, which is used for a single character.
char uses single quotation marks. The single character can be a letter,
digit, punctuation, or whitespace like tab (
String staticVariable = "dog"; char charVariable = 'd';
The table below summarizes some of the most common string methods available in
Java. For these examples, we use the string variable
String str = "Rutabaga".
||Returns the character at index 3, (
||Returns the characters from indexes 2 - 4, (
||Returns the length of the string.|
||Returns the index for the first occurrence of ‘a’, (
||Splits the string into sections at each
||Removes any whitespace at the beginning or end of the string.|
||Changes all alphabetic characters in the string to UPPERCASE or lowercase, respectively.|
||Searches for the specified text within a string and returns
||Compares strings for equality and returns a boolean.|
We will explore the differences between using
we discuss Java operators. For now, use
.equals() if you need to compare
2.1.3. Primitive Types¶
A primitive data type is a basic building block. Using primitive data types, we can build more complex data structures called object data types.
Java uses its own a set of primitive data types. The table below shows the most common types that beginners are likely to encounter. A more complete list can be found on the Oracle website.
||42||Represents positive and negative whole numbers.|
||3.141593 and 1234.567 and 2.0||Represents positive and negative decimal numbers with up to 7 digits.|
||3.14159265358979 and 10000.12345678912||Represents positive and negative decimal numbers with 15-16 digits.|
||‘a’ and ‘9’ and ‘n’||A single unicode character enclosed in single quotes
||Booleans in Java are NOT capitalized.|
As we will see in a later section, the
float data type sacrifices some
accuracy for speed of calculation. Thus, evaluating 1.11111 + 3 results in an
answer of 4.1111097 instead of 4.11111.
Anytime you need to perform calculations with decimal values, consider using
double type instead of
2.1.4. Non-primitive Types¶
Primitive data types are immutable and can be combined to build larger data
structures. One example is forming the
String “LaunchCode” from multiple
char characters (‘L’, ‘a’, ‘u’, etc.).
String is a non-primitive data type, also called an object type. As we
saw in the
String table above, object types have methods which we can call
using dot notation. Primitive data types do not have methods.
Primitive data types in Java begin with a lower case letter, while object data types in Java begin with a capital letter.
Later in this chapter, we will explore the Array and Class object types.
There may be situations when we call a method that expects an object as an argument, but we pass it a primitive type instead (or vice versa). In these cases, we need to convert the primitive type to an object, or convert an object type into a primitive.
In older versions of Java, it was the programmer’s responsibility to convert back and forth between primitive types and object types whenever necessary. Converting from a primitive type to an object type was called boxing, and the reverse process (object to primitive) was called unboxing.
1 2 3
int someInteger = 5; Integer someIntegerObject = Integer.valueOf(someInteger); ClassName.methodName(someIntegerObject);
- Line 1 declares and initializes the variable
- Line 2 converts the primitive
- Line 3 calls
someIntegerObjectas the argument. If
methodNameexpects an object type and we tried sending an
intinstead, we would generate an error message.
Let’s assume that a method returns a random number of
Integer type, and we want to combine it with a value of
1 2 3 4
int ourNumber = 5; Integer randomNumber = ClassName.randomNumberGenerator(); int randomInt = (int) randomNumber; int sum = ourNumber + randomInt;
- Line 2 declares and initializes
- Line 3 converts
intand stores the value in the
Converting between data types in order to pass values between methods quickly became tedious and error prone. In the newer versions of Java, the compiler is smart enough to know when to convert back and forth, and this is called autoboxing.
For us, the consequence of autoboxing is that in many situations, we can use primitive and object types interchangeably when calling methods or returning data from those methods.
It’s a best practice to use primitives whenever possible. The primary exception to this occurs when storing values in collections, which we’ll learn about in a future lesson.
Each of the primitive data types has a corresponding object type:
2.1.7. Check Your Understanding¶
Which of the following is NOT a number data type in Java:
Name the Java method responsible for checking string equality: