Download Master

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Introduction to Java
Chapter 2
Basic Elements of Java
Chapter 2 - Basic Elements of Java
1
Introduction to Java
Java Names
• Java names may contain any number of letters,
numbers and underscore (“_”) characters, but they
must begin with a letter
• Standard Java Naming Convention:
– Names beginning with lowercase letters are variables or
methods
– Names beginning with uppercase letters are class names
– Successive words within a name are capitalized
– Names in all capital letters are constants
Chapter 2 - Basic Elements of Java
2
Introduction to Java
Examples of Java Names
Legal names:
myData
x1
AnotherName
TWO_PI
Variable or method names
Class names
Constant
Illegal names:
_toUpper
1Day
Illegal names
Chapter 2 - Basic Elements of Java
3
Introduction to Java
Java Primitive Data Types
• Integer data types
– byte
– short
– int
– long
8 bits
16 bits
32 bits
64 bits
• Real data types - values with a decimal point
– float
– double
32 bits
64 bits
• Other data types
– boolean
– char
1 bit - true or false
16 bits - Unicode character
Chapter 2 - Basic Elements of Java
4
Introduction to Java
Strings
• Strings are special objects containing one or more
characters
• String constants are surrounded by double quotes (")
Example:
"This is a String"
• A double quote within a string is represented by the
escape sequence \":
Example: "She said, \"Hello!\"."
Chapter 2 - Basic Elements of Java
5
Introduction to Java
Strings (2)
• A plus sign (+) between two strings concatenates them
together
Example: "This " + "is" produces "This is"
• A plus sign (+) between a string and a numeric value
converts the numeric value to a string and concatenates
the two strings together:
int j = 17;
"Value = " + j
produces the string "Value
Chapter 2 - Basic Elements of Java
= 17"
6
Introduction to Java
Arithmetic Operations
• The standard arithmetic operations in Java are:
–
–
–
–
–
Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)
Modulus or Remainder (%)
• All of these operations apply to all numeric data
types
Chapter 2 - Basic Elements of Java
7
Introduction to Java
Integer Arithmetic
• Arithmetic operations between integers produce
integer results by truncating the answer —
fractional parts are discarded
• Examples:
3 / 4 = 0
4 / 4 = 1
5 / 4 = 1
• Integer arithmetic is unsuitable for calculations
involving real-world continuous quantities
Chapter 2 - Basic Elements of Java
8
Introduction to Java
Real (Floating-Point) Arithmetic
• Floating-point arithmetic is used with float and
double values. It produces the expected results:
4 / 3 = 1.3333333333333
• However, these numbers have range and precision
limits:
Type
float
double
Range
±1038
±10308
Precision
6-7 decimal digits
15-16 decimal
digits
Chapter 2 - Basic Elements of Java
9
Introduction to Java
Assignment Statements
• An assignment statement in Java has the form
variableName = expression;
• The expression is evaluated, and the result of the
expression is stored in the specified variable.
• Assignment statements and arithmetic operations
can be combined in a single symbol. For example,
variableName += expression;
is equivalent to
variableName = variableName + expression;
–
Chapter 2 - Basic Elements of Java
10
Introduction to Java
Assignment Conversion
• When a value of one type is assigned to a variable
of another type, a type conversion must occur
• If the new data type can represent a wider range of
numbers than the old data type, this is a widening
conversion. Widening conversions are legal.
• If the new data type can represent a lesser range of
numbers than the old data type, this is a narrowing conversion. Narrowing conversions are
illegal, and produce compilation errors.
Chapter 2 - Basic Elements of Java
11
Introduction to Java
Assignment Conversion (2)
• Examples:
int i = -17;
double d;
d = i;
// Widening conversion
int i;
double d = -17.0;
i = d;
// Narrowing conversion
• The first example is legal, while the second one
will produce a compile-time error.
Chapter 2 - Basic Elements of Java
12
Introduction to Java
Casting Conversion
• Narrowing conversions are legal if a programmer
explicitly specifies them. This is done using the
cast operator, which is the name of the new data
type in parentheses. Cast operations assign the
nearest equivalent value in the new data type.
• Examples:
byte b;
double d = 100000.0;
b = (byte) d;
//Legal: result = 127
Chapter 2 - Basic Elements of Java
13
Introduction to Java
Increment and Decrement Operators
• The increment (++) and decrement (--) operators
increase or decrease an integer value by 1.
• Preincrement and predecrement operators
appear before a variable. They increment or
decrement the value of the variable before it is
used in the expression.
• Example:
int i = 4, j = 2, k;
k = ++i - j;
// i = 5, j = 2, k = 3
Chapter 2 - Basic Elements of Java
14
Introduction to Java
Increment and Decrement Operators (2)
• Postincrement and postdecrement operators
appear after a variable. They increment or
decrement the value of the variable after it is used
in the expression.
• Example:
int i = 4, j = 2, k;
k = i++ - j;
// i = 5, j = 2, k = 2
• Keep expressions involving increment and
decrement operators simple!
Chapter 2 - Basic Elements of Java
15
Introduction to Java
Mathematical Methods
• Java’s standard mathematical methods are found
in the Math class. They are invoked by naming
the class name followed by the method name,
separated by a period. For example, the sine of a
value can be calculated as:
y = Math.sin(x);
• A list of standard mathematical methods is found
in Table 2-7.
Chapter 2 - Basic Elements of Java
16
Introduction to Java
Standard Input and Output
• Every Java program has three standard data
streams: standard input, standard output, and
standard error. Each is represented by an object
in the System class.
– System.in represents the standard input stream (the
keyboard)
– System.out represents the standard output stream (the
screen)
– System.err represents the standard error stream (a
special channel to report severe errors on)
Chapter 2 - Basic Elements of Java
17
Introduction to Java
Standard Input and Output (2)
• These objects process input or output data one
byte at a time. It is the responsibility of the
programmer to combine successive bytes into a
useful form, such as a number or a string.
• This process is very complex for the standard
input stream, and it will not be addressed until
Chapter 14.
• For now, we will use the convenience class
chapman.io.StdIn to read data from the keyboard.
Chapter 2 - Basic Elements of Java
18
Introduction to Java
Using Class StdIn
• First, import package chapman.io
• Then create a StdIn object:
StdIn in = new StdIn();
• Finally, read values from the standard input stream
using methods readInt(), readDouble(),
readBoolean(), readString(), etc.
Chapter 2 - Basic Elements of Java
19
Introduction to Java
Standard Output
• Writing to the standard output stream is
accomplished with the print() and println()
methods.
• The difference between these methods is that
println() prints the data and advances the cursor
to a new one, while print() prints the data and
leaves the cursor on the same line
Chapter 2 - Basic Elements of Java
20
Introduction to Java
Using Input and Output
Import class
Read values
with StdIn
methods
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// This program tests reading values using class StdIn
import chapman.io.*;
public class ReadStdIn {
// Define the main method
public static void main(String[] args) {
double v1; int i1; boolean test;
// Create a StdIn object
StdIn in = new StdIn();
Create StdIn object
// Prompt for a double value
System.out.print("Enter a double value: ");
v1 = in.readDouble();
System.out.println("Value = " + v1 );
// Prompt for an int value
System.out.print("Enter an int value: ");
i1 = in.readInt();
System.out.println("Value = " + i1 );
// Prompt for a boolean value
System.out.print("Enter a boolean value: ");
test = in.readBoolean();
System.out.println("Value = " + test );
Write values to
output stream with
print() and
println() methods
}
}
Chapter 2 - Basic Elements of Java
21