sahar mosleh california state university san marcospage 1 data types and operators

12
Sahar Mosleh California State University San Marcos Page 1 Data Types and Operators

Upload: alan-king

Post on 18-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Sahar Mosleh California State University San MarcosPage 1 Data Types and Operators

Sahar Mosleh California State University San Marcos Page 1

Data Types and

Operators

Page 2: Sahar Mosleh California State University San MarcosPage 1 Data Types and Operators

Sahar Mosleh California State University San Marcos Page 2

• Java is a strongly types language

• all operations are types checked by the compiler for type compatibility

• illegal operations will not be compiled

• Java contains two sets of data types • object-oriented data types • non-object-oriented data types

• Java object- oriented data types are defined by classes.• Discussion of these types will be done later in the course

Page 3: Sahar Mosleh California State University San MarcosPage 1 Data Types and Operators

Sahar Mosleh California State University San Marcos Page 3

Primary data types

• Primary data types in Java are:

boolean Represents true or false valuebyte 8-bit integerchar Characterdouble Double-precision floating pointfloat single-precision floating pointint integerlong Long integershort Short integer

Page 4: Sahar Mosleh California State University San MarcosPage 1 Data Types and Operators

Sahar Mosleh California State University San Marcos Page 4

Character variables can be handles like integers

class CharArithDemo{public static void main(String args[]){

char ch;ch = ‘X’;System.out.println (“ch contains ” + ch);ch++; // increment chSystem.out.println (“ch is now ” + ch);ch = 90; // give ch the value ZSystem.out.println( “ch is now ” + ch);

}}

A char can be incremented

A char can be assigned an integer value

Note that although char is not integer type, in some case it can handled as integer

Output:

Ch contains XCh is now Ych is now Z

Page 5: Sahar Mosleh California State University San MarcosPage 1 Data Types and Operators

Sahar Mosleh California State University San Marcos Page 5

Boolean type

• Boolean types represents true/false.

• true and false are reserved words in Java

class BoolDemo{public static void main(String args[]){

boolean b;b = false;System.out.println(“ b is: ” + b);b = true;System.out.println(“b is: ” + b);// a boolean value can control the if statementb = false;if (b)

System.out.println(“This is not executed.”);// outcome of a relational operator is a boolean valueSystem.out.println(“10 > 9 is ” + (10 > 9) );

}}

Output:

b is falseb is true10 > 9 is true

Page 6: Sahar Mosleh California State University San MarcosPage 1 Data Types and Operators

Sahar Mosleh California State University San Marcos Page 6

More on variables

• Initializing a variable:

• One way to give a variable a value is through assignment statement

• For example, int count = 10; // giving count initial

value of 10char ch = ‘X’; // initializing ch to the

value of ‘X’float f = 1.3F // f is initialized with 1.2

• You can also declare two or more variables of the same type using comma-separated list

• For example, int a, b = 8, c = 19, d; // b and c are

initialized

Page 7: Sahar Mosleh California State University San MarcosPage 1 Data Types and Operators

Sahar Mosleh California State University San Marcos Page 7

Operator

• Java has four different classes of operator: arithmetic, bitwise, relational, and logical

• Arithmetic operators include:+ addition- subtraction* multiplication/ division% module++ increment-- decrement

• *, -, +, /, work mainly the same as other languages. • % is the module operator. It works both for integer and floating-

point numbers

Page 8: Sahar Mosleh California State University San MarcosPage 1 Data Types and Operators

Sahar Mosleh California State University San Marcos Page 8

class ModDemo{

public static void main(String args[]){

int iresult, irem;double dresult, drem;iresult = 10 / 3; irem = 10 % 3;drem = 10.0 % 3.0; dresult = 10.0 / 3.0;

System.out.println(“Result and remainder of 10/3: ” + iresult + “ ”+ irem);System.out.println(“Result and remainder of 10.0 /3.0: ” + dresult + “ ” + drem);

}}

output:

Result and remainder of 10 / 3: 3 1Result and remainder of 10.0 / 3.0: 3.3333333333333335 1.0

Page 9: Sahar Mosleh California State University San MarcosPage 1 Data Types and Operators

Sahar Mosleh California State University San Marcos Page 9

• Examples of increment and decrement:

X++ means X = X + 1++X means X = X + 1

Y = X++ means Y = X and X= X+1Y = ++X means X = X+1 and Y = X

• The same logic works for decrement (X-- )

Page 10: Sahar Mosleh California State University San MarcosPage 1 Data Types and Operators

Sahar Mosleh California State University San Marcos Page 10

• Relational operators are:

= = != > < <= >=

• Logical operators are:

& And| OR^ XOR! Not

• The outcome of the relational operators is a boolean value

• The result of a logical operation is also of type boolean

Page 11: Sahar Mosleh California State University San MarcosPage 1 Data Types and Operators

Sahar Mosleh California State University San Marcos Page 11

• Suppose p and q are two boolean objects (literal, variable, expression), the following truth table holds

p q p&q p|q !p !q-------------------------------------------------T T T T F FT F F T F TF T F T T FF F F F T T

• For example, suppose c is a boolean variables and x=10, y = 20c = ( x > 15) && (y = =20)

• In this case since the first operand (x>15) is false, the result of c is false no matter what the result of the second operand (y==20) is.

Page 12: Sahar Mosleh California State University San MarcosPage 1 Data Types and Operators

Sahar Mosleh California State University San Marcos Page 12

The Assignment operator

• The assignment operator is the single equal sign =. The general format of assignment operator is:

var = expression• The type of expression should match the type of the variable

• It is also possible to create a chain of assignments

• For example:int x, y, zx = y = z = 100;

• In this case, going from right to left, 100 is assigned to z which in turn z is assigned to y and the value of y is assigned to x

• Java also supports assignments for the following logical operators

+= -= *= %= &= |= ^=• For example, a+=b means a = a + b