Professional Documents
Culture Documents
Dasar
Basic Elements
Of Java
Compiling and Running
a Java Application
2
Portable Java Application
3
Java Platform
4
Java Platform SE 7
5
Typical Java Development Environment
Basic Lexical Elements
Character set
– 16-bit Unicode
– Legal characters
Keywords
– Reserved words, special meaning, illegal for identifiers
Identifiers
– Names of declared entities, e.g. variables, constants
Variables
– A variable is a storage location, something that can hold a value
to which a value can be assigned
Literals
– Constants or values, e.g. 12, 17.9, “Hello”
7
Basic Lexical Elements
Other notations
– Operators, e.g. +, -, *, /, etc.
– Block symbols, e.g. pair of {}
Comments
– Help developers, ignored by compiler
– e.g.
/* Program 1 */
// Function to count Circle area
8
Character Set
14
Variables
public class Bicycle {
int cadence = 0; // instance variable
static int wheels = 2; // static variable
…
// formal parameter: decrement
void applyBrakes(int decrement) {
speed = speed - decrement;
}
// local variable: states
void printStates() {
String states = "cadence: "+cadence+
",speed: "+speed+", gear: "+gear;
System.out.println(states);
}
}
15
(Data) Types
16
Primitive Data Types
Type Contains Default Size Range
(for
fields)
boolean true or false 1 bit NA
false
char Unicode '\u0000' 16 bits or 0 to 216-1 or
character
unsigned 2 bytes '\u0000' (0) to '\uFFFF'
(65535)
byte Signed 0 8 bit or -27 to 27-1 or
integer 1 byte -128 to 127
short Signed 0 16 bit or -215 to 215-1 or
integer 2 bytes -32768 to 32767
int Signed 0 32 bit or -231 to 231-1 or
integer 4 bytes -2147483648 to 2147483647
18
Literals
Boolean literals
– Only true and false
Character literals
– Appear with single quotes, e.g. ‘Z’, ‘a’, ‘2’,
‘\u004e’
– Certain special characters can be represented by an escape
sequence, e.g.:
\n newline (\u000A)
\t tab (\u0009)
\b backspace (\u0008)
\r return (\u000D)
\f form feed (\u000C)
\\ backslash itself (\u005C)
\' single quote (\u0027)
\" double quote (\u0022)
19
Literals
Integer literals
– An integer literal is of type long if it ends with the
letter L or l; otherwise it is of type int.
– It is recommended that you use the upper case letter
L because the lower case letter l is hard to
distinguish from the digit 1.
– Values of the integral types byte, short, int, and
long can be created from int literals. Values of
type long that exceed the range of int can be created
from long literals.
20
Literals
Integer literals (cont’d)
– Integer constants are a sequence of octal, decimal,
or hexadecimal digits.
– The start of a constant declares the number's base:
A 0 (zero) starts an octal number (base 8); a 0x or 0X
starts a hexadecimal number (base 16); and any other
digit starts a decimal number (base 10).
– E.g. all the following numbers have the same value
29 035 0x1D 0X1d
21
Literals
Floating-point literals
– A floating-point literal is of type float if it ends
with the letter F or f; otherwise its type is double
and it can optionally end with the letter D or d.
– The floating point types (float and double) can
also be expressed using E or e (for scientific
notation), F or f (32-bit float literal) and D or d (64-
bit double literal; this is the default and by
convention is omitted).
22
Literals
Floating-point literals (cont’d)
– Expressed in either decimal or hexadecimal
– The decimal form consists of a string of decimal digits
with an optional decimal point, optionally followed
by an exponent the letter e or E, followed by an
optionally signed integer.
– e.g. all these literals denote the same floating-point
number:
18. 1.8e1 .18E+2 180.0e-1
23
Literals
Floating-point literals (continued)
– The hexadecimal form consists of 0x (or 0X), a string
of hexadecimal digits with an optional hexadecimal
point, followed by a mandatory binary exponent the
letter p or P, followed by an optionally signed
integer.
– The binary exponent represents scaling by two raised
to a power.
– e.g. all these literals denote the same floating-point
number (decimal 18.0):
0x12p0 0x1.2p4 0x.12P+8 0x120p-4
24
Literals
Floating-point literals (continued)
– There are two zeros: positive (0.0) and negative (-
0.0).
– Positive and negative zero are considered equal
when you use == but produce different results when
used in some calculations.
25
Literals
String literals (continued)
– String literals appear with double quotes, e.g.
“Welcome”, “salam”,
"\u0633\u064e\u0644\u064e\u0627\u0645\u064C".
– Any character can be included in string literals, with the
exception of newline and " (double quote).
– Newlines are not allowed in the middle of strings.
– If you want to embed a newline character in the string, use
the escape sequence \n.
– To embed a double quote use the escape sequence \".
– A string literal references an object of type String.
26