Introduction To Java Larry Stead, Instructor lss2168@columbia.edu cell 973-932-3147 Office Hours Monday afternoon, also around Tues and Thurs Pooja Bepin Shah, TA Sunday 4pm office hour Course survey https:/ /www.surveymonkey.com/s/8X5QPGB Tuesday, October 21, 14
Why Java? Scala, Matlab, Mathematica, Python, CUDA, R Lisp, C, C++, Perl, Groovy, Ruby, Go, Objective C, Javascript, Dart... Tuesday, October 21, 14
Why Java? For the past few years, C, C++, and Java consistently seem to be the most popular languages. Many jobs in Industry Ubiquitous - Appears in huge servers, cell phones, set top boxes, blu ray players... Terrific and numerous APIs, commercial and open source Java’ s Design emphasizes simplicity. Raised the bar for language and API design, very influential Excellent for Learning Language Concepts Tuesday, October 21, 14
Which Java? Java Card - Runs on smart cards Java ME - Micro Edition Java EE - Enterprise Edition Java SE - Standard Edition Version 1.8 Java FX - GUI Package Groovy - Interpreted Java Tuesday, October 21, 14
Java Goals Make things as simple possible Robust(no segfaults) Portable Easy to dynamically run code Built in from day 1, not bolted on later Object Oriented Programming Concurrency support Exception handling First version in 1995, very different world then Tuesday, October 21, 14
Course Objectives Develop Fluency in the Java Language Learn about the Java Ecosystem Java APIs, 3rd party APIs Tools Learn about some of the design tradeoffs in Java How Java compares to other languages Tuesday, October 21, 14
Tentative Course Syllabus Intro, Data Types, Statements, Expressions, Operators, Week1 Control Structures, Memory Model, Classes, Eclipse Object Oriented Programming, Classes, Interfaces, Week2 Strings, Exceptions, Network&File Streams, java.lang.* Week3 Generics, Collections, java.util.* Week4 Functional Programming, Lambda Expressions Week5 Collection Streams Week6 Concurrency, Fork/Join, Synchronizers, Executors Advanced Topics: NIO, Reflection, Android, JavaFX, Week7 Spring Tuesday, October 21, 14
Grading 90% - Seven homeworks Lowest score will be dropped Homework due on Monday at noon Late submissions can not accepted, except with Doctor’ s note 10% - Class Participation No project or final Tuesday, October 21, 14
Academic Honesty Strict polices in the Computer Science Dept. Please look at the web page. For this class, you may talk with people in the class, have whiteboard discussions, but do not share source code. Do not show your source code to anybody, or leave it in a place where it might be seen by somebody else. You must write your own code. Cases of non original source code will be referred to the Dean of Students, and result in a zero for the Assignment. Tuesday, October 21, 14
Resources Java API reference - http:/ /docs.oracle.com/javase/8/ docs/api/index.html Free online “Java Trails” Oracle tutorial Many other tutorials online. Columbia library has some online books Do not recommend buying a book - they go obsolete very quickly Eclipse has good online help Tuesday, October 21, 14
Java Program A Java program consists of one or more classes defined by the developer. These classes can refer to other classes included in Java, and classes written by 3rd Parties Exactly one class must have a “main” method. This is where program execution will begin Also need a way to compile and run the program Many ways to do this, but often complex and tedious We will use the Eclipse IDE, which will handle the details for us Tuesday, October 21, 14
Class - Fundamental Building Block of Java Basically consists of: package declaration Class name zero or more field definitions, “class”(static) or “instance” zero of more method definitions, “class”(static) or “instance” Tuesday, October 21, 14
Classes A class defines a blueprint/design for objects Once a class is defined, any number of objects may be “instantiated” or “built” by using the “new” operator, much like a car factory can crank out an arbitrary number cars based on a design. Classes are usually distributed in “jar” files(very similar to “tar” files), but can also be loaded directly from a network Tuesday, October 21, 14
Make a Java Project in Eclipse File>New>Java Project In “Project Name” box, type “first” [no quotes] Click on “Finish” In the “Package Explorer” window, right click on “first”, then New>Class. For Name, enter “First Class” Click on checkbox for “public static void main... ”. This adds the method where execution will start Add some code Click Run>Run to run the program Tuesday, October 21, 14
Why Eclipse? Most popular IDE Automates many tedious tasks, including builds Syntax coloring Tooltip help Quick Fixes Automatic variable declarations Debugger Eclipse can be used for many other purposes - C/C++, Python, Scala, Android, ... Tuesday, October 21, 14
Java Under the Hood GCC vs JAVAC code generated startup time memory used Tuesday, October 21, 14
JVM Write Once Run Anywhere OS independent Simplifies distribution Provides higher level abstraction Highly optimized Can be updated w/o recompiling programs Some think it is the best piece of Java Technology! Used by other languages Tuesday, October 21, 14
Types A type is a set of things, like 32 bit integers, or characters There are two “types of type” in Java Primitive Reference Debatable - newer languages don’ t do this Why does Java? Tuesday, October 21, 14
Java Variables have a Type Some languages, like Python, have untyped variables x = 1 x = “string” In Java int x = 1 x = “string” - compiler error!! Tradeoff - typed variables are more verbose, but they enable more checking by the compiler. There is a large class of errors that can be made in Python that are impossible in Java Typed variables also make code more readable Tuesday, October 21, 14
Primitives vs References Primitives 8 of them, predefined by Java, not extendable Also knows as an “immediate” or “unboxed” References A reference “refers” to an Object An “array” defines a reference type A “class” defines a reference type New classes and arrays can be defined by the developer Also known as “boxed”, “pointer” Tuesday, October 21, 14
Manual Memory Management Developers find it very difficult to manage complex memory usage explicitly Leads to bugs that can be extremely difficult to find and fix C/C++ are the main languages that do NOT have some form of automatic memory management uses malloc/free for heap Tuesday, October 21, 14
Java Automatic Memory Management Model Things get stored in two places Call Stack frame - “short term” (life of the stack frame) storage for primitives, memory freed by popping the stack. Factorial example, recursion Heap - “long term”(completely independent of stack frame where it was created) storage of reference types, memory freed by garbage collector Tuesday, October 21, 14
Reference Counting Strict, but simple, protocol must be followed Uses extra memory Point(int x, int y) - 33% is overhead Can’ t break cycles used by python, mathematica... Tuesday, October 21, 14
Garbage Collection Pioneered by Lisp No protocol or extra memory per object Quite complex to implement Requires some compute cycles Difficult in large memory spaces Pauses? Tuesday, October 21, 14
Primitives and their Class “Wrappers” Primitive Class boolean Boolean char Character Wrapper classes have byte Byte useful methods for the short Short int Integer data type long Long float Float double Double Tuesday, October 21, 14
Autoboxing Convenient, but can cause problems int x = new Integer(200) Integer y = 34 Integer a[] = new Integer[20] a[3] = 23 Tuesday, October 21, 14
Class Methods Generalization of a function(sqrt vs random) A class(static) method can access local and class(static) variables A instance method can access local, class, and instance variables This lets an instance of a class bundle together state and actions A method is a series of statements Statements are built out of Expressions Expressions are built from literals, variables, and operators Tuesday, October 21, 14
Operators - Arithmetic +, -, *, /, %, ++, -- division of two integers drops the remainder % will yield remainder Oddly, there is no exponentiation operator If you want to cube an integer, write j*j*j For doubles, can use Math.pow + also does string concatenation “abc” + “123” => “abc123” Tuesday, October 21, 14
Operators - Comparison ==, !=, <, <=, >, >= ==, != for primitives, == is true if the values are the same for objects, == is true if the references are to the SAME object. for example “==” == “==” is false Tuesday, October 21, 14
Operators - Boolean AND &, OR | evals both args Conditional, AND &&, OR || will stop eval at first opportunity Negation, ! XOR - ^ Tuesday, October 21, 14
Operators - Bit Bitwise Complement - ~ Bitwise AND &, OR |, XOR ^ Left Shift << Right Shift, Signed >>, Unsigned >>> Tuesday, October 21, 14
Operator shortcuts Most operators support “var op= expression”, which is equivalent to “var = var op expression” Tuesday, October 21, 14
Recommend
More recommend