Presentation is loading. Please wait.

Presentation is loading. Please wait.

Constants Variables change, constants don't final = ; final double PI = 3.14159; … area = radius * radius * PI; see Liang, p. 32 for full code.

Similar presentations


Presentation on theme: "Constants Variables change, constants don't final = ; final double PI = 3.14159; … area = radius * radius * PI; see Liang, p. 32 for full code."— Presentation transcript:

1 Constants Variables change, constants don't final = ; final double PI = 3.14159; … area = radius * radius * PI; see Liang, p. 32 for full code

2 Representing numbers Given 3 bits, what numbers can you represent? 111 110 101 100 000 001 010 011 7654012376540123 -4 -3 -2 0 1 2 3 unsigned: Range 0…2 3 -1 (= 0…7) signed: -2 2 …2 2 -1 At base, everything in a computer is stored by bits of memory, which can be either on or off (1 or 0). 2 2 *1 + 2 1 *1+ 2 0 *1= 4 + 2 + 1 = 7 2 2 *1 + 2 1 *1+ 2 0 *0= 4 + 2 + 0 = 6 2 2 *1 + 2 1 *0+ 2 0 *1= 4 + 0 + 1 = 5

3 Numerical data types: integers Name Range Size int -2 31 … 2 31 -1 32 bits (signed) byte -2 7 … 2 7 -1 8 bits (signed) short -2 15 … 2 15 -1 16 bits (signed) We can write big numbers using scientific notation: byte variables can fit numbers from –128 to 127 short variables can fit numbers from –32768 to 32767 int variables can fit numbers from –2147483648 to 2147483647 long -2 63 … 2 63 -1 64 bits (signed) 2147483647 ~ 2.15e+9 ~ 2.15*10 9 long variables can fit ~2.15e+9 times as many numbers as ints

4 Numerical data types: reals float and double are used to store real numbers: numbers with a (possibly unending!) sequence of decimals; for example, pi: double -1.7e308 … 1.7e308 64 (14…15 digits acc.) float -3.4e38 … 3.4e38 32 (6..7 digits accuracy) 3.14159265358979323846264338327950288419716939937 5106… float and double can only store a certain number of decimal digits: they can’t store them all! float pi = 3.141593; \\ the last 3 is a rounding up double pi = 3.1415926535897932 \\the 2 is rounding down

5 What do these mean? int -2 31 … 2 31 -1 32 bits (signed) short -2 15 … 2 15 -1 16 bits (signed) 2 15 = ( 2x2x2x2x2x2x2x2x2x2x2x2x2x2x2 ) = 32,768 How much bigger is 2 31 than 2 15 ?2 16 times bigger 2 31 = 2,147,483,648 float -3.4e38 … 3.4e38 32 bits (signed) 3.4e38 means 3.4 x 10 38 (scientific notation) To compare, 2,147,483,648 ~=2.1 x 10 9

6 Numerical Operators +,-,/,*,% int i1 = 34 + 1; // i1 becomes 35 double d1 = 34.0 - 0.1; // d1 becomes 33.9 double d2 = 1.0/2.0; // d1 becomes 0.5 int i2 = 1/2; // i1 is 0!!!!! byte i3 = 1/2; // same for all integer types byte i4 = 20%3; // modulus operator. i4 is 2

7 Literals A basic data type value which appears directly is a literal int i = 34; // 34 is a literal. double d = 5.0; // 5.0 is a literal, /* floating point values are taken to be doubles until proven otherwise*/ float myFloat = 5.0f; // f means make this a float float myDouble = 5.0d; // d means make it a double!!

8 Shortcut Operators Some operations are so common, we have a shorthand. The following are equivalent: i = i + 1;i += 1; d = d - 1.0;d -= 1.0; f /= 2.0;f = f / 2.0; i++; d--;

9 A useful string operator When applied to two numbers, the + operator adds those numbers When applied to a string and anything else, the + operator joins the other thing on to the end of that string. String myName = “fintan”; System.out.print(“Welcome ”); System.out.print(myName); System.out.println(“ to Java”); Can be replaced by System.out.println(“Welcome “ + myName + “ to Java”);

10 Type conversion Number range hierarchy: double > float > int Assigning smaller range variable to larger range variable is ok: int i = 5; float f = i; // ok double d = f; // also ok

11 Type conversion 2 To assign a larger range value to a smaller variable, use a type cast. This tells the computer to “squeeze” the value into the variable. Caution is always required! You may lose information because you may be putting a big number into a too-small box. float fl = (float)10.1; // double to float. ok. int i = (int)fl; // i now has the value 10! int i2 = 1000; // ok byte b = (byte)i2; // very bad idea!!!!

12 Casting to integers..... REMEMBER! = loses all information after the decimal point. int i = (int)4.1; // i is now 4 double d = -2.5; i = (int)d; // i is now -2

13 Character data type type char holds a single character: char c1 = 'A'; char c2 = '7'; char c3 = '\u0041'; // unicode for A Characters map onto numbers: int code = (int)'A'; Write a program to investigate characters and integers....

14 Special characters char c1 = '\n'; // new line char c2 = '\t'; // tab stop char c3 = '\"'; // double quote More often, we use strings: String s1 = "Hello world"; String s2 = "I said \"Hello world\"\n"; More on Strings later.........

15 The boolean data type A variable of type int can have 2 32 different values A variable of type boolean can have 2 different values: boolean b = true;....... b = false; More on booleans when we consider "if-statements"

16 Homework Read Liang, p. 26 - 48 Try exercises 2.2, 2.3, 2.7, 2.9, 2.14. You will be able to work out the answers for these questions by writing them into Java programs and trying them out.


Download ppt "Constants Variables change, constants don't final = ; final double PI = 3.14159; … area = radius * radius * PI; see Liang, p. 32 for full code."

Similar presentations


Ads by Google