Upload
elaine-mae-butalon
View
232
Download
0
Embed Size (px)
Citation preview
7/29/2019 Lecture3 Tha Java Language
1/59
The JAVA Language
7/29/2019 Lecture3 Tha Java Language
2/59
11/20/08 Control Statements 2
Comments
Single-line // ... Suitable for brief remarks on the function or structure of a
statement or expression.
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.
7/29/2019 Lecture3 Tha Java Language
3/59
11/20/08 Control Statements 3
Code Block
A code block is everything between the curlybraces, and includes the expression thatintroduces the curly brace part:
class MyBlock {
...
...
}
7/29/2019 Lecture3 Tha Java Language
4/59
12/02/09 The Java Langauge: Data Types 4
Data Type Data Type
a scheme for using bits to represent values.
Values are not just numbers, but any kind ofdata that a computer can process.
All values in a computer are representedusing one data type or another.
7/29/2019 Lecture3 Tha Java Language
5/59
12/02/09 The Java Langauge: Data Types 5
Objects
Java has manydata types built into it, and you cancreate 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 datatype you invent will be a type of object.
7/29/2019 Lecture3 Tha Java Language
6/59
12/02/09 The Java Langauge: Data Types 6
Objects
A primitive data value uses a small, fixed number of bytes. 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 DevelopmentKit.
A programmer can create new classes to meet the particularneeds of a program
7/29/2019 Lecture3 Tha Java Language
7/59
12/02/09 The Java Langauge: Data Types 7
Data Type
1.
byte2. short
3. int
4. long
5.
float6. double
7. char
8. boolean
8primitive(fundamental) Data Types Used inJava:
7/29/2019 Lecture3 Tha Java Language
8/59
12/02/09 The Java Langauge: Data Types 8
Integers
includes byte, short, int, and long
for whole-valued signed numbers
Floating-point numbers
includes float and double
represent numbers with fractional precision
Data Type
7/29/2019 Lecture3 Tha Java Language
9/59
12/02/09 The Java Langauge: Data Types 9
Characters
char
represents symbols in a character set, like letters
and numbers.
Boolean
boolean a special type for representing true/false values
Data Type
7/29/2019 Lecture3 Tha Java Language
10/59
12/02/09 The Java Langauge: Data Types 10
Name Width Range
long 64 9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
int 32 2,147,483,648 to 2,147,483,647
short 16 32,768 to 32,767
byte 8 128 to 127
Integers
7/29/2019 Lecture3 Tha Java Language
11/59
12/02/09 The Java Langauge: Data Types 11
Integers
You can type the number just as you wouldon 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.
7/29/2019 Lecture3 Tha Java Language
12/59
12/02/09 The Java Langauge: Data Types 12
Floating Point
Name Width in Bits Range
double 64 1.7e308 to1.7e+308
float 32 3.4e038 to3.4e+038
7/29/2019 Lecture3 Tha Java Language
13/59
12/02/09 The Java Langauge: Data Types 13
Explicit Floating Point Literals
single-precision float literal
put a lower case 'f' or upper case 'F' at the end:
123.0f -123.5F -198234.234f 0.00000381F
double-precision double literal.
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 floatingpoint literal will automatically be of type double.
7/29/2019 Lecture3 Tha Java Language
14/59
12/02/09 The Java Langauge: Data Types 14
Floating Point
scientific notation the following are all double-precision literals:
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 tento multiply the rest of the number by.
Another way of regarding the integer that followsthe "E" is that it says in which direction and forhow many places to shift the decimal point.
7/29/2019 Lecture3 Tha Java Language
15/59
12/02/09 The Java Langauge: Data Types 15
Precision of Float
0.333333333333333333
float data type, has 32 bits only
float has 23 bits of precision. (the rest of the bits areused 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 thesame no matter what the size of the number
7/29/2019 Lecture3 Tha Java Language
16/59
12/02/09 The Java Langauge: Data Types 16
Precision of Double
data type double uses 64 bits, and has amuch 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 asa double, even though a float might be goodenough.
7/29/2019 Lecture3 Tha Java Language
17/59
12/02/09 The Java Langauge: Data Types 17
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 theextended 8-bit character set, ISO-Latin-1,ranges from 0 to 255.
http://localhost/var/www/apps/conversion/tmp/scratch_5/Lecture2%20Tha%20Java%20Language.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_5/Lecture2%20Tha%20Java%20Language.ppt7/29/2019 Lecture3 Tha Java Language
18/59
12/02/09 The Java Langauge: Data Types 19
Character Literals
a character literal is surrounded with anapostrophe on both sides:
'm' 'y' 'A'
control characters are represented withseveral characters inside the apostrophes
'\n' '\t' \n - 16 bit newline character
\t - represents the tabulation character
7/29/2019 Lecture3 Tha Java Language
19/59
12/02/09 The Java Langauge: Data Types 20
Question
Is this considered a char literal?"W"
7/29/2019 Lecture3 Tha Java Language
20/59
12/02/09 The Java Langauge: Data Types 21
Strings
surrounded by double quotes
Hello!
Characters - Strings
7/29/2019 Lecture3 Tha Java Language
21/59
12/02/09 The Java Langauge: Data Types 22
Booleans
a simple type for logical values
TRUE or FALSE; 0 OR 1
returned by relational operators
required by conditional expressions
7/29/2019 Lecture3 Tha Java Language
22/59
Variables and
Assignment Statements
7/29/2019 Lecture3 Tha Java Language
23/59
12/02/09 The Java Langauge: Data Types 24
Variables
a name for a location in main memory whichuses 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
7/29/2019 Lecture3 Tha Java Language
24/59
12/02/09 The Java Langauge: Data Types 25
Variables
payAmount
type: long
Declaring a variable
where a program says that it needs a variable
type identifier[ = value][, identifier[= value] ...] ;
7/29/2019 Lecture3 Tha Java Language
25/59
12/02/09 The Java Langauge: Data Types 26
Names for Variables
Names
Use only the characters 'a' through 'z', 'A' through 'Z', '0'through '9', character '_', and character '$'.
A name can not contain the space character.
Do not start with a digit.
A name can be any length.
Upper and lower case count as different characters.
So SUM and Sum are different names.
A name can not be a reserved word.
A name must not already be in use
7/29/2019 Lecture3 Tha Java Language
26/59
12/02/09 The Java Langauge: Data Types 27
Variables
Which of the following variable declarationsare correct?
1. long good-by ;
2. short shrift = 0;3. double bubble = 0, toil= 9, trouble = 8
4. byte the bullet ;
5. int double;
6. char thisMustBeTooLong ;
7. int 8ball;
7/29/2019 Lecture3 Tha Java Language
27/59
12/02/09 The Java Langauge: Data Types 28
example
class Example1 {
public static void main ( String args [ ] ) {
// declaration of a variable
long payAmount = 123;
System.out.println("The variable contains: "
+ payAmount );}
}
Variables
7/29/2019 Lecture3 Tha Java Language
28/59
12/02/09 The Java Langauge: Data Types 29
Variables
long payAmount = 123; a declaration of a variable
declaration statements is placed between the twobraces of the main method
it requests a 64 bit section of memory namedpayAmount which uses the primitive data typelong
when the program starts running, the variable willinitially have the value 123 stored in it.
7/29/2019 Lecture3 Tha Java Language
29/59
12/02/09 The Java Langauge: Data Types 30
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
7/29/2019 Lecture3 Tha Java Language
30/59
12/02/09 The Java Langauge: Data Types 31
Variables
Several ways to declare variables:
dataType variableName;
dataType variableName = initialValue ;
dataType variableNameOne, variableNameTwo ;
dataType variableNameOne = initialValueOne,variableNameTwo = initialValueTwo ;
7/29/2019 Lecture3 Tha Java Language
31/59
12/02/09 The Java Langauge: Data Types 32
Examples
int a, b, c; // declares three ints, a, b, and c.
int d = 3,e, f = 5;
// declares three more ints, initializing d and f.
byte z = 22; // initializes z.
double pi = 3.14159; // declares an approximation of pi
char x = 'x'; // the variable x has the value 'x'
7/29/2019 Lecture3 Tha Java Language
32/59
12/02/09 The Java Langauge: Data Types 33
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) );
}}
7/29/2019 Lecture3 Tha Java Language
33/59
12/02/09 The Java Langauge: Data Types 34
Variables
* means multiply (hoursWorked * payRate)
multiply the number stored in hoursWorked by thenumber stored in payRate.
+ means to add charactersto the end of thecharacter string So
"Hours Worked: " + hoursWorked the program will print out:
Hours Worked: 40
7/29/2019 Lecture3 Tha Java Language
34/59
12/02/09 The Java Langauge: Data Types 35
Variables
Output:
Hours Worked: 40
pay Amount : 400.0
tax Amount : 40.0
7/29/2019 Lecture3 Tha Java Language
35/59
12/02/09 The Java Langauge: Data Types 36
Assignment Statement
assignment statement changes the valuethat is held in a variable.
syntax:
variableName = expression ;
equal sign = is the assignment operator.
variableNameis the name of a variable that has been
declared previously in the program
expressionis a collection of characters that calls for avalue
7/29/2019 Lecture3 Tha Java Language
36/59
12/02/09 The Java Langauge: Data Types 37
Assignment Statement
class Example3 {public static void main ( String args [ ]) {
//a declaration without an initial value
long payAmount ;
//an assignment statement
payAmount = 123;
System.out.println("The variable contains: " +payAmount );
}
}
7/29/2019 Lecture3 Tha Java Language
37/59
12/02/09 The Java Langauge: Data Types 38
semantics of a programming language says whatthe program does as it executes
an assignment statement asks for the computer toperform two steps, in order:
1. Evaluate the expression (that is: calculate a value.)
2. Store the value in the variable.
example sum = 32 + 8 ;
Assignment Statement
7/29/2019 Lecture3 Tha Java Language
38/59
Expressions and
Arithmetic Operators
7/29/2019 Lecture3 Tha Java Language
39/59
12/02/09 The Java Langauge: Data Types 40
Arithmetic Operators
arithmetic operator a symbol that asks fordoing some arithmetic
operators of higherprecedence are donefirst
7/29/2019 Lecture3 Tha Java Language
40/59
12/02/09 The Java Langauge: Data Types 41
Arithmetic Operators
Operator Meaning Precedence
- unary minus highest
+ unary plus highest
* multiplication middle
/ division middle
% remainder middle
+ addition low
- subtraction low
7/29/2019 Lecture3 Tha Java Language
41/59
12/02/09 The Java Langauge: Data Types 42
Operators
Operator Result
++ Increment
+= Addition assignment-= Subtraction assignment
*= Multiplication assignment
/= Division assignment
%= Modulus assignment
- - Decrement
7/29/2019 Lecture3 Tha Java Language
42/59
12/02/09 The Java Langauge: Data Types 43
Modulus Operator
modulus operator, %, returns the remainderof 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
7/29/2019 Lecture3 Tha Java Language
43/59
12/02/09 The Java Langauge: Data Types 44
the ? Operator
a special ternary(three-way) operatorthatcan replace certain types ofif elsestatements
general form:expression1 ?expression2: expression3
expression1 can be any expression that evaluatesto a boolean value
ifexpression1 is true, then expression2isevaluated; otherwise, expression3is evaluated
7/29/2019 Lecture3 Tha Java Language
44/59
12/02/09 The Java Langauge: Data Types 45
the ? Operator
example
ratio = denom == 0 ? 0 : num / denom;
ifdenom equals zero, then 0 is assigned to ratio
ifdenom is not equal zero, then num/denom will
be executed
7/29/2019 Lecture3 Tha Java Language
45/59
12/02/09 The Java Langauge: Data Types 46
Relational Operator
Operator Result
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
7/29/2019 Lecture3 Tha Java Language
46/59
12/02/09 The Java Langauge: Data Types 47
Boolean Logical Operator
Operator 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
7/29/2019 Lecture3 Tha Java Language
47/59
12/02/09 The Java Langauge: Data Types 48
Bitwise Operator
Operator Result~ Bitwise unary NOT& Bitwise AND| Bitwise OR^ Bitwise exclusive OR
>> Shift right>>> Shift right zero fill>= Shift right assignment>>>= Shift right zero fill assignment
7/29/2019 Lecture3 Tha Java Language
48/59
12/02/09 The Java Langauge: Data Types 49
Bitwise Logical Operator
A B A | B A & B A ^ B ~A
0 0 0 0 0 1
1 0 1 0 1 0
0 1 1 0 1 1
1 1 1 1 0 0
7/29/2019 Lecture3 Tha Java Language
49/59
12/02/09 The Java Langauge: Data Types 50
Shift Operators
Operator Use Operation
>> op1 >> op2 shift bits of op1 right bydistance op2
> op1 >>> op2 shift bits of op1 right bydistance op2(unsigned)
7/29/2019 Lecture3 Tha Java Language
50/59
12/02/09 The Java Langauge: Data Types 51
shift operator performs bit manipulation ondata by shifting the bits of its first operandright or left.
the shift occurs in the direction indicated bythe operator itself
Shift Operators
7/29/2019 Lecture3 Tha Java Language
51/59
12/02/09 The Java Langauge: Data Types 52
example1
13 >> 1;
the following statement shifts the bits of theinteger 13 to the right by one position
13 is 1101(in binary)
0110 or 6 in decimal
Shift Operators
7/29/2019 Lecture3 Tha Java Language
52/59
12/02/09 The Java Langauge: Data Types 53
example2
int i = 8; 1000
i >>=2; 0010
answer: i =2
Shift Operators
7/29/2019 Lecture3 Tha Java Language
53/59
12/02/09 The Java Langauge: Data Types 54
Operator PrecedenceHighest
( ) [ ] .++ -- ~ !* / %+ ->> >>> >= <
7/29/2019 Lecture3 Tha Java Language
54/59
12/02/09 The Java Langauge: Data Types 55
Expressions
expression
a combination of literals, operators,variable names, and parentheses used to calculatea 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 ()
7/29/2019 Lecture3 Tha Java Language
55/59
12/02/09 The Java Langauge: Data Types 56
Expressions
subexpression a part of an expressionthat is by itself a correct expression
example 13 5
13& 5 operand
operator
5 subexpression
(32 - y) / ( x + 5 )
(32 y) & (x + 5) operand
/operator
/(x+5) subexpression
7/29/2019 Lecture3 Tha Java Language
56/59
12/02/09 The Java Langauge: Data Types 57
Expressions
an expression can be written without usingany spaces
example (hoursWorked*payRate)-deduction
(hoursWorked * payRate) - deduction
The following is NOT correct: ( hours Worked * pay Rate) -deduction
7/29/2019 Lecture3 Tha Java Language
57/59
12/02/09 The Java Langauge: Data Types 58
Expressions
1. -153
2. (12 - 33. x + p
4. *z 99
5. -sum / -value
6. 2 - value
7. ((m - n) + (w*x+z) / (p % q )
8. (a-b) * (c-d)9. A - b/c + D
10. -sum + partial
11. ( (x+y) / z ) / ( a - b )
12. 2( a - b )
Check if the following expression isCORRECT orNOT
7/29/2019 Lecture3 Tha Java Language
58/59
12/02/09 The Java Langauge: Data Types 59
Exercise
Find the value of the following expressions
1. 16 - 12 / 4 =
2. 2 + 6 / 2 =
3. 8 + 4*2 =
4. 8+4 * 2 =
5. 12/2 3 =
6. 6/8 + 2 = 6/8 is done using integer division, resulting in 0;
then that 0 is added to 2.
7/29/2019 Lecture3 Tha Java Language
59/59
SELF TEST