Elementary Programming
Elementary Programming
// Assign a radius
radius = 20;
// Compute area
area = radius * radius * 3.14159;
// Display results
System.out.print("The area for the circle of radius “);
System.out.println(radius + " is " + area);
}
}
IDENTIFIERS
a sequence of characters that consist of letters,
digits, underscores (_), and dollar signs ($).
must start with a letter, an underscore (_), or a
dollar sign ($). It cannot start with a digit.
cannot be a reserved word.
(REFER to list of reserved words).
cannot be true, false, or
null.
can be of any length.
VARIABLES
a data item that may take on more than one value
during the runtime of a program
VARIABLES
// Compute the first area
radius = 1.0;
area = radius * radius * 3.14159;
System.out.println("The area is “ + area
+ " for radius "+radius);
Constants:
Capitalize all letters in constants, and use underscores to
connect words.
For example, the constant PI and MAX_VALUE
NUMERICAL DATA TYPES
Name Range Storage Size
Positive range:
4.9E-324 to 1.7976931348623157E+308
READING INPUT FROM THE CONSOLE
1. Create a Scanner object
Scanner input = new Scanner(System.in);
2. Use the method nextDouble() to obtain to a double
value.
For example,
System.out.print("Enter a double value: ");
Scanner input = new Scanner(System.in);
double d = input.nextDouble();
READING NUMBERS FROM THE KEYBOARD
Scanner input = new Scanner(System.in);
int value = input.nextInt();
Method Description
+ Addition 34 + 1 35
% Remainder 20 % 3 2
INTEGER DIVISION
5 / 2 yields an integer 2.
5.0 / 2 yields a double value 2.5
System.out.println(Math.pow(2, 3));
// Displays 8.0
System.out.println(Math.pow(4, 0.5));
// Displays 2.0
System.out.println(Math.pow(2.5, 2));
// Displays 6.25
System.out.println(Math.pow(2.5, -2));
// Displays 0.16
NUMBER LITERALS
A literal is a constant value that appears directly in
the program.
For example, 34, 1,000,000, and 5.0 are literals in
the following statements:
int i = 34;
long x = 1000000;
double d = 5.0;
INTEGER LITERALS
An integer literal can be assigned to an integer variable as long as it
can fit into the variable.
A compilation error would occur if the literal were too large for the
variable to hold.
For example, the statement byte b = 1000 would cause a compilation
error, because 1000 cannot be stored in a variable of the byte type.
An integer literal is assumed to be of the int type, whose value is
between -231 (-2147483648) to 231–1 (2147483647). To denote an
integer literal of the long type, append it with the letter L or l. L is
preferred because l (lowercase L) can easily be confused with 1 (the
digit one).
FLOATING-POINT LITERALS
Written with a decimal point.
By default, a floating-point literal is treated as a double
type value.
For example, 5.0 is considered a double value, not a
float value.
Appending with F or f make a number a float and
appending the letter d or D make a number a double by
For example,
100.2f or 100.2F for a float number,
100.2d or 100.2D for a double number
AUGMENTED ASSIGNMENT OPERATORS
INCREMENT & DECREMENT OPERATORS
USING INCREMENT & DECREMENT OPERATOR
Explicit casting
int i = (int)3.0; (type narrowing)
int i = (int)3.9; (Fraction part is
truncated)