Section 3.1 Numeric
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.
With that distinction in mind, here are the common types youβll use, most of which are similar to Pythonβs types:
-
int: The primitive type for integers (whole numbers), such as 3, 0, and -76.
-
double: The primitive type for floating-point numbers like 6.3 or -0.9.
-
boolean: The primitive type that can only be true or false.
-
char: The primitive type for a single character, like βaβ or βZβ. It is represented using single quotes.
-
String: An object type that represents a sequence of characters in double quotes, like "Hello".
A data type fundamentally defines a set of values and the operations you can perform on them. For instance, you can do math with int and double values, but not with boolean values. This is simlar to Python, where you can perform arithmetic on integers and floats, but not on booleans or strings.
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 β the Javascript pop-up for input is only an artifact of the digital textbook.
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.
There are several new concepts introduced in this example. We will look at them in the following order:
Subsection 3.1.1 Import
In Java, you can use any class that is available without having to import the class, subject to two very important conditions:
-
You must use the full name of the class
Your first question might be how do the
java and javac commands know that certain classes exist. The answer is the following:
-
Java knows about all the classes that are defined in .java and .class files in your current working directory.
-
Java knows about all the classes that are shipped with Java.
-
Java knows about all the classes that are included in your
CLASSPATHenvironment variable. YourCLASSPATHenvironment variable can name two kinds of structures.-
A .jar file that contains Java classes
-
Another directory that contains Java class files
-
You can think of the import statement in Java as working a little bit like the
from module import xxx statement in Python. However, behind the scenes, the two statements actually do very different things. The first important difference to understand is that the class naming system in Java is very hierarchical. The full name of the Scanner class is really java.util.Scanner. You can think of this name as having two parts: The first part java.util is called the package and the last part is the class. Weβll talk more about the class naming system a bit later. The second important difference is that it is the Java class loaderβs responsibility to load classes into memory, not the import statementβs.
So, what exactly does the import statement do? What it does is tell the compiler that we are going to use a shortened version of the classβs name. In this example we are going to use the class
java.util.Scanner but we can refer to it as just Scanner. We could use the java.util.Scanner class without any problem and without any import statement, provided that we always referred to it by its full name. As an experiment, you may want to try this yourself. Remove the import statement and change the string Scanner to java.util.Scanner in the rest of the code. The program should still compile and run.
Subsection 3.1.2 Variable Declaration
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.
A valid variable name in Java can contain letters, digits, and underscores. It must begin with a letter, an underscore, or a dollar sign. It cannot start with a digit and it cannot be a reserved keyword (like class, int, or static). Variable names are case-sensitive, so
fahr and Fahr are different variables. The convention is to use lower case for variable names, and to use camel case (where the first word is lowercase and subsequent words are capitalized) for multi-word variable names, such as fahrenheitTemperature.
An important feature of Java is that when you declare a variable of a primitive type (like int or double), the system automatically allocates a fixed amount of memory to store its value directly. This is different from reference types (like String or Scanner), where the variable holds a memory address that points to the actual object data stored elsewhere. This distinction makes operations on primitives very fast.
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. In our temperature converter, the calculation (fahr - 32) * 5.0/9.0 works correctly because 5.0 and 9.0 are treated as double values, preventing the integer division that would occur if we had written 5/9, which would result in 0.
You have attempted of activities on this page.
