University of British Columbia Reading This Week Review: High-Level Language Review: Java Does Both! CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 ■ Chap 1: 1.3-1.8 ■ Must be translated into machine language so the computer Your Program.java can understand it. ■ Chap 2: 2.1-2.2, 2.5 (Java) Tamara Munzner javac ■ Chap 4: 4.1-4.2 ■ High-level instruction: A = B + C Compiler becomes at least four machine language instructions! java Whitespace, Errors, Variables, JVM interpreter (Mac) ■ reminder: weekly reading questions due next time Your Program.class Data Types, Assignment 00010000001000000000000000000010 load B (Java Bytecodes) (Fri) at start of lecture java 00010000010000000000000000000011 load C java JVM interpreter (Win) 00000000001000100011000000100000 add them JVM interpreter (Unix) Lecture 4, Wed Jan 13 2010 00010100110000000000000000000001 store in A borrowing from slides by Kurt Eiselt ■ How? Windows PC Macintosh SPARC Server ■ You could translate it as you go ( interpreter ). ■ You could translate it in advance ( compiler ). http://www.cs.ubc.ca/~tmm/courses/111-10 1 2 3 4 Review: Comments Review: Identifiers Review: Reserved Words Review: Identifiers public class Oreo ■ Comments: help humans understand ■ Get familiar with these ■ Identifier must { ■ Start with a letter and be followed by ■ ignored by compiler public static void main (String[] args) ■ But you don’t need to memorize all 52 for exam { ■ Zero or more letters and/or digits ■ comment out rest of line: // System.out.println ("Feed me more Oreos!"); } ■ Digits are 0 through 9. ■ comment start/end: /* */ } ■ Letters are the 26 characters in English alphabet abstract do if private throw ■ Words we use when writing programs are called boolean double implements protected throws ■ both uppercase and lowercase break else import public transient identifiers ■ plus the $ and _ byte enum instanceof return true ■ also alphabetic characters from other languages case extends int short try ■ except those inside the quotes ■ Which of the following are not valid identifiers? catch false interface static void ■ Kurt made up identifier Oreo char final long strictfp volatile class finally native super while ■ Other programmers chose identifier System.out.println const float new switch continue for null synchronized default goto package this userName user_name $cash 2ndName first name user.age _note_ note2 5 6 7 8 Identifiers Identifiers White Space White Space ■ Java is case sensitive ■ Creating identifiers in your Java programs //******************************************************* //******************************************************* ■ Oreo oreo OREO 0reo ■ Remember other people read what you create // Oreo.java Author: Kurt Eiselt // Oreo1.java Author: Kurt Eiselt // // ■ are all different identifiers, so be careful ■ Make identifiers meaningful and descriptive for both // Demonstrating good use of white space // Demonstrating mediocre use of white space //******************************************************* //******************************************************* you and them ■ common source of errors in programming ■ No limit to how many characters you can put in your public class Oreo public class Oreo1 { { identifiers ■ are these all valid identifiers? public static void main (String[] args) public static void main (String[] args) { { ■ but don’t get carried away System.out.println ("Feed me more Oreos!"); System.out.println ("Feed me more Oreos!"); } } } } public class ReallyLongNamesWillDriveYouCrazyIfYouGoOverboard { public static void main (String[] args) { System.out.println ("Enough already!"); } 9 10 11 12 } //******************************************************* White Space White Space White Space // Oreo4.java Author: Kurt Eiselt // // Demonstrating deep psychological issues with whitespace ■ White space //******************************************************* //******************************************************* //******************************************************* ■ Blanks between identifiers and other symbols // Oreo2.java Author: Kurt Eiselt // Oreo3.java Author: Kurt Eiselt public // // ■ Tabs and newline characters are included class // Demonstrating bad use of white space // Demonstrating totally bizarre use of white space Oreo4 //******************************************************* //******************************************************* { ■ White space does not affect how program runs public White Space public class Oreo2 { public static void main (String[] public args) { System.out.println ("Feed me more Oreos!"); } } class Oreo3 static void { ■ Use white space to format programs we create so they’re main public static ( easier for people to understand void main (String[] args) String[] { args System.out.println ("Feed me more Oreos!") ) ; { } } System.out.println ("Feed me more Oreos!") ; } 13 14 15 16 }
Recommend
More recommend