Chapter 1: Introduction to Computers, Programs, and Java
Programming
Means to create (or develop) software, which is also called a program.
Computer
Is an electronic device that stores and processes data.
Programming languages
Software developers used to create software.
What are hardware and software?
Computer hardware refers to the physical, tangible parts of computer and software refers to the computer Instructions
and programs.
List five major hardware components of a computer.
A central processing unit (CPU), Memory (main memory), Storage devices (such as disks and CDs), Input devices (such
as the mouse and keyboard), Output devices (such as monitors and printers), Communication devices.
What does the acronym “CPU” stand for? What are hardware and software?
Central processing unit is the computer’s brain. It retrieves instructions from memory and executes them. The CPU has
two components: a control unit and an arithmetic logic unit. The control unit controls and coordinates the actions of the
other components. The arithmetic/logic unit performs numeric operations (addition, subtraction, multiplication,
division) and logical operations (comparisons).
What unit is used to measure CPU speed?
Gigahertz.
What is a bit? What is a byte?
These are binary digits, or bits for short. Single bits are too small to be much use, so they are grouped together into
units of 8 bits. Each 8 bit unit is called a byte. Data of various kinds, such as numbers and characters, are encoded as a
series of bytes.
What is memory for?
The computer’s work area for executing a program. Memory stores data and program instructions in uniquely
addressed memory locations. Each memory location can store one byte of data.
What does RAM stand for? Why memory is called RAM?
RAM is short for Random access memory. Since the bytes in the memory can be accessed in any order, the memory is
also referred to as random-access memory (RAM).
What unit is used to measure memory size?
Computer storage is often measured in bytes
What unit is used to measure disk size?
Bytes
What is the primary difference between memory and a storage device?
A computer’s memory (RAM) is a volatile form of data storage: any information that has been stored in memory (that
is, saved) is lost when the system’s power is turned off, while Programs and data are permanently stored on storage
devices.
Computer programs
Known as software, are instructions that tell a computer what to do.
Machine Language
A computer’s native language, which differs among different types of computers.
Assembly language
Created in the early days of computing as an alternative to machine languages. Assembly language uses a short
descriptive word, known as a mnemonic, to represent each of the machine-language instructions.
The computer cannot understand assembly language, another program called an assembler—is used to translate
assembly-language programs into machine code.
High-Level Language
Are platform-independent, which means that you can write a program in a high-level language and run it in different
types of machines.
A program written in a high-level language is called a source program or source code.
Interpreter / Compiler
Used for translating source code or source program into machine code for execution.
What language does the CPU understand?
Machine languages in form of binary code
What is an assembly language?
An alternative to machine languages. Assembly languages were developed to make programming easier.
What is an assembler?
Because the computer cannot understand assembly language, another program—called an assembler—is used to
translate assembly-language programs into machine code
What is a high-level programming language?
New generation of programming languages, they are platform-independent, which means that you can write a program
in a high-level language and run it in different types of machines. High-level languages are English-like and easy to learn
and use. The instructions in a high-level programming language are called statements.
What is an interpreter?
An interpreter reads one statement from the source code, translates it to the machine code or virtual machine code,
and then executes it right away.
What is a compiler?
A compiler translates the entire source code into a machine-code file, and the machine-code file is then executed.
Difference: An interpreter translates and executes a program one statement at a time. A compiler translates the entire
source program into a machine-language file for execution.
What is the difference between an interpreted language and a compiled language?
A compiled language is converted into machine code so that the processor can execute it. An interpreted language is
language in which the implementation execute instructions directly without compiling a program into machine language.
Therefore, the compiled programs runs faster than interpreted programs.
Operating Systems
The operating system (OS) is the most important program that runs on a computer. The OS manages and controls a
computer’s activities.
The major tasks of an operating system are:
■ Controlling and monitoring system activities
■ Allocating and assigning system resources
■ Scheduling operations
Multiprogramming allows multiple programs to run simultaneously by sharing the same CPU.
Multithreading allows a single program to execute multiple tasks at the same time.
Multiprocessing, or parallel processing, uses two or more processors together to perform subtasks concurrently
and then combine solutions of the subtasks to obtain a solution for the entire task.
What is an operating system? List some popular operating systems.
The operating system (OS) is the most important program that runs on a computer. The OS manages and controls a
computer’s activities. Microsoft Windows, Mac OS, and Linux. Application programs, such as a Web browser or a word
processor, cannot run unless an operating system is installed and running on the computer.
Java
Is a powerful and versatile programming language for developing software running on mobile devices, desktop
computers, and servers. Java was developed by a team led by James Gosling at Sun Microsystems. Sun Microsystems
was purchased by Oracle in 2010. Originally called Oak, Java was designed in 1991 for use in embedded chips in
consumer electronic appliances.
Java is simple, object oriented, distributed, interpreted, robust, secure, architecture neutral, portable, high
performance, multithreaded, and dynamic.
What is a Java applet?
Java initially became attractive because Java programs can be run from a Web browser. Such programs are called
applets. Applets employ a modern graphical interface with buttons, text fields, text areas, radio buttons, and so on, to
interact with users on the Web and process their requests. Applets make the Web responsive, interactive, and fun to
use. Applets are embedded in an HTML file. HTML (Hypertext Markup Language) is a simple scripting language for laying
out documents, linking documents on the Internet, and bringing images, sound, and video alive on the Web.
What programming language does Android use?
The software for Android cell phones is developed using Java.
Java syntax
Is defined in the Java language specification, and the Java library is defined in the Java API. The JDK is the software for
developing and running Java programs. An IDE is an integrated development environment for rapidly developing
programs. The Java language specification is a technical definition of the Java programming language’s syntax and
semantics.
The application program interface (API), also known as library, contains predefined classes and interfaces for
developing Java programs.
What does IDE stand for?
Integrated development Environment. Editing, compiling, building, debugging, and online help are integrated in one
graphical user interface.
Are tools like NetBeans and Eclipse different languages from Java, or are they dialects or extensions of Java?
The JDK consists of a set of separate programs, each invoked from a command line, for developing and testing Java
programs. Instead of using the JDK, you can use a Java development tool (e.g., NetBeans, Eclipse, and TextPad)—
software that provides an integrated development environment (IDE) for developing Java programs quickly. Therefore,
NetBeans, and Eclipse are not programming languages, or dialects, or extensions of java. They are java development
tools.
A Java program is executed from the main method in the class.
Console input means to receive input from the keyboard, and console output means to display output on the monitor.
Reserved words, or keywords, have a specific meaning to the compiler and cannot be used for other purposes in the
program.
Comments help programmers to communicate and understand the program.
TABLE 1.2 Special Characters
Character Name Description
{} Opening and closing braces denote a block to enclose statements.
() Opening and closing parentheses Used with methods.
[] Opening and closing brackets denote an array.
// Double slashes Precede a comment line.
"" Opening and closing quotation marks enclose a string
; Semicolon Mark the end of a statement.
Some Java keywords:
char, case, double, float, while, public, static, switch, break, else, byte etc.
Is Java case sensitive? What is the case for Java keywords?
Java source programs are case sensitive. It would be wrong, for example, to replace main in the program with Main.
Since Java is a case sensitive language, all keywords must be written in lowercase letters including java keywords.
What is a comment? Is the comment ignored by the compiler? How do you denote a comment line and a comment
paragraph?
Comments help programmers to communicate and understand the program. They are not programming statements and
thus are ignored by the compiler. In Java, comments are preceded by two slashes (//) on a line, called a line comment, or
enclosed between /* and */ on one or several lines, called a block comment or paragraph comment. When the compiler
sees //, it ignores all text after // on the same line. When it sees /*, it scans for the next */ and ignores any text
between /* and */.
What is the statement to display a string on the console?
System.out.println(...) is used to display a message on the console.
What is the Java source filename extension, and what is the Java bytecode filename extension?
The source file must end with the extension .java and must have the same exact name as the public class name. The
java bytecode filename has an extension of “.class”. After compilation of the java file, the JVM (java Virtual machine)
creates a byte code file with an extension of “.class”
What are the input and output of a Java compiler?
The input of java compiler is a java source code file and the output is a java class file.
What is the command to compile a Java program?
javac command
What is the command to run a Java program?
java command (Command prompt, compile, show files, run)
What is the JVM?
Java virtual machine is a program that loads, verifies, executes and interprets Java bytecode.
Can Java run on any machine? What is needed to run Java on a computer?
To execute a Java program is to run the program’s bytecode. You can execute the bytecode on any platform with a JVM,
which is an interpreter. It translates the individual instructions in the bytecode into the target machine language code
one at a time rather than the whole program as a single unit. Each step is executed immediately after it is translated.
If a NoClassDefFoundError occurs when you run a program, what is the cause of the error?
You execute a class file that does not exist
If a NoSuchMethodError occurs when you run a program, what is the cause of the error?
You execute a class file that does not have a main method or you mistype the main method (e.g., by typing Main instead
of main).
JOptionPane is one of the many predefined classes in the Java library
showMessageDialog method is used to display any text in a message dialog box
What is the statement to display the message “Hello world” in a message dialog box?
You can use the showMessageDialog method to display any text in a message dialog box.
Why does the System class not need to be imported?
The system class is in java.lang package. This package is already/ implicitly imported. So, there is no need to explicitly
import it.
Are there any performance differences between the following two import statements?
There are two types of import statements: specific import and wildcard import. The specific import specifies a single
class in the import statement (e.g., import javax.swing.JOptionPane). The wildcard import imports all the classes in a
package by using the asterisk as the wildcard (e.g., import javax.swing.*). The information for the classes in an imported
package is not read in at compile time or runtime unless the class is used in the program. The import statement simply
tells the compiler where to locate the classes. There is no performance difference between a specific import and a
wildcard import declaration.
Good programming style and proper documentation make a program easy to read and help programmers prevent
errors.
Programming style deals with what programs look like.
Documentation is the body of explanatory remarks and comments pertaining to a program.
Indentation is used to illustrate the structural relationships between a program’s components or statements
A block is a group of statements surrounded by braces. There are two popular styles: next-line style and end-of-line
style.
What are syntax errors (compile errors), runtime errors, and logic errors?
Syntax errors result from errors in code construction, such as mistyping a keyword, omitting some necessary
punctuation, or using an opening brace without a corresponding closing brace. These errors are usually easy to detect,
because the compiler tells you where they are and what caused them. Runtime errors are errors that cause a program
to terminate abnormally. They occur while a program is running if the environment detects an operation that is
impossible to carry out. Input mistakes typically cause runtime errors. Logic errors occur when a program does not
perform the way it was intended to. Errors of this kind occur for many different reasons.
Give examples of syntax errors, runtime errors, and logic errors.
Syntax error:
public class ShowSyntaxErrors {
public static main(String[] args) {
System.out.println("Welcome to Java);
Runtime error:
public class ShowRuntimeErrors {
public static void main(String[] args) {
System.out.println(1 / 0);
}
Logic error:
public class ShowLogicErrors {
public static void main(String[] args)
System.out.println("Celsius 35 is Fahrenheit degree ");
System.out.println((9 / 5) * 35 + 32);
If you forget to put a closing quotation mark on a string, what kind error will be raised?
Syntax error
If your program needs to read integers, but the user entered strings, an error would occur when running this program.
What kind of error is this?
Runtime error
Suppose you write a program for computing the perimeter of a rectangle and you mistakenly write your program so
that it computes the area of a rectangle. What kind of error is this?
Logic error