Upload
adele-puckett
View
21
Download
0
Embed Size (px)
DESCRIPTION
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 - PowerPoint PPT Presentation
Citation preview
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
• 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)
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
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
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
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
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)
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
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()
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?
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
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 ?
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?
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
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