Java Console (Standard Input Output)
Java Console (Standard Input Output)
Java Console (Standard Input Output)
1 Standard Output
When a program computes a result, we need a way to display this
result to the user of the program. One of the most common ways to
do this in Java is to use the console window.
The console window is also called the standard
output window. We output data such as the
computation results or messages to the console
window via System.out.
Every data item we send to System.out will appear on
this console window. We call the technique to output
data by using System.out the standard output.
We use the print method to output a value. For
example, executing the code
System.out.print("Hello, Dr. Caffeine.");
2.4.2 String
The textual values we passed to the print method or
the constructor of the Jframe class are instances of
the String class. A sequence of characters separated
by double quotes is String constants. As String is a
class, we can create an instance and give it a name.
For example,
String name;
name = new String("Jon Java");
Unlike in other classes, the explicit use of new to create an
instance is optional for the String class. We can create a new
String object, for example, in this way:
Code:
String name;
name = "Decafe Latte";
There are close to 50 methods defined in the String class. We
will introduce three of them here: substring, length, and
indexOf. We can extract a substring from a given string by
specifying the beginning and ending positions. For example,
Code:
String text;
text = "Espresso";
System.out.print(text.substring(2, 7));
Notice the use of method composition in the last
statement, where the result of a method call is used
as an argument in another method call.
In the statement
Code:
System.out.print (text.substring(2,7));
the result of method call
text.substring(2,7)
Result of extracting and displaying the substring of
"Espresso" from index position’s 2 to 6.
The index position of the first character in a string is
0.