Agenda Topic 2 Topic 2 � Brief History of Java and overview of Java Basics language � Solve a problem to demonstrate Java syntax "On the other hand, Java has already been a big win � Discuss coding issues and style via example scuss cod g ssues a d sty e a e a p e in academic circles where it has taken the place of in academic circles, where it has taken the place of � Slides include more details on syntax Pascal as the preferred tool for teaching the basics of good programming good programming… " – may not cover everything in class, but you are may not cover everything in class but you are - The New Hacker's Dictionary version 4.3.1 expected to know these www.tuxedo.org/~esr/jargon/html/The-Jargon-Lexicon-framed.html www tuxedo org/~esr/jargon/html/The Jargon Lexicon framed html CS 307 Fundamentals of Java Basics CS 307 Fundamentals of Java Basics 1 2 Computer Science Computer Science A 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 time trying to James Gosling and was chartered to spend time trying to figure out what would be the "next wave" of computing and Brief History of Java 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 least one of the waves was going to be the convergence of Overview of Langauge digitally controlled consumer devices and computers. " � Applets and Applications java.sun.com/features/1998/05/birthday.html – " 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 HotJava TM browser It was 1994 WebRunner was just a demo but an 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 Java Basics 3 CS 307 Fundamentals of Java Basics 4 Computer Science Computer Science
How Java Works More on How Java Works � Java's platform independence is achieved by the � J ' l tf i d d i hi d b th � T � To run a Java program the bytecode in a .class file J h b d i l fil use of the Java Virtual Machine is fed to an interpreter which converts the byte code � A Java program consists of one or more files with a A Java program consists of one or more files with a to machine code for a specific chip (IA 32 to machine code for a specific chip (IA-32, .java extension PowerPC) – these are plain old text files � Some people refer to the interpreter as "The Java Some people refer to the interpreter as The Java � When a Java program is compiled the .java files Virtual Machine" (JVM) are fed to a compiler which produces a .class file � The interpreter is platform specific because it takes The interpreter is platform specific because it takes for each .java file for each java file the platform independent bytecode and produces � The .class file contains Java bytecode. machine language instructions for a particular chip � Bytecode is like machine language, but it is � Bytecode is like machine language but it is � So a Java program could be run an any type of intended for the Java Virtual Machine not a specific chip such as a Pentium or PowerPC chip computer that has a JVM written for it. – PC, Mac, Unix, Linux, BeaOS, Sparc CS 307 Fundamentals of Java Basics CS 307 Fundamentals of Java Basics 5 6 Computer Science Computer Science A Picture is Worth… So What! � The platform independence of Java may be a huge The platform independence of Java may be a huge marketing tool, but is actually of little use to people The output of the learning Object Oriented Programming and compiler is .class Ab t Abstract Data Types t D t T file � What is of use is the simplicity of the Java syntax and programming concepts and programming concepts � Java is a "pure" Object Oriented Language – encapsulation, inheritance, and polymorphism 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++ altho gh someone class) like C++, although someone who wants to write ho ants to rite messy Java code certainly can The Interpreter's are sometimes referred to as the Java Virtual – Is OO the best programming paradigm? Machines CS 307 Fundamentals of Java Basics CS 307 Fundamentals of Java Basics 7 8 Computer Science Computer Science
HelloWorld.java More on Java Programs � All code part of some class � All code part of some class /** public class Foo * A simple program { { //start of class Foo //start of class Foo */ /*all code in here!*/ } // end of class Foo public class HelloWorld bli l H ll W ld � The code for class Foo will be in a file { named Foo.java public static void main(String[] args) public static void main(String[] args) – just a text file with the .java extension { – a class is a programmer defined data type System.out.println( HELLO CS307! ); System.out.println("HELLO CS307!"); � A complete program will normally consist of } many different classes and thus many } } different files diff t fil CS 307 Fundamentals of Java Basics CS 307 Fundamentals of Java Basics 9 10 Computer Science Computer Science Attendance Question 1 Attendance Question 2 What does 6,967 * 7,793 equal? How many factors does 54,161,329 have? A. 10,000 A. 2 B. 23,756,201 B. 3 C 54 293 831 C. 54,293,831 C. 4 C 4 D. 2,147,483,647 D. 6 E E. - 2,147,483,648 2 14 483 648 E. more than 6 E h 6 Bonus question. What are they? CS 307 Fundamentals of Java Basics CS 307 Fundamentals of Java Basics 11 12 Computer Science Computer Science
Example Problem Error Types � Determine if a given integer is prime � Syntax error / Compile errors – problem definition – caught at compile time. – really naïve algorithm – compiler did not understand or compiler does not allow – implementation � Runtime error – testing – a small improvement p – something “Bad” happens at runtime. Java breaks these into Errors and Exceptions – another improvement � Logic Error g – yet another improvement y p – program compiles and runs, but does not do – always another way ... what you intended or want – what about really big numbers? (Discover AKS what about really big numbers? (Discover AKS Primality Testing) CS 307 Fundamentals of Java Basics CS 307 Fundamentals of Java Basics 13 14 Computer Science Computer Science Basic Features � D t � Data Types T – primitives – classes / objects l / bj t Java Language � Expressions and operators � Control Structures Review of Basic Features � Arrays � Methods � Programming for correctness g g – pre and post conditions – assertions CS 307 Fundamentals of Java Basics CS 307 Fundamentals of Java Basics 15 16 Computer Science Computer Science
Identifiers in Java � letters digits � letters, digits, _, and $ (don't use $. Can confuse and $ (don't use $ Can confuse the runtime system) � start with letter, , or $ , _, � by convention: 1. start with a letter Java Data Types Java Data Types 2. variables and method names, lowercase with internal 2 variables and method names lowercase with internal words capitalized e.g. honkingBigVariableName 3. constants all caps with _ between internal words e.g. ANOTHER HONKING BIG INDENTIFIER ANOTHER_HONKING_BIG_INDENTIFIER 4. classes start with capital letter, internal words capitalized, all other lowercase e.g HonkingLongClassName HonkingLongClassName � Why? To differentiate identifiers that refer to classes from those that refer to variables CS 307 Fundamentals of Java Basics CS 307 Fundamentals of Java Basics 17 18 Computer Science Computer Science Data Types Java Primitive Data Types � Primitive Data Types yp Data Data Characteristics Characteristics Range Range – byte short int long float double boolean char Type byte 8 bit signed integer -128 to 127 //dataType identifier; yp ; 16 bit signed integer -32768 to 32767 int x; short int y = 10; 32 bit signed integer -2,147,483,648 to 2,147,483,647 int int z, zz; int z zz; long 64 bit signed integer -9,223,372,036,854,775,808 to- double a = 12.0; 9,223,372,036,854,775,807 boolean done = false, prime = true; float float 32 bit floating point 32 bit floating point + 1.4E-45 to + 1 4E-45 to char mi = 'D'; number + 3.4028235E+38 double 64 bit floating point + 4.9E-324 to – stick with int for integers, double for real numbers g number + 1.7976931348623157E+308 � Classes and Objects true or false boolean NA, note Java booleans cannot be converted to or from other types – pre defined or user defined data types consisting of constructors, Unicode character, \u0000 to \uFFFF U code c a acte , \u0000 to \u char 16 bit, Unicode 16 bit, Unicode methods, and fields (constants and fields (variables) which may be methods and fields (constants and fields (variables) which may be Can mix with integer types primitives or objects.) CS 307 Fundamentals of Java Basics CS 307 Fundamentals of Java Basics 19 20 Computer Science Computer Science
Recommend
More recommend