1. Programming in Java¶
1.1. Objectives¶
- Define terms related to programming
- Describe the basic structure of a Java program
- Identify the steps in compiling and running a Java program
1.2. Time Goal¶
- 20 minutes on this section
1.3. Key Terms¶
- program
- A sequence of instructions that specifies how to perform tasks on a computer
- source file
- The Java program as written by the programmer
- class file
- The executable program produced by the compiler
- compile
- The process of checking syntax and converting a source file into an executable program. Translates a program in a high-level language into a low-level language, all at once, in preparation for later execution.
- compiler
- Software that translates the Java source code (ends in .java) into the Java class file (ends in .class)
- execute
- The process of running a program on a computer
- high-level language:
- A programming language that is designed to be easy for humans to read and write.
- low-level language:
- A programming language that is designed to be easy for a computer to run. Also called “machine language” or “assembly language”.
1.4. Exercises¶
Note
- Java programs are made up of class and method definitions.
- A method is a named sequence of statements.
- A statement is a line of code that performs a basic action.
In the hello world program below , this line is a print statement that displays a message on the screen:
System.out.println("Hello, World!");
System.out.println
displays results on the screen; the name
println
stands for “print line”. Like most statements, the print
statement ends with a semicolon ;
.
Java is “case-sensitive”, which means that uppercase and lowercase are
not the same. In this example, System
has to begin with an uppercase
letter; system
and SYSTEM
won’t work.
This program defines one method named main
:
public static void main(String[] args)
The name and format of main
is special. Java starts execution in the main
method
(public static void main(String[] args)
). The body of the main method is all the code between
the first {
and the last }
of the main method. When the program runs, it
starts at the first statement in main
and ends when it finishes the
last statement. Every class in Java can have a main method.
Later in the course, we will see programs that define more than one method.
Note
A class is a collection of related
methods. The program below defines a class named Hello
. You can give
a class any name you like, but it is conventional to start with a
capital letter. The name of the class has to match the name of the file
it is in, so this class has to be in a file named Hello.java.
Read the code and run the program below.
Note
Java uses curly braces {
and }
to group blocks of related code
together. In Hello.java, the outermost braces contain the class
definition, and the inner braces contain the method definition.
The line that begins with two slashes (//
) is a comment, which is a
bit of English text that explains the code. When Java sees //
, it
ignores everything from there until the end of the line. Comments have
no effect on the execution of the program, but they make it easier for
other programmers (and your future self) to understand what you meant to
do.
-
Q-74: Which of the following statements are true?
- A statement is a line of code that performs a basic action
- In Java, typically a statement ends with a period
.
- A method is equivalent to a statement in Java
- When a program runs, it starts at the first statement in the main method.
- Code blocks, which are sets of related statements, are contained within curly braces
{ }
in Java.
Note
To define a class in Java usepublic class
followed by a ClassName. Then the body of the class is enclosed in a starting{
and ending}
as shown below.
public class ClassName
{
}
Note
In Java every open curly brace {
must have a matched close curly brace }
.
These are used to start and end both class definitions and method definitions.
Q-76: Q-75: The following has all the correct code to print out "Hi my friend!"
when the code is run, but the code is mixed up. Drag the blocks
from left to right and put them in the correct order. Click on
the "Check Me" button to check your solution.public class MyFriendClass
{
---
public static void main(String[] args)
{
---
System.out.println("Hi my friend!");
---
}
---
}
Q-78: Q-77: The following has all the correct code to print out "Hi there!" when the code is run,
but the code is mixed up and contains some extra blocks with errors. Drag the needed
blocks from left to right and put them in the correct order. Click on the "Check Me"
button to check your solution.public class FriendlyClass
{
---
public Class FriendlyClass
{ #paired
---
public static void main(String[] args)
{
---
public static void main()
{ #paired
---
System.out.println("Hi there!");
---
System.out.println("Hi there!") #paired
---
}
---
}
Note
Java is a high-level language. Other high-level languages you may have heard of include Python, C and C++, PHP, Ruby, and JavaScript.
Before they can run, programs in high-level languages have to be translated into a low-level language, also called machine language. This translation takes some time, which is a small disadvantage of high-level languages. But high-level languages have two major advantages:
- It is much easier to program in a high-level language. Programs take less time to write, they are shorter and easier to read, and they are more likely to be correct.
- High-level languages are portable, meaning they can run on different kinds of computers with few or no modifications. Low-level programs can only run on one kind of computer, and have to be rewritten to run on another.
Note
There are two kinds of programs translate high-level languages into low-level languages: interpreters and compilers. Compiled programs often run faster than interpreted programs.
- An interpreter reads a high-level program and executes it, meaning that it does what the program says to do.
- A compiler reads the entire program and translates it completely before the program starts running. The high-level program is called the source code, and the translated program is called the object code or the executable.
Java is both compiled and interpreted. Instead of translating programs directly into machine language, the Java compiler generates byte code. Similar to object code, byte code is easy and fast to interpret. But it is also portable, so it is possible to compile a Java program on one machine, transfer the byte code to another machine, and run the byte code on the other machine.
Note
Steps of the development process
The Java compiler is a program named javac. It translates .java files into .class files that store the resulting byte code. The Java interpreter is a program named java, which is short for “Java Virtual Machine” (JVM).
The process of compiling and running a Java program.
The programmer writes source code in the file Hello.java and uses javac to compile it. If there are no errors, the compiler saves the byte code in the file Hello.class. To run the program, the programmer uses java to interpret the byte code. The result of the program is then displayed on the screen.
Although it might seem complicated, these steps are automated for you in most program development environments . In this course, we will use tools (including this lab) where the Java code is actually being sent to a server to compile and run (as long as you have an internet connection) and the output will be shown in the browser.
-
Q-79: Which of the following statements are true?
- Java is only compiled and not interpreted.
- When we are programming in a Java, we are writing object code
- The compiler program for Java is called javac
- Java is not a high-level language.
- An advantage of a high-level language is that it is easier to read than machine language.
-
Drag the term to the correct statement
- Java
- A programming language that you can use to tell a computer what to do
- Main Method
- Where execution starts in a Java program
- Object code
- The executable program produced by the compiler
- Source code
- The Java program as written by the programmer