Lesson 1

Data Types

Author: Brandon Fajardo (8/29/17). Updated (6/18/20)

Introduction

Review: This lesson is a continuation of two previous setup lessons which helped you set up everything we will need to code in Java.

*If you do not have the things described in those lessons, you will not be able to follow along.

Lesson topic: In this lesson, you will learn about how to set up a class and how to store data as variables.

Topic 1 - Basics of a Class

To begin, you should have created your first project and your first Java class. After creating a class, Java makes the first line of code for you. It writes:

public class NAME {

}

*NAME is whatever you named your class

Within this first set of curly brackets, write the following line of code:

public class NAME {
    public static void main(String[] args){
    
    }
}

*This can be done more quickly by writing "psvm" and pressing tab or enter: "psvm" (written) + [tab/enter]

Tip

In Java, everything that is executed in code is written within a set of curly brackets (also called braces) {}. All the code you write for the time being should be written within the set of curly brackets of public static void main.

Topic 2 - Variables

In programming, we need a way to manipulate data (information). To start, we can store data using variables. Variables are associated with (hold) certain values. This is similar to how we use variables in mathematics. Java also requires us to classify our variables under categories. These categories of data are called data types.

Data type: a classification that specifies the type of data associated with a variable.

Topic 3 - Writing Variables

Writing a variable consists of two parts: declaration and assignment.

Subtopic 1 - Declaration

The first part of writing a variable is declaration.

Declaration: we declare a variable’s data type.

This is done with the notation:

data type variable;

*The data type is written in front of the name of the variable with a semicolon afterward.

For example:

int number;

This defines the variable “number” as part of the “int” data type.

String animal;

This defines the variable “animal” as part of the “String” data type

*Note: These data types and more will be described later in this lesson.

Subtopic 2 - Assignment

The second part of writing a variable is assignment.

Assignment: we assign a value to the variable. This is also known as initialization.

Effectively, we are giving a value to our variable. This is done with the notation:

variable = value;

*The name of the variable is equated to a value with a semicolon.

number = 1234; 

This defines the variable "number" as equal to 1234.

animal = “cat”;

This defines the variable “animal” as equal to cat.

*Note: Again, don’t worry about the specifics of writing it just yet. Just understand that we are allowed to do this in general.

Subtopic 3 - Combining

With declaration and assignment, we are able to now use variables in our code. We can simplify this by declaring and assigning a variable at the same time. To assign and declare simultaneously, the following notation is used:

data type variable = value;

Side Note

Question: Why learn declaration and assignment separately?

Answer: You may wish to assign a value to a variable at a different time than the declaration.

Topic 4 - Naming Variables

  1. The name of a variable must be a string of letters.

  2. You cannot use spaces in the name of your variable.

  3. You cannot use symbols (or collections of symbols, such as words) which Java uses for other procedures.

  4. The name must be unique to other names. This allows Java to tell two values apart. (i.e. two variables can’t both be named the same name)

Topic 5 - Data Types

Now that we know how to create a variable, we can explore actually implementing all the different data types. Remember, a data type denotes what category of information our variable falls under.

Subtopic 1 - Numeric data types: Integers, Decimals

Numeric data types hold numbers.

Integer data types allow our variables to hold integer values. ( … , -2, -1, 0, 1, 2, …)

*Integer data types can store one integer lower negative than positive compared to what is shown here

EX:

byte a = 29;
short b = 26834;
int c = 43545343;
long d = 900000000;

Decimal data types allow our variables to store decimal values.

Note: Integer data types cannot store decimals.

*Decimal data types can hold much smaller numbers than the highest positive values shown here

EX:

float e = 2.5f;
double f = -11.687468;
int g = 2.5;

Question: Line 3 would not work. Why?

Answer: Line 3 would not work because an "int" is an integer data type, and integer data types cannot hold decimal points.

Subtopic 2 - Precision

Precision: Indicates how many decimal significant figures the value has.

Floats usually only have 6 or 7 decimal digits and will round the others.

Doubles usually have 15 decimal digits and will round the others.

float variable = 3.234234897234987f;
double variable_two = 3.234234897234987;

If you print both "variable" and "variable_two", you will get: variable = 3.2342348 and variable_two = 3.234234897234987. This demonstrates that doubles have more precision than floats, because they can hold more decimal significant figures.

Subtopic 3 - Non-numeric Data Types: Boolean, Characters

Non-numeric data types focus on being able to hold either true/false or characters.

EX:

boolean h = true;
boolean i = false;

Unicode: "an international encoding standard for use with different languages and scripts, by which each letter, digit, or symbol is assigned a unique numeric value that applies across different platforms and programs."

EX:

char j = ‘A’;
char k = 10000; // Unicode character for a pencil

String l = “Words and such”;

Tip

The semicolon (;) is used at the end of many statements of value in Java. The semicolon is like the period of a sentence in Java.

Topic 6 - Comments

Using double slashes (//) in front of a line of code makes it a comment. Java ignores any comments when you run your code.

*Try adding short notes to the code you have written, such as explanations. This practice is known as commenting on your code, and is highly useful and important in all programming.

Ctrl + / makes what is highlighted a comment.

*This is undone by highlighting and pressing Ctrl + / again.

Encasing anything in /* and */ makes it into a comment. This is useful when commenting out a large section of code.

EX:

int m; //THIS IS A COMMENT

/*
This is used to comment large sections of code.
*/

Conclusion

Recap: In this lesson, you should have learned how Java stores data with variables and what data types can be used to store different types of data.

Next: In the next lesson, you will cover how to run the code you have written and how to work with some basic mathematical operations.

Last updated