1.4. Java Data Types¶
1.4.1. Primitives¶
One of the great things about Python is that all of the basic data types are objects. Integers are objects, floating point numbers are objects, lists are objects, everything. In Java that is not the case. In Java, some of the most basic data types like integers and floating point numbers are not objects. The benefit of having these primitive data types be non-objects is that operations on the primitives are fast. The problem is that it became difficult for programmers to combine objects and non-objects in the way that we do in Python. So, eventually all the non-object primitives ended up with Objectified versions.
Primitive |
Object |
---|---|
int |
Integer |
float |
Float |
double |
Double |
char |
Char |
boolean |
Boolean |
In older versions of Java, it was the programmers responsibility to convert back and forth from a primitive to an object whenever necessary. This process of converting a primitive to an object was called “boxing.” The reverse process is called “unboxing.” In Java 5, the compiler became smart enough to know when to convert back and forth and is called “autoboxing.” In this book, we will typically use the Object version of all the numeric data types and let the compiler do its thing.
Let’s look at a simple Python function which converts a Fahrenheit temperature to Celsius. If this program were run on the command-line, you would enter the temperature when prompted.
1def main():
2 fahr = int(input("Enter the temperature in F: "))
3 cel = (fahr - 32) * 5.0/9.0
4 print("the temperature in C is: ", cel)
5
6main()
Next, lets look at the Java equivalent. If this program were run on the command-line, you would enter the temperature when prompted – the “Input for Program” text box is only an artifact of the digital textbook.
1import java.util.Scanner;
2
3public class TempConv {
4 public static void main(String[] args) {
5 Double fahr;
6 Double cel;
7 Scanner in;
8
9 in = new Scanner(System.in);
10 System.out.println("Enter the temperature in F: ");
11 fahr = in.nextDouble();
12
13 cel = (fahr - 32) * 5.0/9.0;
14 System.out.println("The temperature in C is: " + cel);
15 }
16}
There are several new concepts introduced in this example. We will look at them in the following order:
Variable Declaration
Import
Input/Output and the Scanner Class
Note
We have split this section of the reading across lectures, and so we will cover imports as well as Input/Output and the Scanner class next time.
1.4.2. Declaring Variables¶
Here is where we run into one of the most important differences between Java and Python. Python is a dynamically typed language. In a dynamically typed language a variable can refer to any kind of object at any time. When the variable is used, the interpreter figures out what kind of object it is. Java is a statically typed language. In a statically typed language the association between a variable and the type of object the variable can refer to is determined when the variable is declared. Once the declaration is made it is an error for a variable to refer to an object of any other type.
In the example above, lines 5—7 contain variable declarations.
Specifically we are saying that fahr
and cel
are going to
reference objects that are of type Double
. The variable in
will
reference a Scanner
object. This means that if we were to try an
assignment like fahr = "xyz"
the compiler would generate an error
because "xyz"
is a string and fahr
is supposed to be a double.
For Python programmers, the following error is likely to be even more
common. Suppose we forgot the declaration for cel
and instead left
line 6 blank. What would happen when we type javac TempConv.java
on
the command line?
TempConv.java:13: cannot find symbol
symbol : variable cel
location: class TempConv
cel = (fahr - 32) * 5.0/9.0;
^
TempConv.java:14: cannot find symbol
symbol : variable cel
location: class TempConv
System.out.println("The temperature in C is: " + cel);
^
2 errors
When you see the first kind of error, where the symbol is on the left side of the equals sign, it usually means that you have not declared the variable. If you have ever tried to use a Python variable that you have not initialized the second error message will be familiar to you. The difference here is that we see the message before we ever try to test our program. More common error messages are discussed in the section Common Mistakes.
The general rule in Java is that you must decide what kind of an object your variable is going to reference and then you must declare that variable before you use it. There is much more to say about the static typing of Java, but for now this is enough.
1.4.3. String¶
Strings in Java and Python are quite similar. Like Python, Java strings are immutable. However, manipulating strings in Java is not quite as obvious since Strings do not support an indexing or slicing operator. That is not to say that you can’t index into a Java string, you can. You can also pull out a substring just as you can with slicing. The difference is that Java uses method calls where Python uses operators.
In fact, this is the first example of another big difference between Java and Python. Java does not support any operator overloading. Table 3 maps common Python string operations to their Java counterparts. For the examples shown in the table we will use a string variable called “str”
Python |
Java |
Description |
---|---|---|
|
|
Return character in index 3 (remember strings are 0-indexed, so this is the 4th character) |
|
|
Return substring from index 2 up to but not including index 4 |
|
|
Return substring from index 2 to the end of the string |
|
|
Return the length of the string |
|
|
Find the first occurrence of x |
|
|
Split the string on whitespace into a list/array of strings |
|
|
Split the string at |
|
|
Concatenate two strings together |
|
|
Remove any whitespace at the beginning or end |