You are on page 1of 20

Chapter 1: Introduction

Adopted from the Notes provided by the authors


The Java Language
• Developed in the early 1990s at Sun Microsystems
• Introduced to the public in 1995
• Oracle purchased Sun Microsystems in 2010
• Version 8 was released in March 2014
• Multiple configurations for different platforms:
– Java SE (Java Platform, Standard Edition) (our focus)
– Java EE (Java Platform, Enterprise Edition)
– Java ME (Java Platform, Micro Edition)
• Java is the first programming language that allows
execution in a web browser
• Java is an object-oriented programming language

2nd semester, 2014-2015 ELEC 2543 Chapter 1-2


Programming Language Levels
• Programming languages can be classified
into four groups:
– Machine language
– Assembly language
– High-level languages
– Fourth-generation languages

2nd semester, 2014-2015 ELEC 2543 Chapter 1-3


Machine Language (Code)
• Theoretically, a CPU only understands machine
language
• Each type of CPU has its own language and
machine codes for Intel processors cannot be
run on AMD processors
• Each machine language instruction only
performs a very simple task, such as copy a
value into a register
• Codes are expressed as binary digits
– E.g. 000000 00001 00010 00110 00000 100000
2nd semester, 2014-2015 ELEC 2543 Chapter 1-4
Assembly
• Binary digits in machine code become
understandable words
• Still needs to first translate to machine
code before a CPU can execute
• Similar to machine code, each assembly
instruction performs a simple operation
– E.g. mov ebp, esp; add esp, 20

2nd semester, 2014-2015 ELEC 2543 Chapter 1-5


3rd and 4th Generation
Languages
• The kind of programming languages that is
mostly used for writing programs
• Each statement can perform much more tasks
• Require compiler or interpreter to translate a
program into machine language
• Fourth-generation languages (4GL) provides a
further higher level of abstraction.

2nd semester, 2014-2015 ELEC 2543 Chapter 1-6


Compiler and Interpreter
• A compiler translate a high-level program
into machine language program. The
program can then executed.
• An interpreter translates a smaller part of
the program at a time and execute it.
• What are the pros and cons?

2nd semester, 2014-2015 ELEC 2543 Chapter 1-7


Java Translation (1)
• The Java compiler translates Java source code
into a special representation called bytecode

• Java bytecode is not the machine language for


any traditional CPU
• Java Virtual Machine (JVM), being an interpreter,
translates bytecode into machine language and
executes it

• Therefore the Java compiler is not tied to any


particular machine
• Java is considered to be architecture-neutral
2nd semester, 2014-2015 ELEC 2543 Chapter 1-8
Java Translation (2)

Java source
code Java
bytecode

Java
compiler
Java Virtual Bytecode
Machine compiler

Machine
code

2nd semester, 2014-2015 ELEC 2543 Chapter 1-9


Java Program Structure
• In the Java programming language:
– A program is made up of one or more classes
– A class contains one or more methods
– A method contains program statements

• These terms will be explored in detail throughout


the course

• A Java application always contains a method


called main
• See Obama.java

2nd semester, 2014-2015 ELEC 2543 Chapter 1-10


Object-Oriented Programming
• As the term implies, an object is a fundamental
entity in a Java program
• Objects can be used effectively to represent real-
world entities
• For instance, an object might represent a
particular bank account in a bank
• Each bank object handles the processing and
data management related to the account such as
money withdrawals, deposits, etc.

2nd semester, 2014-2015 ELEC 2543 Chapter 1-11


Objects
• An object has:
– state - descriptive characteristics
– behaviors - what it can do (or what can be done to it)

• The state of a bank account includes its account


number and its current balance
• The behaviors associated with a bank account
include the ability to make deposits and
withdrawals
• Note that the behavior of an object might change
its state
2nd semester, 2014-2015 ELEC 2543 Chapter 1-12
Classes
• An object is defined by a class
• A class is the blueprint of an object
• The class uses methods to define the behaviors of the
object
• The class that contains the main method of a Java
program represents the entire program (Obama.java)
– Main function in C/C++ is standalone

• A class represents a concept, and an object represents


the embodiment of that concept
• Multiple objects can be created from the same class
2nd semester, 2014-2015 ELEC 2543 Chapter 1-13
Objects and Classes
A class An object
(the concept) (the realization)

Bank John’s Bank Account


Account Balance: $5,257

Bill’s Bank Account


Balance: $1,245,069
Multiple objects
from the same class
Mary’s Bank Account
Balance: $16,833

2nd semester, 2014-2015 ELEC 2543 Chapter 1-14


Inheritance
• One class can be used to derive another via
inheritance
• Classes can be organized into hierarchies
Account

Credit Card Bank


Account Account

Savings Checking
Account Account
2nd semester, 2014-2015 ELEC 2543 Chapter 1-15
Java Program Structure (1)
// comments about the class
public class MyProgram
{
class header

class body

Comments can be placed almost anywhere


}

2nd semester, 2014-2015 ELEC 2543 Chapter 1-17


Java Program Structure (2)
// comments about the class
public class MyProgram
{
// comments about the method
public static void main (String[] args)
{
method header
method body
}

2nd semester, 2014-2015 ELEC 2543 Chapter 1-18


Identifiers (1)
• Identifiers are the words a programmer uses in a
program
• Java identifiers are similar to the ones in C++ that
– can be made up of letters, digits, the underscore character ( _ )
– Identifiers cannot begin with a digit
– case sensitive - Total, total, and TOTAL are different
identifiers
• By convention, programmers use different case styles for
different types of identifiers, such as
– title case for class names - Lincoln
– upper case for constants - MAXIMUM

2nd semester, 2014-2015 ELEC 2543 Chapter 1-19


Identifiers (2)
• Sometimes we choose identifiers ourselves
when writing a program (such as Lincoln)

• Sometimes we are using another programmer's


code, so we use the identifiers that he or she
chose (such as println)

• Often we use special identifiers called reserved


words that already have a predefined meaning in
the language
• A reserved word cannot be used in any other
way
2nd semester, 2014-2015 ELEC 2543 Chapter 1-20
Reserved Words
• A reserved word cannot be used in any other way
• The Java reserved words:

abstract else interface switch


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

2nd semester, 2014-2015 ELEC 2543 Chapter 1-21

You might also like