Topic 2 Java BasicsAgendaBrief History of Java and Overview of LangaugeSlide 4How Java WorksMore on How Java WorksA Picture is Worth…So What!HelloWorld.javaMore on Java ProgramsAttendance Question 1Attendance Question 2Example ProblemError TypesJava Language Review of Basic FeaturesBasic FeaturesJava Data TypesIdentifiers in JavaData TypesJava Primitive Data TypesWhat are Classes and Objects?Creating and Using ObjectsBuilt in ClassesimportMore on importThe String ClassStandard OutputConstantsExpressions and OperatorsOperatorsExpressionsCastingPrimitive CastingJava Control StructuresControl StructuresBoolean ExpressionsMore Flow of Controlfor Loopswhile loopsAttendance Question 3Attendance Question 4ArraysArrays in JavaArray DetailsArray InitializationArrays of objectsArray UtilitiesThe arraycopy method2D Arrays in JavaTwo Dimensional ArraysThe Real PictureArrays of Multiple DimensionRagged ArraysEnhanced for loopSlide 55Attendance Question 5Attendance Question 6MethodsSlide 59More on Methodsstatic methodsMethod Overloading and ReturnMethod ParametersValue Parameters vs. Reference ParametersValue vs. ReferenceProgramming for CorrectnessCreating Correct ProgramsProgramming by ContractThinking about pre and postconditionsPrecondition ExampleSlide 71AssertionsAssertions Uncover Errors in Your LogicjavadocCS 307 Fundamentals of Computer ScienceJava Basics1Topic 2 Java Basics"On the other hand, Java has already been a big win in academic circles, where it has taken the place of Pascal as the preferred tool for teaching the basics of good programming…"-The New Hacker's Dictionary version 4.3.1www.tuxedo.org/~esr/jargon/html/The-Jargon-Lexicon-framed.htmlCS 307 Fundamentals of Computer ScienceJava Basics2AgendaBrief History of Java and overview of languageSolve a problem to demonstrate Java syntaxDiscuss coding issues and style via exampleSlides include more details on syntax–may not cover everything in class, but you are expected to know theseCS 307 Fundamentals of Computer ScienceJava Basics3Brief History of Java and Overview of Langaugejava.sun.com/features/1998/05/birthday.htmlCS 307 Fundamentals of Computer ScienceJava Basics4A brief history of Java–"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 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 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 HotJavaTM browser. It was 1994. WebRunner was 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 conference.]"CS 307 Fundamentals of Computer ScienceJava Basics5How Java WorksJava's platform independence is achieved by the use of the Java Virtual MachineA 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 fileThe .class file contains Java bytecode. Bytecode 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 Basics6More on How Java WorksTo 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 (IA-32, PowerPC) Some people refer to the interpreter as "The Java Virtual Machine" (JVM) The 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.–PC, Mac, Unix, Linux, BeaOS, SparcCS 307 Fundamentals of Computer ScienceJava Basics7A Picture is Worth…The Interpreter's are sometimes referred to as the Java Virtual MachinesThe output of the compiler is .class fileCS 307 Fundamentals of Computer ScienceJava Basics8So What!The platform independence of Java may be a huge marketing tool, but is actually of little use to people learning Object Oriented Programming and Abstract Data TypesWhat is of use is the simplicity of the Java syntax and programming conceptsJava is a "pure" Object Oriented Language–encapsulation, inheritance, and polymorphism–all code must be contained in a class–no free functions (functions that do not belong to some class) 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 Basics9HelloWorld.java/** * A simple program */public class HelloWorld{public static void main(String[] args){System.out.println("HELLO CS307!");}}CS 307 Fundamentals of Computer ScienceJava Basics10More on Java ProgramsAll code part of some class public 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 different filesCS 307 Fundamentals of Computer ScienceJava Basics11Attendance Question 1What does 6,967 * 7,793 equal?A. 10,000B. 23,756,201C. 54,293,831D. 2,147,483,647E. - 2,147,483,648CS 307 Fundamentals of Computer ScienceJava Basics12Attendance Question 2How many factors does 54,161,329 have?A. 2B. 3C. 4D. 6E. more than 6Bonus question. What are they?CS 307 Fundamentals of Computer ScienceJava Basics13Example ProblemDetermine if a given integer is prime–problem definition–really naïve algorithm–implementation–testing–a small improvement–another improvement–yet another improvement–always another way ...–what about really big numbers? (Discover AKS Primality Testing)CS 307 Fundamentals of Computer ScienceJava Basics14Error TypesSyntax error / Compile errors–caught at compile time.–compiler did not
View Full Document