Intro to Java Week 6 Wednesday, December 3, 14 Final Homeworks - - PowerPoint PPT Presentation

intro to java week 6
SMART_READER_LITE
LIVE PREVIEW

Intro to Java Week 6 Wednesday, December 3, 14 Final Homeworks - - PowerPoint PPT Presentation

Intro to Java Week 6 Wednesday, December 3, 14 Final Homeworks Hwk 5 due Wed Noon Hwk 6 up soon Hwk 7 will be review questions. Does NOT depend on any Lecture 7 material. Will put it up before next Monday All previous homework solutions


slide-1
SLIDE 1

Intro to Java Week 6

Wednesday, December 3, 14

slide-2
SLIDE 2

Final Homeworks

Hwk 5 due Wed Noon Hwk 6 up soon Hwk 7 will be review questions. Does NOT depend on any Lecture 7 material. Will put it up before next Monday All previous homework solutions posted soon

Wednesday, December 3, 14

slide-3
SLIDE 3

Week 7

Topics Design Patterns J2EE/Spring Android Scala

Wednesday, December 3, 14

slide-4
SLIDE 4

Homework 5

What problems do the concurrent queues solve? Could accept orders from external sources Program doesn’ t exit - sits around waiting for more

  • rders

Doing a clean shutdown of a concurrent system can be tricky

Wednesday, December 3, 14

slide-5
SLIDE 5

Java Messaging Server

What if “machines” are on different hosts? Somewhat similar to an email server Delivery guaranteed Two Models Point to Point Publish/Subscribe M2M/IOT GE engines and turbines will “phone home” Street sensor data

Wednesday, December 3, 14

slide-6
SLIDE 6

Java Drools

Drools Rule based AIish system Forward Chaining Production rule systems(OP5) Recognize inputs and do something Backward Chaining Prolog Try to satisfy a goal Can be used for workflow

Wednesday, December 3, 14

slide-7
SLIDE 7

What do you do if you have 2,568 cores?

CPU cores - optimized for latency GPU cores - optimized for throughput Columbia has a GPU class, but it doesn’ t run every year GPU conference All previous talks online Next conference in March Lots happening on Amazon cloud example: video

Wednesday, December 3, 14

slide-8
SLIDE 8

Quantum Computing

Truly Radical! Bits vs Qbits. Qbits can take an infinite number of states Entanglement - Qbits can interfere, and lose individual identity Shor factoring algorithm Columbia has a class this spring Active research groups in academia, Microsoft, Google, IBM, HP, and, of course, the NSA DWave machine Don’ t have to be a physicist, just need to understand complex numbers and some linear algebra

Wednesday, December 3, 14

slide-9
SLIDE 9

Time and Dates

Original version in java.util.*(Date, Calendar), but had design problems and has been replaced by routines in java.util.time.* example:Dates

Wednesday, December 3, 14

slide-10
SLIDE 10

Enum, EnumSet

Can use an enum to make a singleton example: Days

Wednesday, December 3, 14

slide-11
SLIDE 11

Suppose You Have a Big C Program You Want To Use With Java

You could Rewrite it in Java. Look around, somebody may have done it for you already Try to “exec” it, but it if it just a “subroutine library”, that won’ t work Use JNI

Wednesday, December 3, 14

slide-12
SLIDE 12

Java Native Interface(JNI)

Generally not recommended Complex Difficult to debug Can corrupt Java memory

Wednesday, December 3, 14

slide-13
SLIDE 13

Is This a Valid Program?

package Reuse; class Reuse { static Reuse Reuse(Reuse Reuse){ Reuse: for (;;) { if (Reuse.Reuse(Reuse) == Reuse) break Reuse; } return Reuse; } }

Wednesday, December 3, 14

slide-14
SLIDE 14

Yep - There are 6 different namespaces in Java

package names class/type names field names method names local variable names labels

Wednesday, December 3, 14

slide-15
SLIDE 15

Stack Revisited

Took the int stack, and converted it to a String stack I was in a hurry for the big demo... example: stack

Wednesday, December 3, 14

slide-16
SLIDE 16

JProfiler Demo

Great tool, always surprising Can get a 10 day eval if you want to play java has a “debugging port” that lets tools do all kinds of things

Wednesday, December 3, 14

slide-17
SLIDE 17

java.lang.OutOfMemoryError

Thrown by JVM when java heap is exhausted Not much you can do about it Bigger heap? example: OutOfMem

Wednesday, December 3, 14

slide-18
SLIDE 18

java.util.ref.SoftReference

A reference to an object that can get garbage collected get() either returns the object, or null if it has been GCed example: SoftRef

Wednesday, December 3, 14

slide-19
SLIDE 19

Caches

A good cache is surprisingly difficult to write Recommend ehcache Supposed to be a “standard” Java cache, “JCache”, but seems to have stalled out

Wednesday, December 3, 14

slide-20
SLIDE 20

java - Command Line Arguments

program arguments use Apache commons-cli for complex args JVM arguments heap size -Xmx500m, -Xmx2g stack size -Xss

  • Dvar=value
  • cp, -classpath - root of classes

In Eclipse, can specify args in “Run Configurations” example: Args

Wednesday, December 3, 14

slide-21
SLIDE 21

