Look Good In Front Of Your Coworkers By Acing This Quiz on Java Basics

Whether you’re new to Java, returning after a break, or a seasoned user, everyone can benefit from a SURPRISE QUIZ now and again. Don’t worry, I promise it won’t be bad. For this one, we’re just going to review some basic concepts — namely, data types and variable declaration.

Name each data type in Java and how they differ.

Primitive data types are included in the Java language. Non-primitive types are not included but, rather, are accessed through Java libraries.

Name the eight primitive data types.

Integer numerics:

  • byte – 1 byte with a range of -128 to 127
  • short – 2 bytes with a range of -32,768 to 32,767
  • int – 4 bytes with a range of -2,147,483,648 to 2,147,483, 647
  • long – 8 bytes with a range of -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Floating-point numerics:

  • float – 4 bytes with a precision of 6-7 decimal places
  • double – 8 bytes with a precision of 15 decimal places

Characters:

  • char – 2 bytes, stored as an integer code

Booleans:

  • boolean – non-numeric true or false

Why are there so many of them?

Java offers many different types of numerical values and pays close attention to their size. While 4 bytes here and there may not seem like a ton of memory, when spread across a database of a few million lines of code it starts to add up very quickly. For example, if you wanted to store someone’s middle initial, you would use a char. If you wanted to store the year someone was born, you would use a short.

With that in mind, it is important to pay close attention to variable declarations throughout different projects in order to use as little memory as is necessary.

If you’re worried about memorizing all of this size and range information, don’t stress about it too much. The most popular numeric types are int and double. Aside from knowing those two, you can just reference this handy chart if you get stuck on the other values.

BONUS ROUND:

How do we declare variables?

Now that we know these data types, let’s see what they would look like in action. In Java it’s necessary to declare a variable type — along with its name — before you can use it elsewhere.

int year            // this works 
int year = 2019     // this too

Why do we use the final keyword?

Final is used with a value that we want to be assigned only once. Remember when we talked about storing the value of a year with a short? If we were storing someone’s user profile that included their birth year, we would not want that value to change. It doesn’t matter how often someone pretends to be older or younger, the piece of data is final! 🙂

Another great example of the use of final is with mathematical constants. Let’s say that you were writing a class called Circle. It has fields like radius, diameter, area, and pi. The first three values would change depending on the values input by the user. However, pi would never change so you would want that variable declaration to be preceded with final.

Resources

Congratulations! You made it to the end of the pop quiz. Whether you got a perfect score or need to brush up on a concept or two, here are some great items to check out if you want to learn more.

Links: