Return to main page
View source or report issues on GitHub

Lesson 1.2 - Variables and Data Types

Lessons 1.3 in both CodeHS and CS Awesome cover many of these same ideas, but these notes go into a bit more formal detail. There are good practice problems in both of those tools!

At the lowest level, all informaton (or data) on a computer is just a string of 0’s and 1’s. It is very important that a string of 0’s or 1’s will be interpreted correctly - does it represent a number? A word? A command?

One of the ways our computer programs know how to interpret data in the context of a programming language is by the data type:star: of the data, which is set in the program. This defines the specific type of information held in that piece of data.

In a program, there are three things that have types:

Variables

A variable is basically a name that refers to a spot in your computer’s memory where you can hold data of a specific type. The value can be changed later.

In order to use a variable in java, you need to declare:star: the variable by typing first the type of the variable, then its name, then an OPTIONAL section where you set its initial value with an equals sign (called initializing the variable:star:). Once a variable has been named an initialized, you can change its value by assigning a new value to it, for example with the code a = 3; which would change the value of the variable a to 3.

To make the variable a constant, so it cannot be changed, you use the keyword final first.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int numApples;
   // makes a new variable named numApples that can hold an integer.
   // but it is not currently initialized
numApples = 3; // now it is initialized to 3!

int numMangoes = 17; // create and initialized in one line!

final int numOranges = 6;
   // makes a new variable called numOranges that can hold an integer.
   // initializes the value to 6
   // declares that we can't change it.

numApples = 5; // assigns the value 5 to the already existing numApples
numMangoes = numApples; //now numMangoes is 5 too!


numOranges = 7; // ERROR! Can't change a final value.
int numApples = 9;
   // ERROR! numApples already exists! Can't declare again with int.
numApples = 1.3;
// ERROR! numApples is an integer, and can't hold decimals.

Variable names - rules and conventions

There are some rules when it comes to naming variables in java (most languages have similar rules)

There are also some conventions that we usually follow when naming a variable. These won’t create an error - java itself doesn’t care if you follow these - but it improves readability and is strongly encouraged!

Be careful - once you name a variable, you have to use EXACTLY that name, including lowercase and uppercase, for the rest of the program!

Basic data types in java

General Type java-specific usage Description
Boolean boolean isTired = false; The value of a boolean is either true or false. These are most commonly used for comparison and decision-making.
Integer int count = 11; or long n2 = 32039403094; Holds a positive or negative whole integer (whole number). You would use the long type if you might need to hold values above 2 billion or so (or below negative 2 billion), but int is more common, as such large integers are rarely needed.
Floating Point value float f = 1.230; or double d = 2.3e5; Holds a decimal value, or a value in scientific notation. In this course we will usually use the double form as it is more accurate (but takes up more space). Because there is always rounding and therefore possible errors involved with decimal math, it is preferred to use integers when possible.
Character char c = 'a'; A character is a single piece of text data, ready to print to the screen, such as the character 'a' or '8' or 'â' or '안'. In java, a character (or char) literal is always surrounded by SINGLE quotes. Not every possible character is supported by java - for example, emojis are not natively supported.
String String s = "abc"; A string is a series of characters joined together. In java, we use the String type to hold these values (notice the capital, very important!) and we use double quotes to surround a string literal.

Check your Understanding

Bitmoji Books

  1. What type of data is the value 234 in java?

    Answer

    This is an integer, declared with int or long (usually int)

  2. You want to write the code j = "bob". What data type must j be declared as for this to not give an error?

    Answer

    String

  3. The following java code gives an error. Why?

    1
    2
    
    String j;
    j = 123;
    
    Answer

    j is a String but you are trying to assign it an integer

  4. What does an int literal look like?

    Answer

    A number with no decimal point or scientific notation, like 3, 8708, or -34021

  5. What does a double literal look like?

    Answer

    A number a decimal point or scientific notation, like 3.2, -1.2, or 3.2e5

  6. What does a String literal look like?

    Answer

    Any number of characters inside double quotes. Like "hi", or "123!!!", or "Have a nice day!"

  7. The following java code gives an error. Why?

    1
    2
    
     int j;
     j = "123";
    
    Answer

    j is an integer, but you are assigning it a string.

  8. The following code gives an error. Why?

    1
    2
    
    int numCookies = 3;
    int numCookies = 2;
    
    Answer

    the first line declares numCookies by using the type int. It cannot be declared again. This programmer could drop the keyword int and simply do numCookies = 2; on the third line to avoid the error.

  9. The following code gives an error. Why?

    1
    
    string theWord="cookie";
    
    Answer

    There is no type string - you need to capitalize it to make a String!

  10. Write the line of code to declare (but not initialize) a variable named “count” that can hold a whole number.

    Answer
    1
    
    int count;
    
  11. Write the line of code to declare and initialize a variable that holds a first name, in camel case. Initialize it to “David”.

    Answer
    1
    
    String firstName = "David";
    
  12. Write the line of code that creates a constant named MAXSIZE with the value 100;

    Answer
    1
    
    final int MAXSIZE = 100;
    

Study Materials

Bitmoji Books Create flash cards or a quizlet deck with the following words, with examples, a description, and a code example for java on the back.