Java Tutorial - Basic data types and variables

Common Sense?
30 followers
16 Video Views·Nov 7, 2022
Java Tutorial - Basic data types and variables
In computer science, data is information
that is stored or processed by a computer.
While that might seem fairly abstract,
there are many data points we use in everyday life.
Your name, your age, the number of apples in your pantry,
whether your kitchen light is on or off,
these can all be considered pieces of data.
And we represent pieces of data in code using data types.
Similar to other programming languages,
Java classifies different pieces of data
with data types based on their value.
For example, there's a data type for letters and symbols,
and there are various data types for numbers.
Ultimately, a data type provides a set of possible values.
And if a piece of data is one of these values,
it is classified as that specific type.
Java separates its data types into two main categories.
One of the categories is called primitive data types,
which consist of the most basic data types
in the Java language.
These are the most foundational.
The Boolean data type represents a true or false value.
This means the data piece
of whether the kitchen light is on or off
could be represented as a Boolean in code.
It might have the value true,
or it might have the value false, but it can never be both.
An int, short for integer, represents a whole number.
The number of siblings you have
could be represented in code as an int.
You could have zero siblings, three siblings, 10 siblings,
any whole number amount of siblings.
A double represents a decimal number.
With a double, you can represent your GPA,
say 3.4, in Java.
The last primitive type is a char,
which is short for character.
A char represents a single letter or symbol.
Some possible values for a char
could be your first initial or last initial,
your favorite letter, or the hashtag symbol.
We classify these data types as primitive types
because they are the basis and foundation
for all other data types within the Java programming language.
Let's try using our primitive types in Java.
We'll be creating a few pieces of data
that could represent attributes
of a given high school student.
We could represent a student's age by just writing 15.
Then we could represent a student's GPA with just 3.45.
However, we get errors.
What we've written are technically numbers
and pieces of data, but in Java,
we save data in something called a variable.
A variable is a way to label data
and reference it later in your program.
Instead of saying 15, we can create a label,
say student age, and then assign a value to it,
in this case, 15.
We still get an error here,
and that's because we need to add a data type
to the variable.
We can add int at the beginning of the line
and then a semicolon at the end of the line
to end the statement, and all of our errors are gone.
As you can see, a variable has a data type, label, and value.
In this case, the data type is int.
The label is student age, and the value is 15.
The equal sign is an assignment operator
that assigns the int variable with the label student age
and the value 15.
Let's create the next piece of data in Java,
a student's GPA.
We need to create another variable,
and we decided before that that data type would be double.
So let's write double and give the variable
the label student GPA,
and then we'll sign it the value 3.45
and add a semicolon to end the statement.
Next, we'll represent a student's first initial
with the data type char student first initial
as the label and give it the value K.
We'll do something similar for the student's last initial
with the data type char label student last initial
and give it the value H.
For all of these variables,
you could put a different value
as long as it can still be represented
by that given data type.
For example, you couldn't put a letter or symbol
as the value of student age,
but you could put another whole number,
say 17 instead of 15.
The last piece of data we want to represent
is has perfect attendance.
Like the other variables,
we'll write the data type Boolean,
the label has perfect attendance,
and then give it the value true.
You may notice that our variable names are grayed out.
That's because although we've created a piece of data
and represented it in Java,
we don't use or reference it in our program.
One way we could reference it
is by printing out the value of the variable to the console.
We'll write system.out.println,
and instead of adding quotes,
we'll just write the name of the variable.
Since student age has the value 15,
we'll print out 15 to the console.
We'll save this and then click the play button,
or you could left click and hit run main.main,
and we get 15 in our console.
Let's try this with the other variables.
We'll write system.out.println, student GPA,
system.out.println, student first initial,
system.out.println, the last initial,
and then the has perfect attendance.
We'll run this again,
and we get 15, 3.45, K, H, and true,
as expected in the console.
