Software Design 2 (SDN260S)
Introduction to Java Programming
H. Mataifa
Department of Electronic, Electrical and
Computer Engineering
1
Programming in Java
• Java is an object-oriented programming (OOP) language, every Java program is written as a
combination of one or more classes
Fig. 2.1: Example of a simple Java program
Notes:
Comments are used in a program to improve the understandability of the program’s functionality
Java uses the end-of-line (//) and traditional (/* */) comment types
Errors that commonly occur in computer programs are syntax and logic errors. Syntax errors violate the rules of programming language (similar
to grammar rules in natural language), and prevent the program from being compiled (i.e. cause compile-time errors). Logic errors lead to the
program producing results different from those which are expected. They do not prevent the program from executing successfully
Class/method declaration
• Class declaration: class keyword introduces a class declaration and is immediately followed by the
class name. public is another keyword that usually appears in a class declaration line
• By convention, class names begin with a capital letter, and capitalize the first letter of each other word
in the case of multiple-word class names. A class name is an identifier – a series of characters
consisting of letters, digits, and certain special symbols (e.g. underscore, dollar sign), may not begin
with a digit, and does not contain any spaces
• Java is case-sensitive – uppercase and lowercase letters are distinct – thus value and Value are two
distinct identifiers
• Declaring a method: the starting point of every Java application is the main method. Notice how it is
declared in line 7. The main method is required for the Java Virtual Machine (JVM) to be able to
execute the application
• The main parts of the method declaration in line 7 are (1) access specifier (public), access modifier
(static), return type (void), method name (main), and parameter list ((String[] args))
Escape sequences
• Escape sequences are special character sequences with a special meaning. They are formed by a
combination of a backslash and a letter or other character. They are frequently used in the [Link]
object along with the print method to print to the output console
Formatted output
• Formatted output can be displayed using method [Link](); the first argument of the method
is a format string, which may consist of fixed text and format specifiers; each format specifier is a
placeholder for a value and specifies the type of data to output. A format specifier begins with the
percentage (%) character, followed by the character that represents the data type. For example, the
format specifier %s is a placeholder for a string data type
• The format string is followed by a comma-separated list of values to replace the format specifiers in the
format string, listed sequentially
Another application: adding integers
• The application requests the user to enter two integers, adds them up, then displays the result
• Notice the use of variables to keep track of the data the program is working with; a variable is an
identifier for a location in memory where a value can be stored and be used later in a program.
Declaring a variable in Java requires specifying the data type and variable name
Another application: adding integers
• Java packages: a Java package is a named group of related classes that makes it easy to reuse
predefined classes in Java applications. Together, the collection of Java packages are referred to as
the Java Class Library or the Java Application Programming Interface (API)
• To use a predefined class in a Java application, we have to import it; for example, the statement:
is an import declaration that gives the program access to class Scanner which is part of package
[Link]
• Notice that all import declarations need to come before any class definitions in the program. That is,
they have to be the placed at the top of the program
• To use the Scanner class in the program, we have to create an object of the class, as done by the
statement:
• A Scanner enables a program to read data (e.g. numbers, strings) for use in a program; the source of
data needs to be specified when creating the Scanner object. In this case, the source of the data is the
keyboard, and the standard input object, [Link], acts as the channel through which the Scanner
object reads from the keyboard
Another application: adding integers
• Primitive and reference data types: Java has two main data types that can be used to define
variables: primitive data types and reference data types.
– Primitive data types include boolean (boolean), byte (byte), character (char), short (short), integer (int), long
(long), float (float), double (double).
– Reference types are essentially user-defined types, defined as classes (e.g. String)
• To request (prompt) the user for input, we use [Link]():
• To capture the (integer) input entered by the user from the keyboard, we use the Scanner object’s
nextInt() method:
• To display the result of the calculation, we use [Link]():
Arithmetic operations in Java
• Most Java applications perform arithmetic operations, so it’s useful for us to be familiar with how
arithmetic operations are implemented in Java, as well as the concept of arithmetic operator
precedence
Examples of arithmetic operator precedence
Decision making: equality and relational operators
• Equality and relational operators are used in conditional statements, which evaluate to either true or
false, to control program execution. Use of equality and relational operators is summarized in Fig. 2.14
Example of program making use of
equality and relational operators
Exercises