280 likes | 366 Views
Chapter 14: Additional Capabilities. Object-Oriented Program Development Using Java: A Class-Centered Approach. Objectives. Additional Features Bit Operators Command-Line Arguments. Other Number Bases. Integer literals can be specified using non-decimal bases: Octal Hexadecimal
E N D
Chapter 14: Additional Capabilities Object-Oriented Program Development Using Java: A Class-Centered Approach
Objectives • Additional Features • Bit Operators • Command-Line Arguments Object-Oriented Program Development Using Java: A Class-Centered Approach
Other Number Bases • Integer literals can be specified using non-decimal bases: • Octal • Hexadecimal • Integer with leading 0 is taken to be an octal value • Integer beginning with leading 0x or 0X is considered a hexadecimal number Object-Oriented Program Development Using Java: A Class-Centered Approach
The flush() Method • When using print() method, arguments placed within the parentheses are sequentially put in a temporary holding area • Called a buffer • Buffer is continually used until it is forced to send its contents to an output stream Object-Oriented Program Development Using Java: A Class-Centered Approach
The flush() Method (continued) • Flush: • Forcing buffer contents to an output stream • Occurs whenever: • Line terminator character is encountered • End of program is reached • Explicit flush() method is called • Syntax: • System.out.flush(); Object-Oriented Program Development Using Java: A Class-Centered Approach
Object-Oriented Program Development Using Java: A Class-Centered Approach
Conditional Expressions • Conditional operator: • ? : • Provides an alternative way of expressing a simple if-else statement • Syntax: • condition ? expression1 : expression2 • Ternary operator Object-Oriented Program Development Using Java: A Class-Centered Approach
Conditional Expressions (continued) • If the value of the condition is true: • expressionis evaluated • Otherwise expression2isevaluated • Conditional expressions are usually unclear • Only useful in replacing if-else statements • When expressions in equivalent if-else statement are not long or complicated Object-Oriented Program Development Using Java: A Class-Centered Approach
Bit Operators • Java provides for manipulation of individual bits of character and integer constants and variables • Each operand is considered as a binary number • Consisting of a series of individual 1s and 0s Object-Oriented Program Development Using Java: A Class-Centered Approach
Object-Oriented Program Development Using Java: A Class-Centered Approach
The AND Operator • Operator symbol: & • Result of each bit-by-bit comparison is 1 • Only when both bits being compared are 1s • Otherwise result is 0 • Useful in masking selected bits from an operand Object-Oriented Program Development Using Java: A Class-Centered Approach
Object-Oriented Program Development Using Java: A Class-Centered Approach
The Inclusive OR Operator • Operator symbol: | • Result of comparison is 1 • If either bit being compared is 1 • Otherwise, result is 0 • Useful in forcing selected bits to take on a 1 value • Or for passing through other bit values unchanged Object-Oriented Program Development Using Java: A Class-Centered Approach
Object-Oriented Program Development Using Java: A Class-Centered Approach
The Exclusive OR Operator • Operator symbol: ^ • Result of comparison is 1 • If one and only one of the bits being compared is 1 • Otherwise, result is 0 • Used to create the opposite value of any individual bit in a variable • Also called complement Object-Oriented Program Development Using Java: A Class-Centered Approach
Object-Oriented Program Development Using Java: A Class-Centered Approach
The Complement Operator • Operator symbol: ~ • Unary operator • Changes: • Each 1 bit in an operand to 0 • Each 0 bit to 1 Object-Oriented Program Development Using Java: A Class-Centered Approach
The Shift Operators • Left shift operator: • Operator symbol: << • Causes bits in an operand to be shifted left by a given amount • Fills any vacated bits with 0 • Right shift operator: • Operator symbol: >> • Causes bits in an operand to be shifted right by a given amount Object-Oriented Program Development Using Java: A Class-Centered Approach
Object-Oriented Program Development Using Java: A Class-Centered Approach
Object-Oriented Program Development Using Java: A Class-Centered Approach
Object-Oriented Program Development Using Java: A Class-Centered Approach
Command-Line Arguments • Arguments can be passed to any method in a program • Including the main() method • Command line: • Line on which the words Java and class name are typed to run a program • Always starts with the operating system prompt Object-Oriented Program Development Using Java: A Class-Centered Approach
Object-Oriented Program Development Using Java: A Class-Centered Approach
Command-Line Arguments (continued) • Command-line arguments: • Typed on the command-line following the program name • Operating system passes them into main() as a sequence of separate strings • Must be declared as part of the main method’s definition • Only one type of argument is allowed: • Array of Strings Object-Oriented Program Development Using Java: A Class-Centered Approach
Command-Line Arguments (continued) • Syntax: • public static void main(String[] args) • Within main method: • args[0] refers to the first argument • args[1] to the second argument • etc • Number of command-line arguments is determined by using the expression args.length Object-Oriented Program Development Using Java: A Class-Centered Approach
Object-Oriented Program Development Using Java: A Class-Centered Approach
Summary • print() method places text input into the buffer • Buffer’s contents are displayed either: • When the program is finished running • When the flush() method is invoked • println() method is automatically flushed from the buffer • Conditional expression provides an alternative way of expressing simple if-else statements Object-Oriented Program Development Using Java: A Class-Centered Approach
Summary (continued) • Individual bits of character and integer variables and constants can be manipulated using Java’s bit operators • Arguments passed to main() are termed command-line arguments • Each argument passed to main() is considered a string • Stored using array of references named args Object-Oriented Program Development Using Java: A Class-Centered Approach