This lesson introduces you to programming and the Java language.
it might be tempting to skip this lesson if you've already learned
another language or have learned a bit of Java before, but I go over
some details that many other books and courses skip over. These
details will make it easier to understand how we write programs and
this will in turn help you solve problems more easily.
In this lesson we'll cover the following:
Overview of this Lesson
What is Programming?
How do we Write Programs?
How do we Write Java Programs?
What is Programming?
What is programming? What does a programmer do, exactly?
You likely use many different applications (programs) on your computer:
your browser is an application, any games you play are applications,
even your operating system (e.g. Windows, Mac OS, Linus) are applications.
On your phone, you have so many applications - pretty much any icon you see
is an application, and there are several applications that aren't represented
by those visual icons that work behind the scenes.
Where do these applications come from? Developers and designers work together to create and write these
applications. Some programs are written by a team of developers; some programs are small and written by
one developer.
You probably already have an idea that a programmer, or coder
sits at the computer all day and their fingers magically fly over the keys at
great speeds while symbols and characters rapidly appear on the screen. This is
how many programmers are portrayed on TV and in movies. Some of this image of
coding is true, but programming doesn't only involve sitting at a computer writing code.
Programming often also includes other activities such as:
Exploratory activities and critical thinking
analysing an existing system and data flows
interviewing and gathering information from users
doing research, taking extra classes/seminars, going to conferences
designing applications (user interfaces, UML diagrams, data flow, etc)
Writing
internal program documentation (in your code)
external documentation (help files for users, API documentation for other programmers)
reports and emails to client or team leads
Dealing with People
working with other programmers/analysts
presenting to a client or team
training users, helping users with problems
Some of the activites above may sound appealing and some may not be
appealing to you at all; some might seem exciting and some might seem
boring or frightening. So don't panic, these tutorials are all about coding.
Perhaps you just want to code and deal only
with the computer. That's fine. If you're in a college or university
program, you'll likely take courses that focus on many of the activities
mentioned above. But if you found these pages, you're probably most
interested in programming or coding, even if it's just for the moment.
That's fine, too.. but note that in order to become a good coder, you will
have to become a good critical thinker and problem solver, so these
tutorials will include activities and exercises that will help you
with those things, too.
So, What is Coding, Then?
The coding part of programming involves using a programming
language to give the computer instructions
that perform various tasks. Today we use high-level languages
to write code: languages like Java, Visual Basic, C++, and hundreds of others.
Don't worry, you don't need to learn all of those languages.
Some people only know one or two, lots of people know several. At the
time I last updated this page, I knew 11 programming languages (not
counting markup languages like HTML), but about
half of them aren't used much anymore. Some I have forgotten because
I haven't used them in years. Today I mostly code with 2 or 3.
If you learn one language really, really well, you'll find it really
easy to learn a new language. Most programming languages use the same
logical structures, they work in similar ways, and have a lot of the same
features and characteristics. So once you learn one language, learning
another one is quick and easy.
As you start working through these tutorials, make sure you've got
Java installed, an Editor or IDE, and a note book and pen or pencil
(or a device you can write on, like a tablet with a stylus).
Yes, you will need to write things down, but also draw
things like diagrams, tables, and flow charts. You won't ONLY be
coding: you'll also be using your critical thinking skills and you'll
be problem solving, and some of the activities are best done by
writing and drawing!
How we Write Programs
Evolution of Programming Languages
When you write code, you're generally using a high-level language.
Java is a high-level language, and so are languages like Python, C/C++,
Ruby, and Turing. High-level languages are English-like and can be
read by other programmers.
Prior to the invention of high-level languages, developers had to code
in lower-level languages. For example, in the beginning, we only had
machine language. Machine language is basically just binary
code, or a series of 1's and 0's, or even hexadecimal code (digits from 0
to 9, along with the letters from A to F). A machine language is the native language
of a particular platform. A platform consists of the
combination of an operating system and hardware. For example, the PC
platform is a combination of an IBM-type PC configuration with Microsoft
Windows installed. The Mac platform is an Apple-type computer configuration
with Mac OS installed. The machine language for a PC machine is different
from the machine language for a Mac machine. If you wanted to write
a program that worked for both Mac and PC, you would have to write it
in two different machine languages.
Eventually, assembly languages were developed to help
make programming easier. Assembly consisted of abbreviations for specific
types of operations (such as ADD for addition or MOV to move data from
one memory location to another). This was still very low-level programming:
a command to add two numbers involved several statements to move
and copy digits to and from memory registers (locations in memory).
Assembly was not like machine language in that it was similar between
different platforms, however a machine did not "speak" assembly so
it couldn't run assembly programs. Instead, the developer would have
to take their finished assembly code and assemble
the code into machine code using an Assembler program.
This machine code could then be read and
executed by the platform on which the code was compiled. For example,
if you compiled the code on a PC, it would only run on a PC, and if you
compiled the code on a Mac, it would only run on a Mac. Even though
you still had to compile a program for different platforms, it was
still much easier than writing the program more than once in different
machine languages.
You can clearly understand now how high-level languages are much easier to read and learn!
However, high-level language code is also not understood by a machine - remember,
a machine only understands machine code. Therefore, there needs to be a way
to convert your high-level source code that you write into
machine language. This is the job of the compiler.
How Programs are Written
To write programs in any language you use an editor or an
IDE (Integrated Development Environment).
Editors are generally very simple, and good for first-time programmers.
IDEs are more complex and include debugging tools, project management,
memory management, design features, testing tools, and often many
other things. In a lot of the introductory lessons of this course
I'll be using Notepad++ or IntelliJ but in later tutorials I'll be using various
IDEs. The Installation Instructions includes instructions for
IntelliJ and VS Code.
Source Code is the actual code that you type into
an editor or IDE. Source code is generally just plain text, so you
could actually use any editor or even a word processor to write it,
although most developers prefer an editor or IDE that's geared
towards Java programming because of the extra features that those tools
have. Even the most basic editor will include syntax
highlighting, which will assign different colours and
formatting to different words and symbols in your source code
to show how they fit into the Java language.
Once you've written some source code, you compile
that code. A compiler translates your high-level language
source code into machine language that the computer can read and
execute. Compiling your code will check your code for syntax errors,
or errors in the spelling/grammar of the programming language.
If you have any syntax errors, you'll need to fix them and then recompile
your code.
Successful compilation of the source code results in a machine
language file or program that can actually run on the computer.
Usually this is an .exe or Executable file.
Machine language is specific to a particular machine, so if you compile
your source code on a PC, that machine code can only run on a PC. If you
compile that code on a Mac, you can only run that program on a Mac.
Also, remember that there are hundreds of languages you could program with,
so each language has to come with its own set of compilers for the various
kinds of machines. As with Assembly, you will have to compile your code
with the different compilers if you wanted to create executable files
for each kind of machine (Windows, Mac, Linux, etc.)
A compiler does more than translates source code into machine language. It also
checks your code's syntax. Syntax refers to the grammar and
spelling of the programming language. For example, it ensures you have
all the right punctuation and symbols and that you haven't spelled any
words wrong. If you have any syntax errors in your code,
the compiler will list them all for you, and it will not compile the source
code into machine code. You then have to fix your syntax errors and try
the compiler again. You repeat this process until the compiler finds no
syntax errors, in which case it will translate your source code into
machine language.
Larger and more complex programs also use a program called a linker
that allows the developer to add other libraries to their program.
A library is a collection of code that is already written and can be used
in any other program. For example, if you want your program to use a credit card
validation service that someone else wrote, you obtain that library and add
it to your program. This is often done with programs that use operating system
components, such as the standard set of file open/save dialog boxes. The linker
takes the library (or libraries) and adds them to the machine code for your
program and creates an executable file that can be run on the computer.