Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Types and Operators  Two category of data types:  Primitive type: value type. Store values.  Object type: reference type. Store addresses.

Similar presentations


Presentation on theme: "Data Types and Operators  Two category of data types:  Primitive type: value type. Store values.  Object type: reference type. Store addresses."— Presentation transcript:

1 Data Types and Operators  Two category of data types:  Primitive type: value type. Store values.  Object type: reference type. Store addresses.

2 Data Types and Operators  Primitive types:  int  double  String (?)  boolean

3 Data Types and Operators  Operators  Numerical operators: + - * / %  Relational operators: == != > >= < <=  String cancatenation: + Examples: “ABC” + “DEF”  “ABCDEF” “The average is “ + average

4 Data Types and Operators  Short cuts:  a = a + b;  a += b;  a = a – b;  a -= b;  a = a * b;  a *= b;  a = a / b;  a /= b;  a = a % b;  a %= b;

5 Data Types and Operators  Increment and decrement  Increment ++: increase … by 1 int a=3; a++  the value of a is 4 ++a  the value of a is 4

6 Data Types and Operators  Increment and decrement  Decrement -- : decrease … by 1 int a=5; a--  the value of a is 4 --a  the value of a is 4

7 Data Types and Operators  Increment and decrement  pre and post ++a: preincrement a++: postincrement --a: predecrement a--: postdecrement

8 Data Types and Operators  Increment and decrement  Stand-alone: no difference int a = 10; ++a and a++  a has a new value 11. --a and a--  a has a new value 9.

9 Data Types and Operators  Increment and decrement  In expression evaluation: different  pre --- increase the value by 1 before evaluating the expression  post --- evaluate the expression before increasing the value by 1

10 Data Types and Operators  Increment and decrement  Examples: int a=10; int b; b = ++a; /* now a = 11, b=11 */ b = a++; /* now a = 11, b=10 */

11 Data Types and Operators  Increment and decrement  Examples: int a=10; int b; b = ++a + 4; /* now a = 11, b=15 */ b = a++ + 4; /* now a = 11, b=14 */

12 Data Types and Operators  char and String types:  Examples: ‘A’, ‘B’,…, ‘a’, ‘b’, ‘$’, ‘*’,… are all constants (literals) of char char a=‘A’; int i = (int)’A’; int j = ‘H’ + ‘e’ + ‘l’ + ‘l’ +’o’;

13 Data Types and Operators  char and String types:  char is a single char, is an integer type in many languages.  Enclosed in a pair of single quotes.  Its value is the ASCII value.

14 Data Types and Operators  char and String types:  String contains zero or many chars  Enclosed in a pair of double quotes.  char and String are totally different types. A String has no numerical values.  Operator on String: +

15 Data Types and Operators  cast:  byte  short  int  long  float  double: precision increases  Assigning a value of high precision to a low precision variable loses precision, and needs cast

16 Data Types and Operators  cast:  var = (type)expr examples: int a = 10; double d=3.14; d = a; /* ok */ a = d; /* error */ a = (int)d; /* ok with cast */

17 Programming Problems 1. Write a program that reads the radius (double) of a circle from the console, computes the area of the circle and displays the result on the console. 2. Write a program to calculate and display the total restaurant bill after reading the amount of food and drink, tax rate, and tip percentage from the console. The display should itemize different items.

18 Programming Problems 3. Write a program reads an integer between 0 and 999 and calculates the sum of the digits. 4. Write a program to calculate the net pay after reading gloss pay, fed and state tax rates from the console. Programs 3 and 4 --- homework, send the source as an email to tangy@sunysuffolk.edu as an attachment.


Download ppt "Data Types and Operators  Two category of data types:  Primitive type: value type. Store values.  Object type: reference type. Store addresses."

Similar presentations


Ads by Google