1 / 147

Chapter 2

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.

dugan
Download Presentation

Chapter 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 2 Objects and Primitive Data Section 2.0 An Introduction to Objects

  2. Chapter 2 - Objectives • Define the difference between primitive data and objects. • Declare and use variables. • Perform mathematical computations. • Create objects and use them.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. 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.

  8. 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.

  9. 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.

  10. 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.

  11. An Introduction to Objects

  12. Section 2.1 Using Objects

  13. 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.

  14. 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.

  15. 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.

  16. 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.

  17. 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.

  18. Section 2.2 String Literals

  19. 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.

  20. String Concatenation

  21. 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.

  22. String Concatenation

  23. 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.

  24. String Concatenation

  25. 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.

  26. 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.

  27. String Concatenation

  28. 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.

  29. 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.

  30. String Concatenation

  31. String Concatenation

  32. 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.

  33. 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.

  34. Escape Sequences

  35. Escape Sequences

  36. Escape Sequences

  37. Section 2.3 Variables and Assignment

  38. 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.

  39. 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.

  40. The Assignment Statement

  41. 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;

  42. The Assignment Statement

  43. 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.

  44. 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.

  45. 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.

  46. The Assignment Statement

  47. 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.

  48. 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.

  49. Constants

  50. 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.

More Related