1.47k likes | 1.69k Views
Chapter 2. Objects and Primitive Data. Section 2.0. An Introduction to Objects. Chapter 2 - Objectives. Define the difference between primitive data and objects. Declare and use variables. Perform mathematical computations. Create objects and use them. An Introduction to Objects.
E N D
Chapter 2 Objects and Primitive Data Section 2.0 An Introduction to Objects
Chapter 2 - Objectives • Define the difference between primitive data and objects. • Declare and use variables. • Perform mathematical computations. • Create objects and use them.
An Introduction to Objects • Java is an object-oriented programming language. • An object is a basic part of the Java language. • In addition to objects, java also manages primitive data types.
An Introduction to Objects • Primitive data types include common values such as numbers and characters. • An object usually represents something more complicated, such as a bank account. • An object often contains primitive values and is in part defined by those values.
An Introduction to Objects • A data type defines a set of values and operations – what we can do with those values. • We perform operations on primitive types using operators that are built into the programming language.
An Introduction to Objects • An object is defined by a class, which is like the data type of an object. • The operations that can be performed on an object are defined by the methods in the class. • A method is a collection of programming statements given a name so we can use them when we need them.
An Introduction to Objects • Once a class has been defined, objects can be created from that class. • For example, once we define a class to represent the idea of a bank account, we can create objects that represent individual bank accounts.
An Introduction to Objects • Each bank account would keep track of its own balance. • This is what is known as encapsulation. • Encapsulation means that each object protects and manages its own information.
An Introduction to Objects • Classes can be created from other classes using inheritance. • Inheritance is defining one class based on another class that already exists. • Inheritance is a form of software reuse.
An Introduction to Objects • Classes made from other classes are known as derived classes. • Classes, objects, encapsulation, and inheritance are all ideas that make up the world or object-oriented programming.
Section 2.1 Using Objects
Using Objects • The Lincoln program we used earlier invokes a method through an object. • System.out.println(“…”); • The System.out object represents an output device or file, which by default is the monitor. • The objects name is out and it is in the System class.
Using Objects • The println method is a service that the System.out object performs for us. • When println is used, it sends a string of characters to the screen for us. • In short, we send the println method to the System.out object and ask it to print to the screen.
Using Objects • When data is sent to a method, it is called a parameter. • The parameter we send to the println method is the string of characters between the quotes. • There is also one more important method the System.out object gives us, the print method.
The print and println Methods • There is one small but important difference between print and println. • The println method prints what is sent to it to the screen then moves to the next printable line. • The print method prints what is sent to it, but does not move the next printable line.
Abstraction • When an object is an abstraction, it means that the details of how it works do not matter to the object user. • We do not really know how the println method works, but we know how to use it, which is the important part.
Section 2.2 String Literals
String Literals Introduction • A character string is an object defined by the classString. • Since strings are used all of the time, Java incorporates what is known as a string literal, which appears inside double quotes (“…”). • This section will focus on two important features, concatenation and escape sequences.
String Concatenation • As you can see, the previous program there are several println statements. • The first line is so long it can not fit on one line, so to combine them in the same println method, we use the (+) symbol to add two or more strings together. • When you do this it is called string concatenation.
String Concatenation • In the second line of the program, you see a println called with nothing in the parenthesis. • The println method does not need a parameter sent to it, if you do this, it will just move the cursor to the next printable line and print nothing.
String Concatenation • As you can see with the rest of the program, not only can string be concatenated with other strings, but they can also be concatenated with numbers. • Numbers do not need to be in quotes, numbers are automatically converted to a string and the two strings are concatenated.
String Concatenation • Numbers can be included in strings, i.e. “Speed of ketchup: 40 km per year.” • This can be done because digits are characters and can be parts of strings. • They are separated here to show that it can be done, plus this will be very useful later.
String Concatenation • As you know, ( + ) means to add, so the meaning of the + sign depends greatly on the context in which it is used. • When dealing with pure numbers, the + sign will always add. • When dealing with strings, unless told to do otherwise, the + sign will concatenate.
String Concatenation • When anything is put inside the println method, it is automatically considered a string and therefore concatenated. • Using parenthesis around numbers forces the + sign to perform addition instead of concatenation.
Escape Sequences • Since the double quote (“) is part of the Java language to indicate the beginning and the end of a string, we need a special way to print quotation marks. • Putting a set of quotes are one, (“””), would only confuse the compiler, this would result in a compiling error.
Escape Sequences • To overcome this problem, Java defines several escape sequences to represent special characters for screen or file output. • To use these escape sequences, place a single backslash (\) in front of the character that needs to be sent to the screen.
Section 2.3 Variables and Assignment
Variables • A variable is a name for a memory location used to hold a data value. • A variable declaration tells the compiler to reserve a portion of main memory large enough to store the data. • The declaration also tells the compiler what to name the location.
Variables • When variables are declared they are undefined unless you give it an initial value. • Some Java compilers will result in an error when variables are not initialized with an initial value. • Variables are initialized with an initial value by using the assignment statement.
The Assignment Statement • The first statement inside the main is the variable declaration / initialization statement. • This statement declares a variable of type int (integer), names it sides and gives it an initial value of 7. • The statement is simply: dataType varName = value;
The Assignment Statement • The two assignment statements, sides = 10; and sides = 12; basically take the variable sides and give it a new value. • It is called an assignment statement because it takes a variable and assigns it a value.
The Assignment Statement • The assignment statement evaluates what is on the right hand side of the assignment operator (=), and stores it in the memory location indicated by the name on the right hand side of the assignment operator. • For right now, the left hand side is just a number, but later you will see it can also be a mathematical expression, a string, etc.
The Assignment Statement • One variable can only hold one value of its own data type. • So when a variable is reassigned, the previous value is written over and lost. • But previously referenced versions of the variable are not changed.
Constants • Sometimes your programs need variables that hold values that never change. • If your bank account has a minimum value, let’s say $100, this is a value that will never change, and you don’t want it to accidentally be changed by another part of the program.
Constants • Constants are identifiers and like variables except that their value never changes. • To make a variable a constant, use the reserved word finalbefore the declaration, and although you do not have to give it an initial value, it is good practice to do so, as a naming convention, use all capitols for the name.
Constants • If you try to change a constant, the compiler will give you an error message once the constant has been given its initial value. • Constants should be used for a value you will need to reference a lot, but do not want changed.