Introduction To Java Larry Stead, Instructor lss2168@columbia.edu - - PowerPoint PPT Presentation

introduction to java
SMART_READER_LITE
LIVE PREVIEW

Introduction To Java Larry Stead, Instructor lss2168@columbia.edu - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

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

slide-2
SLIDE 2

Why Java?

Scala, Matlab, Mathematica, Python, CUDA, R Lisp, C, C++, Perl, Groovy, Ruby, Go, Objective C, Javascript, Dart...

Tuesday, October 21, 14

slide-3
SLIDE 3

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

slide-4
SLIDE 4

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

slide-5
SLIDE 5

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

slide-6
SLIDE 6

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

slide-7
SLIDE 7

Tentative Course Syllabus

Week1 Intro, Data Types, Statements, Expressions, Operators, Control Structures, Memory Model, Classes, Eclipse Week2 Object Oriented Programming, Classes, Interfaces, 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 Week7 Advanced Topics: NIO, Reflection, Android, JavaFX, Spring

Tuesday, October 21, 14

slide-8
SLIDE 8

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

slide-9
SLIDE 9

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

slide-10
SLIDE 10

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

slide-11
SLIDE 11

Java Program

A Java program consists of one or more classes defined by the developer. These classes can refer to

  • ther 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

slide-12
SLIDE 12

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

slide-13
SLIDE 13

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

slide-14
SLIDE 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

slide-15
SLIDE 15

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

slide-16
SLIDE 16

Java Under the Hood

GCC vs JAVAC code generated startup time memory used

Tuesday, October 21, 14

slide-17
SLIDE 17

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

slide-18
SLIDE 18

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

slide-19
SLIDE 19

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

slide-20
SLIDE 20

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

slide-21
SLIDE 21

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

slide-22
SLIDE 22

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

slide-23
SLIDE 23

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

slide-24
SLIDE 24

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

slide-25
SLIDE 25

Primitives and their Class “Wrappers”

Wrapper classes have useful methods for the data type Primitive Class boolean Boolean char Character byte Byte short Short int Integer long Long float Float double Double

Tuesday, October 21, 14

slide-26
SLIDE 26

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

slide-27
SLIDE 27

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

  • perators

Tuesday, October 21, 14

slide-28
SLIDE 28

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

slide-29
SLIDE 29

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

slide-30
SLIDE 30

Operators - Boolean

AND &, OR | evals both args Conditional, AND &&, OR || will stop eval at first opportunity Negation, ! XOR - ^

Tuesday, October 21, 14

slide-31
SLIDE 31

Operators - Bit

Bitwise Complement - ~ Bitwise AND &, OR |, XOR ^ Left Shift << Right Shift, Signed >>, Unsigned >>>

Tuesday, October 21, 14

slide-32
SLIDE 32

Operator shortcuts

Most operators support “var op= expression”, which is equivalent to “var = var op expression”

Tuesday, October 21, 14

slide-33
SLIDE 33

Operators

Assignment - x = y Conditional Operator(very handy) int max = (x>y) ? x : y instanceof - checks the type of a reference object “string” instanceof String is true . - access members of a object (args...) - invoke a method new - create an object (type) - cast

Tuesday, October 21, 14

slide-34
SLIDE 34

Statements

Compound or Block - {s1; s2; s3;...} Empty - ; Statements can be labeled for break

Tuesday, October 21, 14

slide-35
SLIDE 35

Statement - if

Slightly different from C version - the predicate must be of type boolean else clause is optional

if (predicate) then truestatement; else falsestatement;

Tuesday, October 21, 14

slide-36
SLIDE 36

Statement - switch

switch(n) { case 1: / / code break; case 2: / / code break; }

Tuesday, October 21, 14

slide-37
SLIDE 37

Statement - while

while(predicate) { / / loop body }

Tuesday, October 21, 14

slide-38
SLIDE 38

Statement - do

do { / / loop body } while (predicate)

Tuesday, October 21, 14

slide-39
SLIDE 39

Statement - for

/ / for “classic” for(int j = 0; j<5; j++) / / body / / for “enhanced int a[]; for(int j : a) / /body for can be nested - Square

Tuesday, October 21, 14

slide-40
SLIDE 40

Statement - break

int a[]; for(int j : a) if (j == 5) break;

Tuesday, October 21, 14

slide-41
SLIDE 41

Statement - continue

int a[]; for(int j : a) { if (j == 5) continue; / / more stuff }

Tuesday, October 21, 14

slide-42
SLIDE 42

Statement - return

executing return will immediately exit a method(instead of falling off the bottom) if the method returns a value, the value is an argument to return executing return in the “main” method causes the program to terminate can also terminate the program anywhere by calling java.lang.System.exit

Tuesday, October 21, 14

slide-43
SLIDE 43

Statements - wrapup

synchronized try/catch/finally throw

Tuesday, October 21, 14

slide-44
SLIDE 44

Arrays

Some differences from C arrays int a[10] won’ t work, must use “new” to get storage int a[] = new int[10] 2d arrays int a[][] = new int[5][6] is not a linear array in memory - it is an “array of arrays” a 2d array can be “rectangular” or “ragged” “int [] a”, and “int a[]” are both valid declarations

Tuesday, October 21, 14

slide-45
SLIDE 45

Homework

Monte Carlo simulation Dice Game Hadamard Matrix Taxi Ascii Art

Tuesday, October 21, 14