Lesson Weekend

We have some good news for you: Most of the types of information you've already worked with in JavaScript—Strings, Integers, Variables, Methods, Objects, etc.—function fairly similarly in Java. There are a few differences; but we know you'll catch on quickly!

This lesson will walk through these basic data types in Java. Along the way we'll emphasize any differences between Java and JavaScript. (But we think you'll be pleasantly surprised at how much you already know!)

Strings

Java Strings are very similar to the Strings we used in Intro to Programming: They're simply a sequence of characters between two quotation marks.

We can open our Java REPL to check this out by running the $ javarepl in the terminal (if you installed a REPL via Homebrew), or visiting the web-based Java REPL recommended in the previous lesson.

Once in the REPL we can define a basic String just like we did in Intro:

> "Hello World";
java.lang.String res0 = "Hello World"

(Do note that every line in Java must end with a semicolon. There are a few exceptions, but we won't worry about them at the moment. For now, always end lines in semi-colons.)

We can also concatenate strings together with the + operator, just like JavaScript:

> "Hello" + " " + "World";
java.lang.String res0 = "Hello World"

Easy enough!

Integers

Remember, Integers are simply a type of data representing whole numbers (numbers without decimals). Java Integers are also quite similar to those you worked with previously.

We can create an Integer in the REPL like this:

1;
java.lang.Integer res0 = 1

But do take note that this is different than:

> "1";
java.lang.String res1 = "1"

This second example is actually a String containing an Integer. Strings are the only type of data wrapped in quotation marks. So, if we put quotation marks around a number, it's actually a string. (You should already know this; but it's such a common error it's worth mentioning again!)

Arithmetic Operators

Also, the same four arithmetic operators we used in JavaScript work the same in Java. You should recall these basic operators:

  • + for addition
  • - for subtraction
  • * for multiplication
  • / for division

We can use each with Integers in the REPL, and see that we receive expected results:

> 1 + 1;
java.lang.Integer res1 = 2

> 1 - 1;
java.lang.Integer res2 = 0

> 2 * 2;
java.lang.Integer res3 = 4

> 4 / 2;
java.lang.Integer res4 = 2

Variables

Declaring variables does differ in Java. So, let's walk through this process in more detail. To create a Java variable we need three things:

  1. A descriptive name, so it's easy to tell what this variable represents.
  2. A data type, such as String, Integer or Object to define what type of data this variable will hold. (This is new. We didn't do this in JavaScript!)
  3. An initial value. Or, the information our new variable will hold. This value is set with the assignment operator (ie: the = symbol).

For example, we could create a variable called phrase that held a string reading "Hello World" like this:

String phrase = "Hello World";

In this example:

  • phrase is our descriptive name.
  • String is the data type our variable will be.
  • "Hello World" is the initial value.

After creating this variable, we can enter phrase into the REPL, and, as expected, Java will remember that our phrase contains "Hello World":

> phrase;
java.lang.String res0 = "Hello World"

We can also perform actions upon that variable. For example, we could use the concatenation operator on phrase like so:

> String phrase = "Hello World";
java.lang.String phrase = "Hello World"
> phrase;
java.lang.String res0 = "Hello World"
> phrase + "!";
java.lang.String res1 = "Hello World!"

Strictly-Typed Languages

But again, notice the examples above also define what type of data a variable will hold. We say String phrase = "Hello World"' not phrase = "Hello World"; Similarly, we could define a variable containing an Integer like this:

Integer number = 1;

Unlike JavaScript, Java variables must declare the type of data a variable will contain. This is because Java is a Strongly Typed Language, meaning it insists rigidly on keeping the data types consistent and explicit.

Methods

We wrote plenty of functions in Intro to Programming. This process is fairly similar in Java too, but in Java we actually call functions methods.

Methods are specific types of functions, so the umbrella term 'function' is still technically appropriate, but it's rarely used. Java is a compiled, class-based language in which everything is an Object. So functions don't generally exist unless they're part of an Object's definition. (Except some specific types of data called primitives. But don't worry about that yet. We'll explore them soon).

Here is a very basic method, written in Java:

 public String someMethod (String input){
   //do some stuff

   return "Hi, " + input + " nice to meet you."
 }
  • Here, our method is named someMethod().
  • Note that the two keywords preceding this name are very important.
    • Public denotes who is allowed to call this method. We'll learn more about this later.
    • String declares what type of information this method will return. As such, this must match the return type. Similar to the manner we must declare data types for variables, we must also declare data types for the value a method returns.

The following method, for instance, would not be valid:

 public Integer someOtherMethod (String input){
   //do some stuff

   return "Hey " + input + " , you look nice today."
 }

Why? Because it declares an Integer return type. But it actually returns a String object! This is absolutely not okay in Java's eyes.

Again, Java is a Strongly Typed Language. It insists rigidly on keeping the data types of variables and returns consistent. We can change data from one type to another, but we need to do this via dedicated methods and cannot just re-label values.

We couldn't, for example, get away with this:

 public Integer someInvalidMethod (String input){

input = 12; 
   return "I saw " + input + " kittens today."
 }

Java won't allow us to change the data type of the variable input from a string, to a number, back to a string via concatenation like this. Don't worry about how to change data from one type to another yet; we'll learn about that later. For now, just understand that Java data types must be consistent.

Naming Conventions

Before we wrap up, let's also address naming conventions.

Java variable and method names should always be written in a format called lower camel case. The first letter of the first word is lowercase, there are no spaces between words, and the first letter of any subsequent words are capitalized. Lower camel case should look something like this: myVariable, myMultipleWordVariableName, etc.

Also, always choose variables names that are explicit about what data is saved in them. Similarly, method names should explicitly describe what action the method is responsible for. This makes code cleaner, and easier to understand.