Topic 2Topic 2 Java Basics"On the other hand, Java has already been a big winin academic circles where it has taken the place ofin academic circles, where it has taken the place of Pascalas the preferred tool for teaching the basics of good programming"good programming…-The New Hacker's Dictionary version 4.3.1www tuxedo org/~esr/jargon/html/TheJargonLexiconframed htmlwww.tuxedo.org/~esr/jargon/html/The-Jargon-Lexicon-framed.htmlCS 307 Fundamentals of Computer ScienceJava Basics1AgendaBrief History of Java and overview of languageSolve a problem to demonstrate Java syntaxDiscuss coding issues and style via examplescuss cod g ssues a d sty e a e a p eSlides include more details on syntaxmay not cover everything in class but you are–may not cover everything in class, but you are expected to know theseCS 307 Fundamentals of Computer ScienceJava Basics2Brief History of Java and Overview of Langaugejava.sun.com/features/1998/05/birthday.htmlCS 307 Fundamentals of Computer ScienceJava Basics3A brief history of Java–"Java whose original name was Oak was developed as a–Java, whose original name was Oak, was developed as a part of the Green project at Sun. It was started in December '90 by Patrick Naughton, Mike Sheridan and James Gosling and was chartered to spend timetryingtoJames Gosling and was chartered to spend time trying to figure out what would be the "next wave" of computing and how we might catch it. They came to the conclusion that at least one of the waves was going to be the convergence ofleast one of the waves was going to be the convergence of digitally controlled consumer devices and computers. "Applets and Applications–"The team returned to work up a Java technology-based clone of Mosaic they named "WebRunner" (after the movie Blade Runner), later to become officially known as the HotJavaTMbrowser It was 1994WebRunnerwas just ademobut anbrowser. It was 1994. WebRunnerwas just a demo, but an impressive one: It brought to life, for the first time, animated, moving objects and dynamic executable content inside a Web browser. That had never been done. [At the TED CS 307 Fundamentals of Computer ScienceJava Basics4[conference.]"How Java WorksJ'ltf id d i hidbthJava's platform independence is achieved by the use of the Java Virtual MachineA Java program consists of one or more files with aA Java program consists of one or more files with a .java extension– these are plain old text filesWhen a Java program is compiled the .java files are fed to a compiler which produces a .class file for each java filefor each .java fileThe .class file contains Java bytecode. Bytecode is like machine language but it isBytecode is like machine language, but it is intended for the Java Virtual Machine not a specific chip such as a Pentium or PowerPC chipCS 307 Fundamentals of Computer ScienceJava Basics5More on How Java WorksT J h b d i l filTo run a Java program the bytecode in a .class file is fed to an interpreter which converts the byte code to machine code for a specific chip (IA32to machine code for a specific chip (IA-32, PowerPC) Some people refer to the interpreter as"The JavaSome people refer to the interpreter as The Java Virtual Machine" (JVM) The interpreter is platform specific because it takesThe interpreter is platform specific because it takes the platform independent bytecode and produces machine language instructions for a particular chipSo a Java program could be run an any type of computer that has a JVM written for it.CS 307 Fundamentals of Computer ScienceJava Basics6– PC, Mac, Unix, Linux, BeaOS, Sparc A Picture is Worth…The output of the compiler is .class fileThe Interpreter's are sometimes referred to as the Java Virtual CS 307 Fundamentals of Computer ScienceJava Basics7MachinesSo What!The platform independence of Java may be a hugeThe platform independence of Java may be a huge marketing tool, but is actually of little use to people learning Object Oriented Programming and Ab t t D t TAbstract Data TypesWhat is of use is the simplicity of the Java syntax and programming conceptsand programming conceptsJava is a "pure" Object Oriented Language–encapsulation, inheritance, and polymorphismencapsulation, inheritance, and polymorphism– all code must be contained in a class– no free functions (functions that do not belong to some class) like C++ altho gh someone ho ants to riteclass) like C++, although someone who wants to write messy Java code certainly can– Is OO the best programming paradigm?CS 307 Fundamentals of Computer ScienceJava Basics8HelloWorld.java/*** A simple program*/bli l H ll W ldpublic class HelloWorld{public static void main(String[] args)public static void main(String[] args){System.out.println("HELLO CS307!");System.out.println( HELLO CS307! );}}CS 307 Fundamentals of Computer ScienceJava Basics9}More on Java ProgramsAll code part of someclassAll code part of some classpublic class Foo {//start of class Foo{//start of class Foo /*all code in here!*/ } // end of class FooThe code for class Foo will be in a file named Foo.java–just a text file with the .java extension– a class is a programmer defined data typeA complete program will normally consist of many different classes and thus many diff t filCS 307 Fundamentals of Computer ScienceJava Basics10different filesAttendance Question 1What does 6,967 * 7,793 equal?A. 10,000B. 23,756,201C 54 293 831C. 54,293,831D. 2,147,483,647E2 14 483 648E. -2,147,483,648CS 307 Fundamentals of Computer ScienceJava Basics11Attendance Question 2How many factors does 54,161,329 have?A. 2B. 3C4C. 4D. 6Eh6E. more than 6Bonus question. What are they?CS 307 Fundamentals of Computer ScienceJava Basics12Example ProblemDetermine if a given integer is prime– problem definition– really naïve algorithm– implementation– testing–a small improvementp– another improvement–yet another improvementyp– always another way ...–what about really big numbers? (Discover AKSCS 307 Fundamentals of Computer ScienceJava Basics13what about really big numbers? (Discover AKS Primality Testing)Error TypesSyntax error / Compile errors– caught at compile time.– compiler did not understand or compiler does not allowRuntime error– something “Bad” happens at runtime. Java breaks these into Errors and ExceptionsLogic Errorg– program compiles and runs, but does not do what you intended or wantCS 307
View Full Document