Vous êtes sur la page 1sur 1

1- Object-Oriented Programming 1- Object-Oriented Programming

 Java is an object-oriented programming language  Java is an object-oriented programming language


 As the term implies, an object is a fundamental entity in a Java program  As the term implies, an object is a fundamental entity in a Java program
 Objects can be used effectively to represent real-world entities  Objects can be used effectively to represent real-world entities
 For instance, an object might represent a particular employee in a company  For instance, an object might represent a particular employee in a company
 Each employee object handles the processing and data management  Each employee object handles the processing and data management
related to that employee related to that employee

2- Objects 2- Objects
• An object has: • An object has:
– state - descriptive characteristics – state - descriptive characteristics
– behaviors - what it can do (or what can be done to it) – 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 • The state of a bank account includes its account number and its current
balance balance
• The behaviors associated with a bank account include the ability to make • The behaviors associated with a bank account include the ability to make
deposits and withdrawals deposits and withdrawals
• Note that the behavior of an object might change its state • Note that the behavior of an object might change its state

3- Classes 3- Classes
• An object is defined by a class • An object is defined by a class
• A class is the blueprint of an object • A class is the blueprint of an object
• The class uses methods to define the behaviors of the 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 • The class that contains the main method of a Java program represents the
entire program entire program
• A class represents a concept, and an object represents the embodiment of • A class represents a concept, and an object represents the embodiment of
that concept that concept
• Multiple objects can be created from the same class • Multiple objects can be created from the same class

4- Character Strings 4- Character Strings


• A string of characters can be represented as a string literal by putting • A string of characters can be represented as a string literal by putting
double quotes around the text: double quotes around the text:
• Examples: • Examples:
"This is a string literal." "This is a string literal."
"123 Main Street" "123 Main Street"
"X" "X"
• Every character string is an object in Java, defined by the String class • Every character string is an object in Java, defined by the String class
• Every string literal represents a String object • Every string literal represents a String object

5- The println Method 5- The println Method


• In the Lincoln program from Chapter 1, we invoked the println method to • In the Lincoln program from Chapter 1, we invoked the println method to
print a character string print a character string
• The System.out object represents a destination (the monitor screen) to • The System.out object represents a destination (the monitor screen) to
which we can send output: which we can send output:
System.out.println ("Whatever you are, be a good one."); System.out.println ("Whatever you are, be a good one.");

6- String Concatenation 6- String Concatenation


• The string concatenation operator (+) is used to append one string to the • The string concatenation operator (+) is used to append one string to the
end of another end of another
"Peanut butter " + "and jelly" "Peanut butter " + "and jelly"
• It can also be used to append a number to a string • It can also be used to append a number to a string
• A string literal cannot be broken across two lines in a program • A string literal cannot be broken across two lines in a program

7- Constants 7- Constants
 A constant is an identifier that is similar to a variable except that it holds  A constant is an identifier that is similar to a variable except that it holds
the same value during its entire existence the same value during its entire existence
 As the name implies, it is constant, not variable  As the name implies, it is constant, not variable

 The compiler will issue an error if you try to change the value of a constant  The compiler will issue an error if you try to change the value of a constant
 In Java, we use the final modifier to declare a constant  In Java, we use the final modifier to declare a constant
 final int MIN_HEIGHT = 69;  final int MIN_HEIGHT = 69;

8- Constants 8- Constants
• Constants are useful for three important reasons • Constants are useful for three important reasons
• First, they give meaning to otherwise unclear literal values • First, they give meaning to otherwise unclear literal values
– For example, MAX_LOAD means more than the literal 250 – For example, MAX_LOAD means more than the literal 250
• Second, they facilitate program maintenance • Second, they facilitate program maintenance
– If a constant is used in multiple places, its value need only be – If a constant is used in multiple places, its value need only be
updated in one place updated in one place
• Third, they formally establish that a value should not change, avoiding • Third, they formally establish that a value should not change, avoiding
inadvertent errors by other programmers inadvertent errors by other programmers

Vous aimerez peut-être aussi