Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 More data types Character and String –Non-numeric variables –Examples: char orange; String something; –orange and something are variable names –Note.

Similar presentations


Presentation on theme: "1 More data types Character and String –Non-numeric variables –Examples: char orange; String something; –orange and something are variable names –Note."— Presentation transcript:

1 1 More data types Character and String –Non-numeric variables –Examples: char orange; String something; –orange and something are variable names –Note the syntax –char is used to store a single symbol e.g. ‘f’, ‘2’, ‘$’, ‘ ‘, … –String is used to store a sequence of symbols e.g. “My name is George Bush.”

2 2 Even more data types Boolean –Can take two values only: true or false –true and false are two boolean constants –Can’t I declare boolean variables as integers?

3 3 Constants Integer 1, 234, -56, 0, … Floating-point and double 4.5, 56.789, 3.14, 2.71, 0.693, 4.5e3, 45000e-1 Character ‘a’, ‘_’, ‘ ‘, ‘A’, ‘m’, ‘$’, … String “Hi there”, “How are you?”, “This is esc101!!” Boolean true, false You can assign a constant value to a variable.

4 4 Printing characters class printcharacter{ public static void main(String arg[]){ char grade=‘A’; System.out.println(grade);// prints A System.out.println((int)grade); // prints 65 System.out.println(grade+’B’);// prints 131 System.out.println(grade+1);// prints 66 System.out.println((char)(grade+1));// prints B } Notice the type cast Notice the automatic type conversion

5 5 Printing characters class printcharacter{ public static void main(String arg[]){ char grade=‘A’; System.out.println(grade++);// prints A System.out.println(grade); // prints B } Post-increment

6 6 Printing characters class printcharacter{ public static void main(String arg[]){ char grade=‘A’; System.out.println(++grade);// prints B System.out.println(grade); // prints B } Pre-increment

7 7 More type cast class castExample{ public static void main(String arg[]){ double x=Math.PI;// Call to Math class double y=-Math.PI; System.out.println(“Value of PI is: ” + x); System.out.println((int)x); // prints 3 System.out.println((int)y); // prints -3 }

8 8 Operators Arithmetic operators +, -, *, /, % –Addition: a+b –Subtraction: a-b –Multiplication: a*b –Division: a/b (what is this? Find out in lab) –Remainder: a%b Assignment operator = Comparison operators: >, =, <=, ==, !=

9 9 Expressions An expression is a statement involving operators and variables or constants Example: x = a + b; // Add a and b and put the // result in x Example: int x = 6; // Declaration and // initialization int y = x; y = y + 1; // Same as y += 1; // Same as y++; –Two new operators: +=, ++ –More operators: -=, *=, /=, --

10 10 Expressions More examples class SomeThing { public static void main (String args[]) { int x, y; boolean z, w; x = 10; y = 12; z = (x > y); w = (y >= x); System.out.println(“z: ” + z + “,” + “w: ” +w); }

11 11 Expressions More examples class SomeThing { public static void main (String args[]) { int x, y; boolean z, w; x = 10; y = 10; z = (x == y); w = (y != x); System.out.println(“z: ” + z + “,” + “w: ” +w); }

12 12 /* difference between integer and floating point division */ class divisionIntDoub { public static void main (String ars[]) { int var1; double var2; var1 = 1024; var2 = 1024.0; System.out.println("Integer division: "+var1/3); System.out.println("Floating point division: "+var2/3); }

13 13 Celsius to Fahrenheit class CToF{ public static void main(String arg[]){ double C = 40; double F; F = (C/5.0)*9.0 + 32; System.out.println(C + “Celsius is same as ” + F + “ Fahrenheit”); }


Download ppt "1 More data types Character and String –Non-numeric variables –Examples: char orange; String something; –orange and something are variable names –Note."

Similar presentations


Ads by Google