CS 200 - Week 5 Jim Williams, PhD
The Study Cycle Check Am I using study methods that are effective? Do I understand the material enough to teach it to others? http://students.lsu.edu/academicsuccess/studying/strategies/tests/studying
This Week Exam 1 Next Week Lab: Branches, Eclipse & Styling Lecture: Review and more detail on Primitives, Objects, Branches and Methods
What is the value of msg? boolean flag = true; String msg = "before if"; if ( flag = false); { msg = "" + flag; before if true false
What is print out? char choice = 'a'; switch (choice) { case 'a': System.out.print("a"); case 'b': System.out.print("b"); break; default: System.out.print("other"); break; a b ab other
Scanner Scanner input = new Scanner("1 \ntwo \n 2\n\n"); int a = input.nextint(); if ( input.hasnextint()) { int b = input.nextint(); else { input.nextline(); String line = input.nextline(); System.out.println("#" + line + "#");
Scanner String note = "Hmm\na \na\n3\n\nline note."; Scanner input = new Scanner( note); int num = 0; String str1 = input.nextline(); String str2 = input.next(); if ( input.hasnextint()) { num = input.nextint(); String str4 = input.nextline(); num: 0 str4: 3 num: 0 str4: line note. num: 3 str4: line note. num: 0 str4:
Memory Areas static Code Stack - local, parameters in Stack Frame Heap - instances/objects
Strings and memory class S { public static void main( String []args) { String name; new String( dog ); name = new String( happy );
Where is memory allocated? public static void main( String []args) { String name; name = new String("spot"); new String("fido"); name: heap new String("fido"): heap name: stack new String("fido"): heap name: heap new String("fido"): stack name: stack new String("fido"): stack
What is print out? public static void showfruit( String name) { name = new String("apple"); new String("banana"); public static void main(string[] args) { String name = "grape"; showfruit( name); System.out.println( name); grape apple banana
java.lang.string String stra = "This is a string"; //String literal int lengtha = stra.length(); //odd looking but works. int lengthb = "This is a string".length();
What is print out? String str = null; System.out.print( str.length() > 0); null true false error
Values of a1 and a2? String str1 = "Hello"; String str2 = new String( "Hello"); boolean a1 = str1 == str2; boolean a2 = str1.equals(str2); a1: true a2: true a1: false a2: true a1: true a2: false a1: false a2: false
Values of a1 and a2? char ch1 = 'H'; String str = "Hello"; char ch2 = str.charat(0); boolean a1 = ch1 == ch2; boolean a2 = ch1.equals(ch2); a1: true a2: true a1: false a2: true a1: true a2: false a1: false a2: false
Primitives and Wrapper classes Draw a picture of memory and describe each. public static void main( String []args) { int k; Integer m; k = 2; //example of? m = 3; //example of? k = m; //example of?
Dangling Else - What is out? int value = 0; String out = "abc"; if ( value >= 1 ) if ( value <= 10) out = "1 to 10"; else out = "else"; out: abc out: 1 to 10 out: else out: abcelse
char - Which will compile? int intvalue = 0; char achar = 'B'; intvalue = achar; // 1 achar = intvalue + 1; // 2 achar = '\u0041'; // 3 1, 2, 3 1, 3 1 2, 3
What is print out? static void one() { System.out.print( "one "); static void two(int num) { System.out.print("two "); one(); System.out.print("two "); public static void main( String []args) { two( 3); System.out.print("main "); two one two main main two one two two two one main one two two main
What prints out? static void print( double value) { System.out.println( "print(double)"); static int print( int value) { System.out.print( "print(int)"); return 1; public static void main( String [] args) { print( 10); print(double) print(int) invalid overloading error
What prints out? static void print( double val1, double val2) { System.out.println( "print(double)"); public static void main( String [] args) { double value = 10.0; print( value, 20); static void print( int val1, int val2) { System.out.println( "print(int)"); print(double) print(int) invalid overloading error
What prints out? static void print( double val1, double val2) { System.out.println( "void print"); static int print( double num1, double num2) { System.out.print( "int print"); return 1; public static void main( String [] args) { print( 10.0, 20.0); void print int print invalid overloading other error
What prints out? static void print( double val1, double val2) { System.out.println( "print(double)"); static int something( int val1, int val2) { System.out.print( "something(int)"); return 1; public static void main( String [] args) { print( 10, 20); print(double) something(int) invalid overloading error
Integer overflow int bignum = Integer.MIN_VALUE; bignum -= 1; System.out.println( bignum < 0? "negative" : "non-negative");
Comparing Floats double num1 = 10.3; double num2 = 2.2; double num3 = num1 - num2; if ( num3 == 8.1) { System.out.println( "equal"); else { System.out.println( "not equal"); equal not equal error
Short Circuit evaluation boolean flag = true; if ( flag (flag = false)) { System.out.print( "if: " + flag); else { System.out.print( "else: " + flag); if: true if: false else: true else: false
Compound and unary operators int i = 5; i += 2; i -= -4; i %= 2; System.out.print( i); 0 1 2 3
Number Systems Decimal 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Binary 0, 1
Decimal 100 10 1 3 0 2 = 302
Binary 8 4 2 1 1 1 1 0 = 14
Binary 8 4 2 1 0 1 0 1 = 5
Names for Numbers of Bits 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 bit nibble (4 bits) byte (8 bits) 2 bytes (16)
Hexadecimal (group bits by nibble) 0000 = 0 0001 = 1 0010 = 2 0011 = 3 0100 = 4 0101 = 5 0110 = 6 0111 = 7 1000 = 8 1001 = 9 1010 = A 1011 = B 1100 = C 1101 = D 1110 = E 1111 = F
What character is this? 0000 0000 0100 0001 Unicode: 0x003E 62 > 0x003F 63? 0x0040 64 @ 0x0041 65 A 0x0042 66 B 0x0043 67 C http://www.ssec.wisc.edu/~tomw/java/unicode.html A B C @
Color Red, Green, Blue (RGB) 183, 1, 1 B70101 https://umark.wisc.edu/brand/web/colors.php
What do a and b possibly have in common? public static void main(string []args) { ma(); mb(); static void ma() { int a = 10; static void mb() { int b; System.out.println( b); may be the exact same memory location looking at b will reveal a (unintentially) nothing Error
Is result true or false? Integer m = 5; Integer n = 5; boolean result = m == n; true false error Java Language Specification https://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html