[JAVA-2a] Building a Simple Calculator: Types and Variables
By: Jack Boyuan Xu
Tags: USC, Java, Programming, Beginner, Viterbi

Last time we successfully said hello in Java, much like the original Macintosh in 1984 apart from almost every single way. We also came across a number of unfamiliar terms that we skimmed over. Let’s try to understand some of those terms by building a simple command line calculator app.

Overview

Think about how a calculator works. We type in some number, then the arithmetic operation we want to perform, after that another number, and finally the equal sign. Because we are building a simple calculator, we are only concerned with the four basic types of arithmetic operations: Addition, subtraction, multiplication, and division. We also don’t care about chaining operations such as 1+2+3+4=? . We only want to do 1+2=? .

We can try to generalize and streamline how a simple calculator works:

Type in first number -> Choose the arithmetic operation -> Type in second number -> Press equal -> Print results.

Let’s go ahead and make it happen in Java! How hard can it be?

Types and Variables

Have you ever gone to a public restroom? If you have, you may have noticed that there are, generally speaking, two types of restrooms: Male and Female. Let’s assume every person fits into one of those two categories, then we can say there are two types of people in the world: Male and female. Let’s hold this thought for a bit.

Do you have a name, hopefully? If so, chances are your name is not unique. For example, my legal name, Boyuan Xu, is also the name of some character in a novel called “The King’s Avatar”. We can say a name is only a designation — for the same name, we could be referring to different things. Combined with what we said in the last paragraph, we can then try to describe some person using the combination “(Type) — (Name)”. For example, you would describe me as “Male — Boyuan Xu”.

But since our names are not unique, “Male — Boyuan Xu” can easily refer to the character in the novel as well. The point is, even with the same type and name, the actual content can still differ. This is pretty much what a variable is in programming: something with a specific type and name that can have different contents.

Unsurprisingly, types in Java do not consist of Male and Female. Here is a list of primitive data types, aka the most basic types a language supports, in Java:

byte : The smallest unit, can represent values from -128 to 127. Binary.

: The smallest unit, can represent values from -128 to 127. Binary. boolean : Literally either true or false. That’s it.

: Literally either true or false. That’s it. char : A single character. ( A or , )

: A single character. ( or ) double : Use it for decimals. ( 0.123456789 )

: Use it for decimals. ( ) float : We can also use it for decimals but it has less precision compared to double. Just use double.

: We can also use it for decimals but it has less precision compared to double. Just use double. int : An integer. ( 12345 )

: An integer. ( ) short : A smaller integer used for when you need to save space.

: A smaller integer used for when you need to save space. long : A bigger integer when the number gets too big.

Out of all those, we are only concerned with boolean , double , and int for now. Don’t worry about the others. But wait: where is String ? Is it not a primitive data type? How did we print out “Hi, I am ____” last time? In most languages, string is considered to be a primitive data type but not in Java. In Java, a String is considered to be a simple object instead which is why its type begins with an uppercase ‘S’ instead of a lowercase one. We can see it as a primitive type most of the time — String is a special case and it really doesn’t matter too much apart from one sneaky inconvenience which we will cover later.

Now that we know about types in Java, let’s go ahead and declare a variable. We want to create an integer named favoriteNumber with an initial value of 7. Let’s do it:

int favoriteNumber = 7;

Not too difficult, right? We can then print out my favorite number by doing:

System.out.println(favoriteNumber);

When you are referring to a variable after its declaration, you only need to call it by its name, much like how others don’t call me “Male — Boyuan Xu” all the time. You can probably see why when we said hello in Java, we had to encapsulate our sentence with double quotes — anything without single or double quotes, Java sees it as a variable name.

If we want to increase the number, type the following after the previous print statement:

favoriteNumber++;

This means we want to increment the integer by 1. Similarly, we can decrement the integer by 1 by replacing the plus signs with minus signs. Print out the integer once more and you can see that it indeed increased by 1 and went from 7 to 8. Another way to increment the integer by any amount is by typing:

favoriteNumber = favoriteNumber + 9;

Or the shorthand:

favoriteNumber += 9;

We have just increased our favorite number by 9.

We can also completely replace our favorite number by doing:

favoriteNumber = 6;

Now our new favorite number is 6.

You cannot replace the variable contents with something of a different type, for example favoriteNumber = “Hello” .You cannot declare a new variable of the same name even if the types are different. Try it and see what happens!

Here is a screenshot of how thing should work after all the above: