primitive data types and operations identifiers, variables, and constants primitive data types byte,...

34
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting Expressions Operators, Precedence, Associativity, Operand Evaluation Order: ++, --, *, /, %, +=, -=, *=, /=, %=, ^, &, |, +, -, Style and Documentation

Upload: bridget-johnson

Post on 28-Dec-2015

251 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting

Primitive Data Types and Operations

Identifiers, Variables, and ConstantsPrimitive Data Types

Byte, short, int, long, float, double, char, boolean

CastingExpressionsOperators, Precedence, Associativity, Operand Evaluation Order: ++, --, *, /, %, +=, -=, *=, /=, %=, ^, &, |, +, -, Style and Documentation

Page 2: Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting

Identifiers An identifier must start with a letter, an underscore, or a

dollar sign. An identifier cannot contain operators, such as

+, -, and so on. An identifier cannot be a reserved word. (See Appendix

A, “Java Keywords,” for a list of reserved words). An identifier cannot be true, false, ornull.

An identifier can be of any length.

Page 3: Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting

Numerical Data Types

byte 8 bits

short 16 bits

int 32 bits

long 64 bits

float 32 bits

double 64 bits

Page 4: Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting

Number Literals int i = 34;

long l = 1000000;

float f = 100.2f; orfloat f = 100.2F;

double d = 100.2d ordouble d = 100.2D;

Page 5: Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting

Declaring Variablesint x; // Declare x to be an // integer variable;

double radius; // Declare radius to // be a double variable;

char a; // Declare a to be a // character variable;

Page 6: Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting

Assignment Statements

x = 1; // Assign 1 to x;

radius = 1.0; // Assign 1.0 to radius;

a = 'A'; // Assign 'A' to a;

Page 7: Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting

Declaring and Initializingin One Step

int x = 1;

double d = 1.4;

float f = 1.4;

Is this statement correct?

Page 8: Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting

Declaring and Initializingin One Step

int x = 1;

double d = 1.4;

float f = 1.4;

Is this statement correct?

float f = (float)1.4;

Page 9: Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting

Constants

final datatype CONSTANTNAME = VALUE;

final double PI = 3.14159;

final int SIZE = 3;

Page 10: Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting

Operators

+, -, *, /, and %

5/2 yields an integer 2.

5.0/2 yields a double value 2.5

5 % 2 yields 1 (the remainder of the division)

Page 11: Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting

Shortcut Operators

Operator Example Equivalent

+= i+=8 i = i+8

-= f-=8.0 f = f-8.0

*= i*=8 i = i*8

/= i/=8 i = i/8

%= i%=8 i = i%8

Page 12: Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting

Increment andDecrement Operators

x = 1;

y = 1 + x++;

y = 1 + ++x;

y = 1 + x--;

y = 1 + --x;

Using increment and decrement operators makes expressions short, but it also makes them complex and difficult to read. Avoid using these operators in expressions that modify multiple variables, or the same variable for multiple times such as this: int k = ++i + i.

Page 13: Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting

Assignment Expressions and Assignment Statements

Prior to Java 2, all the expressions can be used as statements. Since Java 2, only the following types of expressions can be statements:

variable op= expression; // Where op is +, -, *, /, or %

++variable;

variable++;

--variable;

variable--;

Page 14: Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting

Numeric Type Conversion

Consider the following statements:

byte i = 100;

long k = i*3+4;

double d = i*3.1+l/2;

int x = k; (Wrong)

long k = x;(fine,implicit casting)

Page 15: Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting

Type Casting

doubledouble float long intint short byte

Automaticcasting, type widening

Do narrowing conversion!

Page 16: Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting

Type Casting, cont.

Implicit casting

double d = 3; (type widening)

Explicit casting

int i = (int)3.0; (type narrowing)

float f = 3.12345f;

Page 17: Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting

Type Casting, cont.

What is wrong?

1) int x = 5/2.0;

2) byte b = 50;

b = 2*b;

3) int x = 10;

float f = 2.0*x;

Page 18: Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting

Type Casting, cont.

What is wrong?

1)int x = 5/2.0;-> int x= (int)(5/2.0);

2) byte b = 50;

b = 2*b; -> b = (byte)(2*b);

3) int x = 10;

float f = 2.0*x; ->

-> float f=(float)2.0*x

-> float f=(float)(2.0*x);

Page 19: Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting

Type Casting in expressions If one of operands is long, type of the expression is long.

If one of operands is float, type of the expression is float.

If one of operands is doable, type of the expression is double

byte b = 42; char c = ‘a’;

short s = 1024; int i = 50000;

float f = 5.67f; double d = 0.1234;

double result = (f*b) + (i/c) - (d*s);

(float) (int) (double)

(float) (double)

(double)

Page 20: Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting

Character Data Type

char letter = 'A'; (ASCII)

char numChar = '4'; (ASCII)

char letter = '\u000A'; (Unicode)

char letter = '\u000A'; (Unicode)

Special characters

char tab = ‘\t’;

Page 21: Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting

Unicode Format

Description Escape Sequence Unicode

Backspace \b \u0008

Tab \t \u0009

Linefeed \n \u000a

Carriage return \r \u000d

Page 22: Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting

The boolean Type and Operators

boolean lightsOn = true;

boolean lightsOn = false;

&& (and) (1 < x) && (x < 100) || (or) (lightsOn) || (isDayTime) ! (not) !(isStopped)

Page 23: Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting

The & and | Operators

If x is 1, what is x after this expression?

(1 > x) & ( 1 > x++)

If x is 1, what is x after this expression?

(1 > x) && ( 1 > x++)

How about (1 = x) | (1 > x++)?(1 = x) || (1 > x++)?

Page 24: Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting

Short logical operations

1) Logical operations

if (denom != 0) && (num/denom > 10)

Both operand, left and right ones, will be evaluated.

What is happened if denom is equal to zero

(denom==0)?

2) Short-circuit form of the logical operations

if (denom != 0) &&&& (num/denom > 10)

If the left operand is FALSE, because denom==0,

the total result will be FALSE and the right operand

will not evaluated.

Page 25: Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting

Operator Precedence var++, var— ++var,--var Casting ! *, /, % +, - <, <=, >, >= ==, !=; & ^ | && || =, +=, -=, *=, /=, %=

Page 26: Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting

Operator Associativity

When two operators with the same

precedence are evaluated, the associativity of

the operators determines the order of

evaluation. All binary operators except

assignment operators are left-associative.

a - b + c – d is equivalent to  ((a – b) + c) – d

Assignment operators are right-associative.

Therefore, the expression

a = b += c = 5 is equivalent to a = (b += (c =

5))

Page 27: Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting

Operand Evaluation Order

The precedence and associativity rules specify the

order of the operators, but do not specify the order in

which the operands of a binary operator are

evaluated. Operands are evaluated from left to right in

Java.

The left-hand operand of a binary operator is

evaluated before any part of the right-hand operand is

evaluated.

Page 28: Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting

Operand Evaluation Order, cont.

If no operands have side effects that change the value of a variable, the order of operand evaluation is irrelevant. Interesting cases arise when operands do have a side effect. For example, x becomes 1 in the following code, because a is evaluated to 0 before ++a is evaluated to 1. 

int a = 0;

int x = a + (++a);

But x becomes 2 in the following code, because ++a is evaluated to 1, then a is evaluated to 1.

int a = 0;

int x = ++a + a;

Page 29: Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting

Programming Style and Documentation

Appropriate Comments Naming Conventions Proper Indentation and Spacing Lines Block Styles

Page 30: Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting

Appropriate Comments

Include a summary at the beginning of the program to

explain what the program does, its key features, its

supporting data structures, and any unique techniques it

uses.

Include your name, class section, instruction, date, and a

brief description at the beginning of the program.

Page 31: Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting

Naming Conventions

Choose meaning and descriptive names.

Variables and method names:

– Use lowercase. If the name consists of

several words, concatenate all in one, use

lowercase for the first word, and capitalize

the first letter of each subsequent word in

the name. For example, the variables

radius and area, and the method

computeArea.

Page 32: Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting

Naming Conventions, cont.

Class names: – Capitalize the first letter of each

word in the name. For example, the class name ComputeArea.

Constants: – Capitalize all letters in constants.

For example, the constant PI.

Page 33: Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting

Proper Indentation and Spacing

Indentation– Indent two spaces.

Spacing – Use blank line to separate segments of the code.

Page 34: Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting

Block Styles

Use next-line style for braces.