Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 28

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 2


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 3


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 4


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 5


Object-Oriented Program Development Using Java: A Class-Centered Approach 6
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 7


Conditional Expressions
(continued)
• If the value of the condition is true:
– expression is evaluated
– Otherwise expression2 is evaluated
• 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 8


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 9


Object-Oriented Program Development Using Java: A Class-Centered Approach 10
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 11


Object-Oriented Program Development Using Java: A Class-Centered Approach 12
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 13


Object-Oriented Program Development Using Java: A Class-Centered Approach 14
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 15


Object-Oriented Program Development Using Java: A Class-Centered Approach 16
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 17


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 18


Object-Oriented Program Development Using Java: A Class-Centered Approach 19
Object-Oriented Program Development Using Java: A Class-Centered Approach 20
Object-Oriented Program Development Using Java: A Class-Centered Approach 21
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 22


Object-Oriented Program Development Using Java: A Class-Centered Approach 23
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 24


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 25


Object-Oriented Program Development Using Java: A Class-Centered Approach 26
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 27


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 28

You might also like