Cloning Objects

clone() - Peculiar method Only makes a “shallow copy” If you need a “deep copy”, you have to write it example: Clone

Wednesday, December 3, 14

slide-22
SLIDE 22

Reflection

java.lang.Class<T>, java.lang.reflect.* Can learn about the variables, methods, and superclass of a class Get and set instance variables example: Reflect

Wednesday, December 3, 14

slide-23
SLIDE 23

Servlets

Standard for writing java code to generate web pages example: Servlet

Wednesday, December 3, 14

slide-24
SLIDE 24

Simple Java Webserver

example: WS

Wednesday, December 3, 14

slide-25
SLIDE 25

Java Web Servers

Lots of Java Web Servers out there, but two most important are Tomcat and Jetty Both are “industrial strength” Tomcat Reference Implementation for J2EE servlets Can run standalone or as part of J2EE or Spring Complex to configure Jetty Can plug into J2EE, but also has simple modes, easy to embed

Wednesday, December 3, 14

slide-26
SLIDE 26

Java Web Clients

Java API for http access is mediocre Use Apache httpclient package example: Wc

Wednesday, December 3, 14

slide-27
SLIDE 27

java.util.regex

Regular Expression package for java Just two classes, Pattern and Matcher Basically same pattern syntax as perl and python Eclipse has plugins for designing regular expressions, like RegEx Tester example: Wc

Wednesday, December 3, 14

slide-28
SLIDE 28

Beefing up Eclipse

Tips and Tricks Help Plugins Maven Regex JUnit Find Bugs Help/Eclipse Marketplace Search for the plugin you want Eclipse will restart

Wednesday, December 3, 14

slide-29
SLIDE 29

Diamond operator

Skip the type spec on the right side List<Integer> foo = new List <>(); Doesn’ t do much for me as I always enter the type on the right side and let Eclipse write the type on the left hand side

Wednesday, December 3, 14

slide-30
SLIDE 30

java.util.Base64

Represents arbitrary bytes with two printing characters(4 bits each) Used to send binary files in email (mime) Package provides Base 64 encoding and decoding

Wednesday, December 3, 14

slide-31
SLIDE 31

Bit Hacking

Can tweak bits in an int or long with bitwise

  • perators(&, |, ^, !)

Bits are great for representing set elements union = |, intersection=&, very fast easy to compute power sets by counting from 0 to N-1 and checking which bits are 1 java.util.BitSet Growable vector of boolean bits Good features Use it if you need more than 64 bits

Wednesday, December 3, 14

slide-32
SLIDE 32

javax.xml.*

Complex package for XML processing Several modes example: Bookstore

Wednesday, December 3, 14

slide-33
SLIDE 33

Processing JSON in Java

JSON is a lightweight replacement for XML Very popular No built in support for JSON in java Maybe in JDK 1.9 Recommend using Jackson Package http:/ /wiki.fasterxml.com/JacksonHome

Wednesday, December 3, 14

slide-34
SLIDE 34

Object.finalize()

Don’ t use it!! Supposed to run when object is garbage collected But, no guarantee it will ever be called, or called “soon” Slows object recycling

Wednesday, December 3, 14

slide-35
SLIDE 35

SQL Interfaces

Use JDBC interface Drivers exist for popular databases, like Oracle, MySql, Postgres Easy to use, somewhat low level Object-relational tools, like Hibernate, are nicer example: Sql

Wednesday, December 3, 14

slide-36
SLIDE 36

java.lang.classloader

Class is responsible for loading byte(compiled) code into the JVM, either from a file or the network Classloaders can be arranged in a hierarchy for advanced applications

Wednesday, December 3, 14

slide-37
SLIDE 37

Math Packages

java.lang.math - math functions like sin, cos, etc java.math.BigInteger - arbitrary precision integer arithmetic java.math.BigDecimal - important for financial work. doubles and floats are not accurate enough

Wednesday, December 3, 14

slide-38
SLIDE 38

Apache Utility Packages

commons-lang - for core classes commons io - for IO classes http components - for client side HTTP commons email - for client side email generation commons cli - for processing command line arguments Mina - for NIO networking log4j - for logging maven - system building, dependency management

Wednesday, December 3, 14

slide-39
SLIDE 39

GUI’ s in Java

java.applet.* - Popular some time ago, but not used much anymore java.aws.* - First Java GUI package. Absolutely not recommended for new code javax.swing.* - Good GUI package. Still works, but Oracle is dropping support for it javaFX - Latest package from Oracle. Has some animation capabilities. Not clear how much use it will get, given the move to HTML5 and mobile platforms

Wednesday, December 3, 14

slide-40
SLIDE 40

Deprecated Things...

Use ArrayList instead of Vector Use HashMap instead of Hashtable

Wednesday, December 3, 14

slide-41
SLIDE 41

Do Use Do NOT Use Reason java.lang. StringBuilder java.lang. StringBuffer StringBuffer is synchronized(runs slower) java.lang. ProcessBuilder java.lang. Process ProcessBuilder is improved version of Process java.lang.util.Concurrent. Executors java.lang. Thread Executors are more sophisticated, easier to use java.time.* java.util.Calendar, java.util.Date java.util.* functions are deprecated

Wednesday, December 3, 14