Object-oriented software design and development

Dejan Živković The Java programming language

 www.java.com  JRE: Java Runtime Environment • to execute Java programs on particular platform (Windows, Unix, iOS, Android, ...) • JVM (Java Virtual Machine): hypothetical computer and its machine language (bytecode) • interpreter for Java bytecode  JDK: Java Development Kit • to develop Java programs • Java API (Application Programming ): included software components to facilitate Java programming

OOSDD / The Java programming language

 Main characteristics • Object-oriented general-purpose programming language • Java program is independent of a particular computer where it is executed – Once compiled, a Java program can be executed on any computer with JRE

OOSDD / The Java programming language

 Java program is compiled and interpreted

java Java interpreter for Mac OS javac Java Java Java Java interpreter program compiler bytecode for Windows

Java interpreter for Linux

OOSDD / The Java programming language

 Java program development • Textual environment – Notepad, TextPad, … • Graphical environment (IDE) – NetBeans, DrJava, Eclipse, Java Studio, JCreator, …

OOSDD / The Java programming language

 Java program structure • Java program consists of one or more units (classes) • Source code of each class must be contained in a separate file whose name is identical to the class name followed by the extension java

OOSDD / The Java programming language

 Java program structure

public class SequenceSum {

public static void main(String[] args) { . . //Instructions(statements) . } }

OOSDD / The Java programming language

 Compiling and interpreting a Java program in a command window:

javac SequenceSum.java

java SequenceSum

OOSDD / The Java programming language

 Java API contains a huge number of ready-to-use classes that are grouped into packages  A package is a collection of related classes intended for one type of a task  Analogy with files and folders on the disk – packages make it easy to find and use classes  Basic packages: java.lang, java.util, java.math,…

OOSDD / The Java programming language

 Using packages • The import declaration includes a particular class or all classes from a package (all classes are specified by the “*” wildcard) • The import declaration must precede the class definition • The java.lang package is included by default

OOSDD / The Java programming language

 Example:

import java.util.*;

public class SequenceSum {

public static void main(String[] args) { . . . Scanner kb = new Scanner(System.in); . . . } }

OOSDD / Fundamental building blocks

 Names (identifiers)  Data types and their values (literals)  Comments  Variables  Expressions

OOSDD / Names (identifiers)

 Names are given to various program elements  Formal rules: • A name must start with a letter (or or _) • Other characters in a name must be letters or digits (or or _) • Upper case and lower case letters are considered to be different • The length of a name is not limited • Certain words are reserved for special uses in Java and cannot be used for names (reserved words)

OOSDD / Names (identifiers)

 Reserved words:

abstract default if private this boolean do implements protected throw break double import public throws else instanceof return transient case extends int short try catch final interface static void char finally long strictfp volatile class float native super while const for new switch continue goto package synchronized assert

OOSDD / Names (identifiers)

 Examples of valid and invalid names:

keyboard level a69bb11 Level x LEVEL z15 3pigs 9n first year one_VERY_long_name freshmen numberOfStudents blabla sign+or- super

OOSDD / Names (identifiers)

 Naming conventions: • Packages: all lower case letters – mypackage • Classes: first letter of every word is capitalized – MyClass

• Variables/methods: first letter is in lower case, and other words start with a capitalized letter – myVariable, myMethod

• Constants: all upper case letters, and words are separated by an _ – MY_CONSTANT

OOSDD / Data types

 Java is a strongly typed language • A variable in Java can hold only one particular type of data  Primitive data types • “embedded” data

 Class (reference) data types • “programmer-defined” data

OOSDD / Primitive data types

 Integer  data type • byte • char • short  • int Logical data type • long • boolean  Real data type  “Empty” data type • float • void • double

OOSDD / Primitive data types

 Range of data values represented by each is fixed  Data values are stored in a memory location of a fixed size

OOSDD / Primitive data types

Data values:

a) Integer numbers 1. byte size: 1 byte range: −2 ⋯ 2 −1 b) Real numbers size: 2 2. short range: −2 ⋯ 2 −1 c) Characters size: 4 bytes 3. int range: −2 ⋯ 2 −1 d) Truth values 4. long size: 8 bytes range: −2 ⋯ 2 −1

OOSDD / Primitive data types

Data values:

a) Integer numbers

b) Real numbers size: 4 bytes 1. float range: ±1.4 ⋅ 10 ⋯ ±3.4 ⋅ 10 c) Characters size: 8 bytes 2. double range: ±4.9 ⋅ 10 ⋯ ±1.8 ⋅ 10 d) Truth values

OOSDD / Primitive data types

Data values:

a) Integer numbers

b) Real numbers

c) Characters size: 2 bytes char range: \u0000 ⋯ \uFFFF d) Truth values

OOSDD / Primitive data types

Data values:

a) Integer numbers

b) Real numbers

c) Characters

d) Truth values size: 1 boolean range: true, false

OOSDD / Primitive data types

 The char values are stored in two bytes • represent characters in the Unicode scheme • the Unicode standard includes character codes for practically all natural languages  The has no value

OOSDD / Literals

 Represent constant values  Naming for data values • Integer literals • Real literals • Character literals • Boolean literals • String literals

OOSDD / Literals

 Integer literals • Represent integer numbers of type byte, short, int or long • Integer literals can be written using decimal, binary, octal (base-), and hexadecimal (base-) number system • Decimal literals don’t begin with a : • Binary literals begin with b or B: b • Octal literals begin with a : • Hexadecimal literals begin with x or X: xA, XA • Literals ending with a letter L or l represent a value of type long: L, L, l

OOSDD / Literals

 Real literals • Represent real numbers of type float or double • Contain decimal point (.) or exponent (e or E): ., .e, e- • Real literals not ending with a letter or ending with a letter D or d represent real values of type double: ., ., e, .e-D • Real literals ending with a letter F or f represent real values of type float: .f, .f, ef, .e-f

OOSDD / Literals

 Character literals • Represent values of type char • Single character or “escape sequence” is surrounded with a pair of single quote marks • Example: 'A', 't', '?', '\n', '\u'  Boolean literals • Represent truth values: true, false  String literals • Represent sequence of characters (strings) • String is surrounded with a pair of double quote marks • Example: "Hello world", "Enter a number: ", "A"

OOSDD /