(fast paced music)
- Java is a C style language.
That means it shares syntax with C, C++
and other similar languages
but it has its own unique set of rules.
First of all, all Java code is defined in classes.
A class is defined in a source code file
with a.java file extension.
In order to turn that file into bytecode,
you use the javac command.
That's the compiler.
And then the bytecode can be run
on top of the Java virtual machine.
You start the virtual machine with the Java command.
Here's what a simple class looks like.
The first line is called the package declaration.
This indicates where the file is stored
in terms of the directory structure of the application
but it's also a tool for organizing your code.
Next is the class declaration.
There might be other lines between the package
and the class depending on the needs of your code.
The class declaration can start with an access modifiers
such as public, private, or protected,
but then must have the class keyword.
The name of the class comes next, followed by a code block.
Everything within the code block
is said to be a member of the class.
This is called a method.
In non-object-oriented languages,
you might refer to this as a function.
It's a way of encapsulating code that you can run.
In this example, I'm starting with the keyword public.
That's the access modifier.
Static which means you don't need to create an instance
of the main class in order to run this method,
void which means that the class doesn't return anything,
and then the name of the method.
Within the parentheses, you define parameters.
These parameters are designed to receive data
as the method is called.
The main method is defined strictly
in command line applications
as the method that's executed automatically
when you start up the class,
and it always receives an array of string values.
Finally, you have your executable code.
This main method only has one line of code
but you can actually have as many as you want.
So this is a simple but well-formed class
that will run successfully on the Java virtual machine.
You can also test your Java code in JShell,
a command you can run from terminal on Mac
or from the command window on other operating systems.
JShell was introduced with Java nine,
and it lets you just test your code
before you take that code
and put it into a fully formed class.
In JShell, you don't need a complete class.
You can just execute one line of code at a time
and then you can take that code
that you've tested and save it out to a file,
so you can convert it to production code.
In Java, everything is case sensitive.
So your identifiers such as variable, method,
and class names all need to be unique
within their particular scopes.
Because of this case sensitivity,
the following are different identifiers
from the Java point of view.
They all say first name
but the first one is lowercase,
the second one uses camel case
and the third is all upper case.
These are each seen as unique.
Java in general is not sensitive to white space.
That is to say extra white space doesn't affect
how your code is interpreted or compiled.
And in fact, if you put in extra spaces, tabs or line feeds,
those will be collapsed by the compiler.
Let's say you have five line feeds
from a compilation point of view,
that's the same as one line feed.
In order for this to work,
all statements in Java must end with a semi-colon character.
So for example, this is a bit of code that outputs a string
hello from Java to the console.
And because it's a single statement,
it ends with that semi-colon character.
If you're working on a full-blown class
and you forget the semi-colon,
it'll cause a compilation error.
If you run this same command though in JShell,
it'll work fine without the semi-colon,
because semi-colons aren't really needed in JShell
or to put it more accurately,
they're added automatically for you in the background.
There are some very strong conventions
that are used by all Java developers,
and you should follow these conventions.
First of all, there's an absolute requirement
that all identifiers must start
with either an alphabetical character,
A through Z or an underscore character.
You can't use numbers as the first character
in an identifier.
You also can't use keywords as identifiers.
You can find a comprehensive list of Java keywords
up through Java eight at this webpage.
And then you can look at the documentation
for more recent versions of Java
to find out what keywords have been added since then.
There are some very strong conventions
for creating your identifiers.
Class names always start with an uppercase character,
but then use mixed case for the rest of the class name.
Methods and variables always have names
that start with a lowercase character.
So when you look in an identifier,
you can distinguish between a class name
and say a method name or a variable name.
Constants are an interesting situation with Java.
There isn't really any such thing
as a pure constant in Java.
That is, there isn't a key word such as const.
Instead in order to define a value that never changes,
you start with the keywords public, static, and final
and then the datatype, string in this case,
and then the identifier.
Constant identifiers are always spelled in all upper case.
Now all of these are just conventions.
You could do it differently if you wanted to
but it's a great idea to follow the conventions
that have been established and are followed broadly
within the Java Development Community.
Finally, I mentioned code blocks.
A code block is defined by a pair of braces.
This is a way of organizing your code,
but it's also a way of restricting visibility of variables.
When you declare a variable within a code block,
it's only visible and useful within that code block
and it expires and is eligible for garbage collection
once the code block is complete.
You'll see code blocks used extensively
with method implementations.
Every method has a code block
and the code within the block
is the executable code for that method,
and loops usually use code blocks as well.
So whatever code is within the block associated with a loop
will get executed as many times as necessary
to satisfy requirements of the loop.
(upbeat music)