1 / 15

Lecture 2: Topics

Lecture 2: Topics. Bits and Bytes Primitive Types Casting Strings Boolean expressions. Getting Help. Don’t hesitate to drop by my office or send email

olaf
Download Presentation

Lecture 2: Topics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 2: Topics • Bits and Bytes • Primitive Types • Casting • Strings • Boolean expressions

  2. Getting Help • Don’t hesitate to drop by my office or send email • When you send email, use the subject header “honors 101” (if you don’t, and you use an outside address such as yahoo or hotmail, I won’t get your email)

  3. Web page error • The mailing list link was wrong! • If you added your name to the Section 3 mailing list, please delete it and join the Section 1 list instead • The link is now fixed but if your web page is cached you may not see the corrected link: may have to force reload

  4. Bits and Bytes • A bit is either 0 or 1, “on” or “off” • A byte is ___ bits • A kilobyte is ___ bytes • A megabyte is ___ kilobytes • A gigabyte is ___ megabytes • A ______ is _____ gigabytes

  5. Primitive Types • boolean: only 2 values: true and false • In principle, needs only a bit, but uses a byte because of the way addressing is organized • char: 256 different possible character values. Each char variable uses 1 byte • int: for signed whole numbers • Each int variable uses a 32-bit word • byte, short, long: 8, 16, 64-bit variants • float and double: for real numbers (not necessarily whole numbers) • Each float (“floating point”) variable uses a 32-bit word divided into 3 fields: sign, significand and exponent • double (“double floating point”): 64-bit variant

  6. int: possible values • Positive integers from 1 to ________, represented by (0bbb…bbb)2 (31 bits are available for bbb…bbb) • Negative integers from -1 to _______ • _________ • When System.out.println is used to print the value of an int, it automatically calls a routine to convert the value from binary to decimal • See Homework 1

  7. float: possible values • + (1.bbb….bbb)2 x 2E where E ranges from -126 to +127 and there are 23 bits available in the “significand” for bbb…bbb • - (1.bbb….bbb)2 x 2E where E ranges from -126 to +127 and there are 23 bits available in the “significand” for bbb…bbb • Thus floats can store very large and very small numbers, but are limited to 23 “significant bits”: corresponds to about 7 decimal digits • 0 (and -0 !) • Infinity and -Infinity • NaN (Not a Number) • The standard that defines this is called the IEEE Floating Point Standard (1985)

  8. double: possible values • + (1.bbb….bbb)2 x 2E where E ranges from -1022 to +1023 and there are 52 bits available in the “significand” for bbb…bbb • - (1.bbb….bbb)2 x 2E where E ranges from -1022 to +1023 and there are 52 bits available in the “significand” for bbb…bbb • Thus doubles have an even bigger range AND have 52 significant bits: more than int. Corresponds to about 16 decimal digits • ______ • ______ • ______ • When System.out.println is used to print the value of a float or double, it calls a conversion routine to convert the value from binary to decimal

  9. More on float and double • System.out.println(1 – 0.9) does not give exactly 0.1, because the computations are not exact: the answer is “rounded” to the closest “double” value to the exact answer, which is not exactly 0.9 • Notice that “doubles” are displayed to about 16 decimal digits by default, while “floats” are displayed to about 7 digits • The Math class has constants such as Math.PI and (static) methods such as Math.sin(), Math.cos(), Math.exp()

  10. The parallel resistance formula • The resistance of a circuit with two resistors connected in parallel is 1/(1/R1 + 1/R2) • What happens if R1 = 0?

  11. Mixing types in expressions • What is value of “1 / 2” ? ____ • What is value of “1.0/2” ? ____ • Type of the result is the “widest” type in the expression • If the type of the identifier on the left-hand side of an assignment statement has a narrower type than the expression on the right, it must be explicitly “cast” to the narrower type

  12. Casting • Explicit “casting” is needed to convert a wider type to a narrower type • Type width order: byte, short, int, long, float, double (widest) • Example: float f = (float) Math.PI • Example: int i = (int) Math.PI • Does float f = 123456789 require casting? • Is the value of f the same as the value on the right-hand side? • What about double f = 123456789 ?

  13. Converting char to int • What happens: char x = ‘5’; int i = x; System.out.println(i); • If want the integral value corresponding to character x: int i = x – ‘0’; • Why is casting not needed?

  14. Strings • A string is not a primitive data type • It is an object • Details later • For now, need to know about some methods of the String class • String s; // s is a reference to a String object • s.length() is a call to a method that returns the length of the string that s references • s.charAt(int j) is a call to a method that returns the character at position j of the string that s references (first character is position 0, the last character is position s.length() – 1

  15. Boolean expressions • Comparison (relational) operators: >, >=, <, <=, ==, != • Boolean (logical) operators: !, ^, &&, ||, &, | • Is this OK? public static boolean isLeapYear(int year){ return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; } • Note: &&, || are “short-circuit” operators

More Related