字幕列表 影片播放
Welcome to Java Fundamentals for Android: Part 1
This is a five part series
with four goals
We want to explain key Java concepts to
people with very little programming experience
or no programming experience
We want to show the Java syntax
We want to introduce people to the jargon
and at the same time we want to keep everything as brief as possible
because the best way to learn is to do it rather than talk about it
In this video we are going to
talk about the Java type system
as well as variables
So why do we need Java in the first place?
When we make an app or when we write a program
we have to instruct the computer in a language it can understand
But computers speak in
ones and zeros
We don't want to resort to having
to write machine code
or machine language
or type in ones and zeros
into the computer
So we use a programming language
which can easily be translated
into machine code that the computer can understand
and that's Java
There's really four stages to programming
First, we translate our ideas into Java code
Second, we send that code on to the compiler
The compiler is a special program
that takes a look at the Java code
and then sees if it complies with the rules of Java
and general syntax requirements
and if it doesn't; if it spots any errors
the compiler complains and we have to fix those problems
before we can continue
However, if we pass the compiler's scrutiny
the compiler will translate the Java code into something that's executable by the machine
and at this point the program starts to run
This is when we are able to discover if the program behaves as expected
Sometimes there are things that we miss and things that the compiler misses
and the program crashes
We have to fix those problems
Often times a big part of developing an app
is trying to break it
trying to spot all the errors
but if everything goes well
the program finishes
So what are those things that the compiler checks?
One of them is the particular rules and
structure that Java has
Java pays particular attention to data
because programming is primarily about manipulating data
Java enforces a set of rules called the type system
to distinguish between different kinds of data
In Java each value must have a particular type
and by "type" I mean "category"
You can think about types as "categories"
For example, whole numbers or integers are of type "int"
They are a category that includes the number 4 or 2001
Another category is boolean
A boolean is something that can only have one of two values
A boolean is either true or false
Another example of a type
is String
A String is a sequence of characters
A String is a piece of text
"All your base", that's a String
How do we keep track of all this data?
We need to put the data somewhere
The place we put the data is a variable
Variables act as containers to hold the data
Just like the raw data, the container itself has a type
In a declaration, we specify
the variable's type as well as it's name
Here we have a variable
called "myValue"
with type "int", so we can put whole numbers into it
This is what we do with assignment
Here we are assigning the value 4 to the variable "myValue"
Note how the value 4 is an integer
the same type as its container
"myValue"
Declaration and assignment can also be combined
We see here two examples
In the first one we are putting the number 6 into "anotherValue"
In the second one, we are putting the characters
"War and Peace" into a variable called "longRead"
Variables aren't any good if we can't use them
to manipulate data
Here we are putting the value 16 into the variable "A"
Here we are putting the value 2 into the variable B
If we divide "A" by "B" we can put the result into in a variable called "C"
which will hold the value 8
The grey text you see on the right is called a comment
Anything that has two slashes in front of it
will be ignored by the compiler
It won't affect the running of the program
We will be using this notation throughout the slides
To give you another example
We have the value "Fish " which is being stored in the variable "main"
We've got the value "& Chips" being stored in the variable "side"
If we combine the two
we can store the result ("Fish & Chips") in the variable "meal"
"meal" will store the concatenated value "Fish & Chips"
To recap,
Java is an intermediary language used
to help communicate instructions to the computer
We use variables to store and manipulate data
Everything in Java must have a type
We declare variables by specifying the type as well as the name
We assign values to the variables with the '=' sign
Lastly, the type of data must match the type of the variable
Thanks for watching!
Join us for part 2, where we'll be
discussing objects, classes, and methods.