Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 Assignment Statement

Similar presentations


Presentation on theme: "Chapter 3 Assignment Statement"— Presentation transcript:

1 Chapter 3 Assignment Statement
9/8/ :43 AM Chapter 3 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms: x = 5; a literal (5) is assigned to x x = y + 2; the value of an expression (y + 2) is assigned to x x = z; the value of another variable (z) is assigned to x Assignment requires an equal sign (=). The value on the right is "given to" or "assigned" the value of the variable on the left. © 2012 EMC Publishing, LLC

2 Chapter 3 Variable Assignment
9/8/ :43 AM Chapter 3 Variable Assignment A variable can store only one value at any time. int x; x = 5; x = 10; x 10 5 Note: This slide contains animations. Press the space bar or click the mouse button to display each animation. This slide contains three (3) animations. The variable declaration, int x, associates the variable name x with a memory location <press space bar> The first assignment statement, x = 5, associates the memory location for x with the value 5 <press space bar> The second assignment statement, x = 10, associates the memory location for x with the value 10 <press space bar> Note that once the variable x is associated with a new value, in this case 10, the previous value, 5, cannot longer be accessed. © 2012 EMC Publishing, LLC

3 Chapter 3 Primitive Data Types
9/8/ :43 AM Chapter 3 Primitive Data Types Type Storage Required int 4 bytes double 8 bytes char 2 bytes boolean 1 bit It is important to choose the appropriate data type when declaring variables so that the compiler allocates only the needed storage space for a variable. For example, declaring every variable a double will ensure that both ints and doubles can be represented, but unneeded storage space will be used. Additionally, the reader of the application will not be clear on what type of value a variable is expected to hold. © 2012 EMC Publishing, LLC

4 Chapter 3 Abstract Data Types
9/8/ :43 AM Chapter 3 Abstract Data Types A variable declared with a class is called an object. For example, the object spot is type Circle: Circle spot = new Circle(4); spot getRadius() area() Note: This slide contains animations. Press the space bar or click the mouse button to display each animation. This slide contains one (1) animation. The instantiation of object spot creates a reference to the object's data and method. <press space bar> © 2012 EMC Publishing, LLC

5 Numerous packages are included with JDK Packages contain classes
9/8/ :43 AM Chapter 3 Java Packages Numerous packages are included with JDK Packages contain classes Packages can be added to an application with an import statement. For example, the statement import java.util.Scanner; makes the Scanner class and its methods accessible to the application. Packages are reusable units of code that can be added to an application. An asterisk (*) can be used in place of a class name to import all the classes of a package. © 2012 EMC Publishing, LLC

6 Chapter 3 The Scanner Class
9/8/ :43 AM Chapter 3 The Scanner Class Part of the java.util package A Scanner object processes text and numbers from the input stream Methods include: next() nextLine() nextInt() nextDouble() nextBoolean() close() The Scanner class is used to read input from the user. The Scanner method that should be used depends on the data expected from the user. © 2012 EMC Publishing, LLC

7 Chapter 3 Integer Division
9/8/ :43 AM Chapter 3 Integer Division Integer division (/) is performed when both operands are integers. Only the integer portion of the quotient is returned: © 2012 EMC Publishing, LLC

8 9/8/ :43 AM Chapter 3 Real Division Real division (/) is performed when one or both operands are type double. The entire quotient, including the decimal portion is returned: double result; result = 20.0/7.0; //result is 2.857 © 2012 EMC Publishing, LLC

9 Chapter 3 Modulus Division
9/8/ :43 AM Chapter 3 Modulus Division Modulus division (%) returns the remainder of a division operation: © 2012 EMC Publishing, LLC

10 Chapter 3 Operator Precedence
9/8/ :43 AM Chapter 3 Operator Precedence Operators in Java have the following precedence: 1. multiplication and division 2. addition and subtraction Operators of the same precedence are evaluated in order from left to right. For example, multiplication is performed first, then division, and finally addition: 5 + 6 * 4 / 2 = 17 © 2012 EMC Publishing, LLC

11 Chapter 3 Changing the Order of Operations
9/8/ :43 AM Chapter 3 Changing the Order of Operations The order in which operators are evaluated can be changed by using parentheses. For example, addition is performed first, then multiplication, and finally division: (5 + 6) * 4 / 2 = 22 © 2012 EMC Publishing, LLC

12 9/8/ :43 AM Chapter 3 Type Casting Type Casting converts a number of one type to a number of a different, but compatible type. Type casting is used to: 1. make the operand types in an expression match. For example, wholeNum = (int)y * 2 2. truncate the decimal portion of a double. For example, wholeNum = (int)z 3. change the way in which a division (/) operation will be performed. For example, realDivision = (double)a / (double)b © 2012 EMC Publishing, LLC

13 Chapter 3 Assignment Operators
9/8/ :43 AM Chapter 3 Assignment Operators Operator Operation += addition and then assignment -= subtraction and then assignment *= multiplication and then assignment /= division and then assignment %= modulus division and then assignment © 2012 EMC Publishing, LLC

14 Chapter 3 Named Constants
A named memory location that cannot be changed from its initial value. The keyword final is used in a constant declaration. Constant identifiers are typically all uppercase with an underscore (_) separating words within the identifier name. © 2012 EMC Publishing, LLC

15 Chapter 3 Java Keywords abstract double int strictfp boolean else
9/8/ :43 AM Chapter 3 Java Keywords abstract double int strictfp boolean else interface super break extends long switch byte final native synchronized case finally new this catch float package throw char for private throws class goto protected transient const if public try continue implements return void default import short volatile do instanceof static While Keywords have special meaning to the Java compiler and therefore cannot be used for a variable or constant identifier. © 2012 EMC Publishing, LLC

16 Chapter 3 Programming Errors
Syntax errors violate the rules of Java. Logic errors, also called semantic errors, occur in statements that are syntactically correct, but produce undesired or unexpected results. Run-time errors, also called exceptions, halt program execution at the statement that cannot be executed. One type of exception is called InputMismatchException. © 2012 EMC Publishing, LLC


Download ppt "Chapter 3 Assignment Statement"

Similar presentations


Ads by Google