Professional Documents
Culture Documents
Lecture3 Tha Java Language
Lecture3 Tha Java Language
Comments
Single-line
// ...
Multi-line /* ... */
Good for any comment that will cover more than one line It requires both opening and closing tags.
Javadoc
/** ... */
This is a multi-line comment that the JDKs Javadoc utility can read and turn into HTML documentation. Javadoc has tags you can use to extend its functionality.
11/20/08
Control Statements
Code Block
A code block is everything between the curly braces, and includes the expression that introduces the curly brace part: class MyBlock { ... ... }
11/20/08
Control Statements
a scheme for using bits to represent values. Values are not just numbers, but any kind of data that a computer can process. All values in a computer are represented using one data type or another.
12/02/09
Objects
Java has many data types built into it, and you can create as many more as you want. All data in Java falls into one of two categories: primitive data and objects. There are only eight primitive data types. Any data type you invent will be a type of object.
12/02/09
Objects
An object is a big block of data. An object may use many bytes of memory.
An object usually consists of many internal pieces. The data type of an object is called its class. Many classes are already defined in the Java Development Kit. A programmer can create new classes to meet the particular needs of a program
12/02/09
Data Type
8 primitive (fundamental) Data Types Used in
Java:
1. 2. 3. 4.
5. 6. 7. 8.
12/02/09
Data Type
Integers
includes byte, short, int, and long for whole-valued signed numbers
Floating-point numbers
12/02/09
Data Type
Characters
Boolean
12/02/09
Integers
Name long Width 64 Range
9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
2,147,483,648 to 2,147,483,647
int
short byte
12/02/09
32
16 8
Integers
You can type the number just as you would on a typewriter. This is called a literal. The word "literal" means that a value is explicitly part of the program. Example:
125
- literally represents the value one hundred twenty five.
12/02/09
11
Floating Point
Name double Width in Bits 64 Range 1.7e308 to 1.7e+308 3.4e038 to 3.4e+038
float
32
12/02/09
12
put a lower case 'f' or upper case 'F' at the end: 123.0f -123.5F -198234.234f 0.00000381F
put a lower case 'd' or upper case 'D' at the end, like this: 123.0d -123.5D -198234.234d 0.00000381D
Remember: Without any letter at the end, a floating point literal will automatically be of type double.
The Java Langauge: Data Types
13
12/02/09
Floating Point
1.23E+02
-1.235E+02
3.81E-06
"E" means "times 10 to the power of" The integer that follows it says what power of ten to multiply the rest of the number by. Another way of regarding the integer that follows the "E" is that it says in which direction and for how many places to shift the decimal point.
12/02/09
14
Precision of Float
0.333333333333333333 float data type, has 32 bits only float has 23 bits of precision. (the rest of the bits are used to indicate the sign and size of the number.) equivalent to only about 7 decimal places. the number of places of precision for float is the same no matter what the size of the number
12/02/09
15
Precision of Double
data type double uses 64 bits, and has a much greater range It also has a much greater precision: about 15 significant decimal digits. if you write a literal like 2.345 in a Java program, it will automatically be regarded as a double, even though a float might be good enough.
The Java Langauge: Data Types
16
12/02/09
Characters
char in Java is not the same as char in C Java uses Unicode char is a 16-bit type. The range of a char is 0 to 65,536 there are no negative chars. standard set of characters known as ASCII still ranges from 0 to 127 as always, and the extended 8-bit character set, ISO-Latin-1, ranges from 0 to 255.
The Java Langauge: Data Types
17
12/02/09
Character Literals
'm'
'y'
'A'
control characters are represented with several characters inside the apostrophes
'\n'
'\t'
12/02/09
Question
12/02/09
20
Characters - Strings
Strings
12/02/09
21
Booleans
a simple type for logical values TRUE or FALSE; 0 OR 1 returned by relational operators required by conditional expressions
12/02/09
22
Variables
a name for a location in main memory which uses a particular data type to hold a value basic unit of storage in a Java program defined by the combination of an identifier, a type, and an optional initializer
12/02/09
24
Variables
12/02/09
25
Names
Use only the characters 'a' through 'z', 'A' through 'Z', '0' through '9', character '_', and character '$'.
Do not start with a digit. A name can be any length. Upper and lower case count as different characters.
A name can not be a reserved word. A name must not already be in use
12/02/09
26
Variables
long good-by ; short shrift = 0; double bubble = 0, toil= 9, trouble = 8 byte the bullet ; int double; char thisMustBeTooLong ; int 8ball;
The Java Langauge: Data Types
27
12/02/09
Variables
example
class Example1 { public static void main ( String args [ ] ) { // declaration of a variable long payAmount = 123; System.out.println("The variable contains: " + payAmount ); } }
12/02/09
28
Variables
a declaration of a variable declaration statements is placed between the two braces of the main method it requests a 64 bit section of memory named payAmount which uses the primitive data type long when the program starts running, the variable will initially have the value 123 stored in it.
12/02/09
29
Variables
payAmount type is "long
NOTE:
a variable cannot be used in a program unless it has been declared a variable can be declared only once
12/02/09
30
Variables
dataType variableName;
dataType variableName = initialValue ; dataType variableNameOne, variableNameTwo ;
12/02/09
Examples
int a, b, c;
int d = 3, e, f = 5;
// declares three more ints, initializing d and f.
byte z = 22;
// initializes z.
// declares an approximation of pi
double pi = 3.14159;
char x = 'x';
12/02/09
32
Variables
class Example2 { public static void main ( String args [ ] ) { long hoursWorked = 40; double payRate = 10.0, taxRate = 0.10;
System.out.println("Hours Worked: " + hoursWorked ); System.out.println("pay Amount : " + (hoursWorked * payRate) ); System.out.println("tax Amount : " + (hoursWorked * payRate * taxRate) );
} }
12/02/09
33
Variables
* means multiply
(hoursWorked * payRate) multiply the number stored in hoursWorked by the number stored in payRate.
"Hours Worked: " + hoursWorked the program will print out: Hours Worked: 40
The Java Langauge: Data Types
34
12/02/09
Variables
12/02/09
35
Assignment Statement
equal sign = is the assignment operator. variableName is the name of a variable that has been declared previously in the program expression is a collection of characters that calls for a value
The Java Langauge: Data Types
36
12/02/09
Assignment Statement
long payAmount ;
37
Assignment Statement
semantics of a programming language says what the program does as it executes an assignment statement asks for the computer to perform two steps, in order:
1. 2.
Evaluate the expression (that is: calculate a value.) Store the value in the variable.
example sum = 32 + 8 ;
12/02/09
38
Arithmetic Operators
arithmetic operator a symbol that asks for doing some arithmetic operators of higher precedence are done first
12/02/09
40
Arithmetic Operators
Operator + * / % + 12/02/09
Meaning unary minus unary plus multiplication division remainder addition subtraction
The Java Langauge: Data Types
Operators
Operator
++ += -= *= /= %= -12/02/09
Result
Increment Addition assignment Subtraction assignment Multiplication assignment Division assignment Modulus assignment Decrement
The Java Langauge: Data Types
42
Modulus Operator
modulus operator, %, returns the remainder of a division operation can be applied to floating-point types as well as integer types. example: let x = 42; double y = 42.3;
x % 10 = 2 y % 10 = 2.3
12/02/09
43
the ? Operator
a special ternary (three-way) operator that can replace certain types of if else statements general form: expression1 ?expression2 : expression3
expression1 can be any expression that evaluates to a boolean value if expression1 is true, then expression2 is evaluated; otherwise, expression3 is evaluated
The Java Langauge: Data Types
44
12/02/09
the ? Operator
if denom equals zero, then 0 is assigned to ratio if denom is not equal zero, then num/denom will be executed
12/02/09
45
Relational Operator
Operator
== != > < >= <=
12/02/09
Result
Equal to Not equal to Greater than Less than Greater than or equal to Less than or equal to
The Java Langauge: Data Types
46
Result
Logical AND Logical OR Logical XOR (exclusive OR) Short-circuit OR Short-circuit AND Logical unary NOT AND assignment OR assignment XOR assignment Equal to Not equal to Ternary if-then-else
The Java Langauge: Data Types
47
Bitwise Operator
Operator
~ & | ^ >> >>> << &= |= ^= >>= >>>= <<=
12/02/09
Result
Bitwise unary NOT Bitwise AND Bitwise OR Bitwise exclusive OR Shift right Shift right zero fill Shift left Bitwise AND assignment Bitwise OR assignment Bitwise exclusive OR assignment Shift right assignment Shift right zero fill assignment Shift left assignment
The Java Langauge: Data Types
48
12/02/09
49
Shift Operators
Operator
>> << >>>
Use
op1 >> op2 op1 << op2 op1 >>> op2
Operation
shift bits of op1 right by distance op2 shift bits of op1 left by distance op2 shift bits of op1 right by distance op2 (unsigned)
50
12/02/09
Shift Operators
shift operator performs bit manipulation on data by shifting the bits of its first operand right or left.
12/02/09
51
Shift Operators
example1 13 >> 1;
the following statement shifts the bits of the integer 13 to the right by one position 13 is 1101(in binary) 0110 or 6 in decimal
12/02/09
52
Shift Operators
1000 0010
answer: i =2
12/02/09
53
Operator Precedence
Highest
[] -/ >>> >= !=
. ~ % << <
<=
op=
54
Expressions
expression a combination of literals, operators, variable names, and parentheses used to calculate a value.
literal characters that directly mean a value, like: 3.456 operator a symbol like plus + or times * that asks for an arithmetic operation operand a value that is acted upon by an operator variable a section of memory containing a value. parentheses ()
12/02/09
55
Expressions
13 5 13 & 5 operand operator 5 subexpression (32 - y) / ( x + 5 ) (32 y) & (x + 5) operand / operator /(x+5) subexpression
The Java Langauge: Data Types
56
12/02/09
Expressions
12/02/09
Expressions
Check if the following expression is CORRECT or NOT
1. -153 2. (12 - 3
7.
((m - n) + (w*x+z) / (p % q )
8. (a-b) * (c-d)
3. x + p
4. *z 99 5. -sum / -value 6. 2 - value
9. A - b/c + D
10. -sum + partial 11. ( (x+y) / z ) / ( a - b ) 12. 2( a - b )
12/02/09
58
Exercise
= = = = = =
12/02/09
SELF TEST
12/02/09
60