Java Reference
In-Depth Information
In this expression, we want to determine whether the substring in
str2
starting at
position
17
and of length
4
is the same as the substring in
str4
starting at position
9
and of length
4
. This expression returns
true
because these substrings are the same.
The program
Ch3_SomeStringMethods.java
, which shows the effect of the preceding
statements, can be found in the Additional Student Files folder at www.cengagebrain.com.
3
To summarize the preceding discussion of the
class
String
:
1.
String
variables are reference variables.
2. A string object is an instance of the
class
String
.
3. The
class
String
contains various methods to process strings.
4. A
String
variable invokes a
String
method using the dot operator, the
method name, and the set of arguments (if any) required by the method.
Input/Output
A program performs three basic operations: it gets data into the program, it manipulates the
data, and it outputs the results. In Chapter 2, you learned how to manipulate numeric data
using arithmetic operations. Because writing programs for input/output (I/O) is quite
complex, Java offers extensive support for I/O operations by providing a substantial number
of I/O classes, such as the
class
Scanner
. In the remainder of this chapter, you will:
Learn how to format output using the method
printf
.
Learn other ways to input data and output results in Java.
Learn how to format the output of decimal numbers to a specific number
of decimal places.
Learn how to instruct the program to read data from, or write output to,
a file. If there is a large amount of data, inputting data from the keyboard
every time you execute your program is not practical. Similarly, if the
output is large or you want to save the output for later use, you must save
a program's output to a file.
In Chapter 2, you learned how to show the output of a program on the standard output
device using the standard output object
System.out
and the methods
print
and
println
. More specifically, to output the results, you used statements such as
System.out.print(expression)
and/or
System.out.println(expression)
,
where
expression
is evaluated and its value is output. However, the methods
print
and
println
cannot directly format certain outputs in a specific manner. For example, the
default output of floating-point numbers is typically up to 6 decimal places for
float
Search WWH ::
Custom Search