Lesson 4 — Data Types, Variables, and Comments

A simple intro to Java

Previous :: Next

Java Documentation

Eventually, when you start using an editor, many of them will do the simple things for you, but you should still know how to do them on your own. One place to go when you are confused is the Java Documentation. However, right now this might be a bit daunting with all the information there, so we will learn how to use data types and variables by looking at the docs so that we can cover two things at once.

Types of Variables

For right now, we will be talking about primitive data types so you and head over here and we can learn more about them. If you start reading this doc page, you can see that there are eight primitive data types, and even these are classified into three main smaller categories.

1. boolean

This is just a data type that holds true or false. These are use for checking and comparing inside if…else statements and various kinds of loops.

2. integral types

These are variable types that can hold a whole number or an integer. As you can see, there are many of them. The main difference between them is how much information they can hold. The more information that they can hold, the bigger the number that they can hold. For example, a short can hold more information than a byte, in other words, the largest number that a short can hold is bigger than a byte. While these differences might not mean a lot to you right now, it is critical for when you are trying to optimize your program.

  1. byte — 8-bit (-128 to 127)
  2. short — 16-bit (-32,768 to 32,767)
  3. int — 32-bit (-2,147,483,648 to 2,147,483,647)
  4. long — 64-bit (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)
  5. char — 16-bit unsigned integers (\u0000 to \uFFFF)

Now char is a bit different because it contains special numbers, which add up to a charter in the alphabet. This process changes the numbers to UTF-16 code units. However, you should still remember that a char is considered an integral type because it holds integers.

3. floating-point

Unlike the integral types, these can hold decimals. Again the difference between the two is how big of a number and how much data each can hold. These are used when you are trying to be more accurate. For example, if you were doing a science experiment and you had to do some calculations, you would use a floating-point type because they will give you the most accurate answer. However, sometimes when you are trying to optimize your programs, you will change some of these back into integral types because sometimes speed is better than accuracy.

  1. float — 32-bit (really big, please don’t make me do this)
  2. double — 64-bit (even bigger than float)

Declaring and Initializing Variables

There are some reels for variable names, setting them up, and putting values into it.

type name = data;
  • type

This is the type of the variable you are using. This is where you would put one of the eight primitive types we learned about.

  • name

This is the variable name. You can use any upper or lower case letters as long as there aren’t any spaces in-between. However, programmers typically name their variables in camel case. Camel case is when you capitalize the first letter of each word and keep the first word lowercase. For example, theQuickBrownFox is in camel case.

  • = data;

This is how you put data into the variable. You would change data with whatever information you are planning on putting into the variable. However, you do not need to put in data when you first make the variable. If you do that, then you would just end with a semicolon and then set the variable later on.

note: When initializing these variables, you cannot use commas in-between, but you can use underscores. Ex: 1,234 is not ok, but 1_234 is.

Comments

Comments allow for other people to understand why you wrote some code and what your code does. The computer will ignore your comments, that way you can leave messages for other people to read.

This is a line comment, and it allows you to tell the computer to skip that line of text and move on. A line comment only stops the computer from reading one single line and is typically used for small descriptions.

stop(); //Hammertime

Block comments are made with a slash and an asterisk in the beginning and an asterisk and a slash in the end. Block comments are used for bigger descriptions or for license info for when you are using someone else’s code. They are usually found at the beginning of your script tag, but can be used anywhere you want.

/* My program will end the world!!
*/
endTheWorld();

It is a good practice to use comments so that other people can follow your code clearly even after you have stopped working on it for a long time.

Got questions? Ask them below!

Previous :: Next