Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 59

The JAVA Language

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.

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

Data Type Data Type

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

The Java Langauge: Data Types

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

The Java Langauge: Data Types

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 Development Kit. A programmer can create new classes to meet the particular needs of a program

12/02/09

The Java Langauge: Data Types

Data Type
8 primitive (fundamental) Data Types Used in
Java:
1. 2. 3. 4.

byte short int long

5. 6. 7. 8.

float double char boolean

12/02/09

The Java Langauge: Data Types

Data Type

Integers

includes byte, short, int, and long for whole-valued signed numbers

Floating-point numbers

includes float and double represent numbers with fractional precision

12/02/09

The Java Langauge: Data Types

Data Type

Characters

char represents symbols in a character set, like letters and numbers.

Boolean

boolean a special type for representing true/false values

12/02/09

The Java Langauge: Data Types

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

32,768 to 32,767 128 to 127


The Java Langauge: Data Types
10

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

The Java Langauge: Data Types

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

The Java Langauge: Data Types

12

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 floating point literal will automatically be of type double.
The Java Langauge: Data Types
13

12/02/09

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 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

The Java Langauge: Data Types

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

The Java Langauge: Data Types

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

a character literal is surrounded with an apostrophe on both sides:

'm'

'y'

'A'

control characters are represented with several characters inside the apostrophes

'\n'

'\t'

\n - 16 bit newline character \t - represents the tabulation character


The Java Langauge: Data Types
19

12/02/09

Question

Is this considered a char literal? "W"

12/02/09

The Java Langauge: Data Types

20

Characters - Strings

Strings

surrounded by double quotes Hello!

12/02/09

The Java Langauge: Data Types

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

The Java Langauge: Data Types

22

Variables and Assignment Statements

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

The Java Langauge: Data Types

24

Variables

payAmount type: long Declaring a variable

where a program says that it needs a variable

type identifier [ = value][, identifier [= value] ...] ;

12/02/09

The Java Langauge: Data Types

25

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

12/02/09

The Java Langauge: Data Types

26

Variables

Which of the following variable declarations are correct?


1. 2. 3. 4. 5. 6. 7.

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

The Java Langauge: Data Types

28

Variables

long payAmount = 123;


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

The Java Langauge: Data Types

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

The Java Langauge: Data Types

30

Variables

Several ways to declare variables:

dataType variableName;
dataType variableName = initialValue ; dataType variableNameOne, variableNameTwo ;

dataType variableNameOne = initialValueOne, variableNameTwo = initialValueTwo ;


The Java Langauge: Data Types
31

12/02/09

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.
// declares an approximation of pi

double pi = 3.14159;

char x = 'x';

// the variable x has the value 'x'

12/02/09

The Java Langauge: Data Types

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

The Java Langauge: Data Types

33

Variables

* means multiply

(hoursWorked * payRate) multiply the number stored in hoursWorked by the number stored in payRate.

+ means to add characters to the end of the


character string So

"Hours Worked: " + hoursWorked the program will print out: Hours Worked: 40
The Java Langauge: Data Types
34

12/02/09

Variables

Output: Hours Worked: 40 pay Amount : 400.0 tax Amount : 40.0

12/02/09

The Java Langauge: Data Types

35

Assignment Statement

assignment statement changes the value that is held in a variable. syntax:


variableName = expression ;

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

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 ); } }


12/02/09

The Java Langauge: Data Types

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

The Java Langauge: Data Types

38

Expressions and Arithmetic Operators

Arithmetic Operators

arithmetic operator a symbol that asks for doing some arithmetic operators of higher precedence are done first

12/02/09

The Java Langauge: Data Types

40

Arithmetic Operators
Operator + * / % + 12/02/09

Meaning unary minus unary plus multiplication division remainder addition subtraction
The Java Langauge: Data Types

Precedence highest highest middle middle middle low low


41

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

The Java Langauge: Data Types

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

example ratio = denom == 0 ? 0 : num / denom;


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

The Java Langauge: Data Types

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

Boolean Logical Operator


Operator
& | ^ || && ! &= |= ^= == != ?:
12/02/09

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

Bitwise Logical Operator


A B 0 1 0 1 0 0 1 1 A|B 0 1 1 1 A&B 0 0 0 1 A^B 0 1 1 0 ~A 1 0 1 0

12/02/09

The Java Langauge: Data Types

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

The Java Langauge: Data Types

Shift Operators

shift operator performs bit manipulation on data by shifting the bits of its first operand right or left.

the shift occurs in the direction indicated by the operator itself

12/02/09

The Java Langauge: Data Types

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

The Java Langauge: Data Types

52

Shift Operators

example2 int i = 8; i >>=2;

1000 0010

answer: i =2

12/02/09

The Java Langauge: Data Types

53

Operator Precedence
Highest

() ++ * + >> > == & ^ | && || ?: =


Lowest
12/02/09

[] -/ >>> >= !=

. ~ % << <

<=

op=

The Java Langauge: Data Types

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

The Java Langauge: Data Types

55

Expressions

subexpression a part of an expression that 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
The Java Langauge: Data Types
56

12/02/09

Expressions

an expression can be written without using any spaces example

(hoursWorked*payRate)-deduction (hoursWorked * payRate) - deduction

The following is NOT correct:

( hours Worked * pay Rate) -deduction


The Java Langauge: Data Types
57

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

The Java Langauge: Data Types

58

Exercise

Find the value of the following expressions


1.
2. 3. 4. 5. 6.

16 - 12 / 4 2+6/2 8 + 4*2 8+4 * 2 12/2 3 6/8 + 2

= = = = = =

6/8 is done using integer division, resulting in 0; then that 0 is added to 2.


The Java Langauge: Data Types
59

12/02/09

SELF TEST

12/02/09

The Java Langauge: Data Types

60

You might also like