System.out.
System.out:
public static void main(String args[]) {
double x,y,z;
x = 4195835.0;
y = 3145727.0;
z = x - (x / y) * y;
System.out.println("result = " + z);
}
z.
System that
contains an object called out that has a method called
println.
out does basic character output.
System used here.
System.out.println uses the plus sign, +,
as an operator to concatenate Strings.
String appears as one operand of the
+, the compiler does not do arithmetic addition, but tries to
convert the other operand to a String and then concatenates the
two.
String with a value of a
primitive type is a Java idiom to convert the value to a string.
int i = 256;
...
"" + i // yields a String containing the value of i
String class:
String.valueof( i )
System.out.println code above, the variable
z is converted to a String, and appended to the
literal message.
String is passed as a single argument to the
println() method, causing it to be printed to the system console.
System.out.println:
System.out.println( "The character (" + 'a' +
") has the value " + ( (int) 'a' ) );
The character (a) has the value 97
Class average is,
followed by the value of the variable average:
average = total / 10; // integer division
System.out.println( "Class average is " + average );
Reader/Writer classes added to the old 8-bit
stream classes.
Reader/Writer Unicode stream classes should
be used instead of the old JDK 1.0
InputStream/OutputStream classes because the
Reader/Writer classes make it easy to write code that
can be internationalized.
Reader (not in InputStream)
Writer (not in OutputStream)
System objects for
interactive I/O.
InputStream or
OutputStream. The 16-bit streams are known as Reader
(input) or Writer (output).
Reader, Writer,
InputStream, or OutputStream in it, it's easy to
know which set of services you are using.
java.lang.System class
and they are all byte streams:
System.in - InputStream used to read bytes from the keyboard
System.out - PrintStream used to write bytes to the screen
System.err - PrintStream used to report errors
System.in (a BufferedInputStream):
int grade;
...
System.out.print( "Enter letter grade: " );
grade = System.in.read();
Enter letter grade: on the
screen.
System.in to obtain one letter
grade from the user. The System.in.read method reads one
character from the keyboard and stores that character in the integer variable
grade.
char.
However, a feature in Java is that characters can be stored in most integer
data types because they are represented as 2-byte integers in Java.
Reader
rather than an 8-bit stream.
InputStream (like
System.in) is to layer an InputStreamReader on top of
it.
InputStreamReader is an adapter from a byte stream to a
character Reader.
InputStream, and it gives you back a Reader.
InputStreamReader around System.in:
InputStreamReader isr = new InputStreamReader(System.in);
InputStreamReader object
InputStreamReader on System.in. This
gives us a Reader class, on which we can layer other Readers.
BufferedReader on the InputStreamReader.
This gives us a method that reads an entire line, allowing us to get all the
text as a string when the user presses Return.
java.util.StringTokenizer on the String just read in.
Call the nextToken() method to extract just the nonspace
characters. This makes the input format a little more forgiving by removing
trailing and leading whitespace.
parseXxx method of the primitive types wrapper
classes to extract the expected value.
import java.io.*;
import java.util.*;
...
InputStreamReader is = new InputStreamReader( System.in );
BufferedReader br = new BufferedReader( is );
...
String s = br.readLine();
StringTokenizer st = new StringTokenizer(s);
try...catch around the input statements (we will
discuss exceptions later).
int i = is.read(); // -1 denotes EOF
char c = (char) i;
String s = br.readLine();
boolean bo = new Boolean(st.nextToken()).booleanValue();
int i = Integer.parseInt(st.nextToken());
byte by = Byte.parseByte(st.nextToken());
short sh = Short.parseShort(st.nextToken());
long lo = Long.parseLong(st.nextToken());
float fl = new Float(st.nextToken()).floatValue();
double db = new Double(st.nextToken()).doubleValue();
try {
// put I/O statement here
} catch (IOException ioe)
{ ioe.printStackTrace(); }
int value:
import java.io.*;
import java.util.*
public class exreadin2 {
public static void main(String args[]) {
int i=0;
char c;
InputStreamReader is = new InputStreamReader( System.in );
BufferedReader br = new BufferedReader( is );
StringTokenizer st;
try {
System.out.print( "int: "); System.out.flush();
String myline = br.readLine();
st = new StringTokenizer(myline);
i = Integer.parseInt(st.nextToken());
System.out.println( "got: " + i );
} catch (IOException ioe) {
System.out.println( "IO error:" + ioe );
}
}
}
Object.
class Fruit {
int grams;
int cals_per_gram;
int total_calories() { ... }
}
And here is an example of class inheritance:
class Citrus extends Fruit {
void squeeze() { ... }
}
Citrus a subclass that inherits all the
Fruit class operations and adds this squeeze()
specialization of its own.
squeeze method on a Citrus
object, and you can also call the total_calories() method.
Citrus is based on
(extends) Fruit. A
Citrus is a specialization of Fruit; it has all the
fields that Fruit has, and adds a method of its own.
class test2e {
public static void main(String args[]) {
Fruit somefruit = new Fruit();
Citrus lemon = new Citrus();
lemon.squeeze();
somefruit = lemon;
}
}
lemon (a Citrus
object) into somefruit (a Fruit object). You can
always make a more general object hold a more specialized one, but the
reverse is not true without an explicit type conversion (you are encouraged
to read about casting on your own).
abstract,
final, or public (abstract and
final are opposites and are not allowed together).
abstract: class must be extended (to be useful)
final: class must not be extended
public: class is visible in other packages
abstract public class Fruit { ...
final public class Citrus extends Fruit { ...
public - fields is visible everywhere
(class must be public too).
(blank) - what you get by default. Field is only visible in this package.
protected - like default, and
the field is visible in subclasses in other packages extended from this
class, too.
private - field is only visible in this
class.
(keywords that modify the way the field is used)
static - one per class, not one for each
object. Where you see static read it as "only one".
final - field is only visible in this class.
protected static final int upper_bound = 2047;
upper_bound is visible throughout this package and in
subclasses of this class, even ones in other packages. The data is associated
with the class, not an individual object, so any change made to it would be
seen by all objects. It won't change though, because it has been assigned
a final value.
public - fields is visible everywhere
(class must be public too).
(blank) - what you get by default. Field is only visible in this package.
protected - like default, and
the field is visible in subclasses in other packages extended from this
class, too.
private - field is only visible in this
class (and so can never be abstract, as it's not visible to be overridden).
(keywords that modify the way the method is used)
final - cannot be overridden.
static - one per class, not one for each
object.
abstract - must be overridden (to be useful).
protected abstract int total_calories() { ... }
static does, as in the example
below:
class Employee {
String[] name;
long salary;
short employee_number;
static int total_employees;
...
}
Employee class is to store and process
data on an individual employee. However, we also use the class to hold the
total number of employees that we have on the payroll.
total_employees is a quantity associated with
employees in general, so this class might be a good home for it.
total_employees is a value associated with the class as a
whole, not each object of it.
static to a data field makes
this happen.
Employee newhire = new Employee(); newhire.total_employees++; // reference through an instance Employee.total_employees = 0; // reference through the class
class Employee {
String[] name;
long salary;
short employee_number;
static int total_employees;
static void clear() {
total_employees = 0;
}
}
...
newhire.clear(); // reference through an instance
Employee.clear(); // better: reference through the class
clear is not applied to an individual
object, and therefore cannot touch the data fields or call the methods that
belong to individual objects.
public class Employee {
String[] name;
long salary;
short employee_number;
static int total_employees;
static {
System.out.println("Calculating how many employees");
if ( some_condition )
total_employees = 100;
else total_employees = 0;
}
}
static in Java, think
once only.