Com 323 Programming in Java Week 01
Com 323 Programming in Java Week 01
Week 01
• You will see when you start writing programs that the sort of
things that can cause compiler errors are the incorrect use of
special Java keywords, missing brackets or semi-colons, and
many others.
• If, however, the source code is free of such errors the compiler
will successfully produce a machine code program that can be
run on a computer, as illustrated.
https://www.tiobe.com/tiobe-index/
• How is this achieved? The answer lies in the fact that any Java
program requires the computer it is running on to also be
running a special program called a Java Virtual Machine or JVM
for short.
• There is a special kind of JVM for mobile phones; and there are
JVMs built into machines where the embedded software is
written in Java.
NOTE: The original developers of Java were Sun Microsystems™. This company was
acquired by Oracle™ in 2010.
• The Java language that comes with JDK consists of two parts.
• The first part, called java.lang, is an essential component of Java.
• The second part is a collection of source codes that can be
selected and added to the Java program being written.
• To write and edit a Java source code, a text editor is needed.
Some editors understand the syntax of Java and offer a variety of
assistance.
• The process of generating Java bytecode from a Java source code
is called compilation.
• A primitive way to compile a Java source code is to execute a
compilation in a command line interface.
• As mentioned earlier, all Java source files must have the file
name extension .java.
• These can be created using a text editor but must be saved with
the .java extension and are called Java files.
javac cuz.java
• Not only does an IDE do all these things, it also lets you run your
programs as soon as you have compiled them.
• The source code that you write is saved in the form of a simple
text file which has a .java extension.
javac MyProgram.java
java MyProgram
• In the last case you would probably be unaware of the fact that the
software is running at all
• In the former cases you would be seeing output from your program
on a screen and providing information to your program via a
keyboard and mouse, via a touch screen, or via a joystick or game
controller.
• Text based
• Graphics based
• Text based programs make use of the keyboard for user input.
• The three words in the first line: public class cuz state that:
a) this is a program unit of type class,
b) the unit is named cuz, and
c) the unit is accessible from all other Java program units.
• The keyword public specifies that the unit is accessible from other
program units. A keyword that specifies the accessibility of program
units and their components is called a visibility attribute.
• This is the way we are always going to get stuff printed on a simple
text screen; we use System.out.println (or sometimes
System.out.print, as explained below) and put whatever we want
to be displayed in the brackets.
• Also, you should notice the semi-colon at the end of the statement.
This is important; every Java instruction has to end with a semi-
colon.
17-Feb-22 © 2021. Cavendish University. Rights Reserved 35
8. Java Programming | Classes
• The first, and most important, thing to pay attention to is the word
class.
• We noted earlier that Java is referred to as an object-oriented
programming language.
• Now, the true meaning of this will start to become clear in later
stages -but for the time being you just need to know that object-
oriented languages require the program to be written in separate
units called classes.
• The simple programs that we are starting off with will contain only
one class (although they will interact with other classes from the
“built-in” Java libraries).
• We always have to give a name to a class and in this case we have
simply called our class HelloWorld.
• When choosing a name for a class, you can choose any name as
long as:
• So, the first line tells the Java compiler that we are writing a class
with the name HelloWorld.
• However, you will also have noticed the word public in front of the
word class; placing this word here makes our class accessible to the
outside world and to other classes—so, until we learn about
specific ways of restricting access (in the second semester) we will
always include this word in the header.
• A public class should always be saved in a file with the same name
as the class itself—so in this case it should be saved as a file with
the name Hello World.java.
• Notice that everything in the class has to be contained between
two curly brackets (known as braces) that look like this {}; these tell
the compiler where the class begins and ends.
• Our HelloWorld class contains just one method and this line
introduces that method.
• A program starts with the first instruction of main, then obeys each
instruction in sequence (unless the instruction itself tells it to jump
to some other place in the program). The program terminates
when it has finished obeying the final instruction of main.
• NOTE: public static void main (String[ ] args) could also be written
with the angle brackets after args i.e. public static void main (String
args [ ]).
• At the moment we will not worry about the words public static void
in front of main, and the bit in the brackets afterwards (String[]
args) —we will just accept that they always have to be there; you
will begin to understand their significance as you learn more about
programming concepts.
Adding Comments:
• It is possible to insert texts that have no relevance to how the code
runs. Such texts are called comments.
• Comments are free-form texts. Java compilers ignore comments
when producing class files and so they exist only in the source file.
• A programmer can use comments to make notes to him or herself.
For example, comments can be about the expected behaviorof the
program and about the underlying algorithms.
• (//) at the beginning of the line
• The comment is enclosed between two special symbols; the
opening symbol is a slash followed by a star (/*) and the closing
symbol is a star followed by a slash (*/). Everything between these
two symbols is ignored by the compiler.