1 / 42

The Java Language: Intro for Software Developers

The Java Language: Intro for Software Developers. Phil Miller. (very briefly…) Who is this guy?. Consulting Software Engineer for IBM Global Services since 1999 Worked on parallel processing R&D Projects at Intel 1988-93 BSEE from Purdue, MSCSE from OGI. How do I know about Java?.

karl
Download Presentation

The Java Language: Intro for Software Developers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Java Language: Intro for Software Developers Phil Miller

  2. (very briefly…) Who is this guy? • Consulting Software Engineer for IBM Global Services since 1999 • Worked on parallel processing R&D Projects at Intel 1988-93 • BSEE from Purdue, MSCSE from OGI

  3. How do I know about Java? • What I do: Java-based software that collects information about IBM-branded servers and sends it to an IBM web site for use by customer service

  4. Why is Java important? • Very wide acceptance and use on the web • Virtually all software companies use Java for web development • “Everybody but Microsoft”(.Net …) • Platform independence • Versatility • Simplicity, clarity

  5. Platform Independence • Java uses a Java Runtime Environment that works the same way on every platform.

  6. Language Standards • Managed by license agreement with Sun Microsystems • Industry standards

  7. The most important concepts in Java: simplicity and clarity Simplicity is the ultimate sophistication.-- Leonardo da Vinci Of two equivalent theories or explanations, all other things being equal, the simpler one is to be preferred.-- William of Ockham (Occam’s Razor)

  8. Versatility • Traditional Unix command-line “filter” applications • Graphical User Interfaces • “lightweight applications” for a wide range of frameworks (e.g., “servlets”, “applets”) • “embedded” applications; (cell phones, for example)

  9. Why you’d like Java • It’s harder to write bad programs in Java • It’s hard to write non-OO programs in Java • Most obvious problems are solved by the compiler and the run-time environment • You’d be more productive in Java

  10. How programs are evolving

  11. Typical Unix file-centric, “batch” programming model • cc is the “driver” program • cpp preprocessor • ccom is parser, intermediate code generator • opt is the optimizer • cgen generates assembly code • as translates to object code • ld links together object modules to produce “a.out” file cc cpp ccom opt cgen as ld

  12. Typical user interface program • Program responds to user input: “event-driven” • Programs often developed in a framework

  13. Lightweight Applications • Very small, specialized pieces of software that perform a limited operation, then terminate (freeing resources, etc.) • example: a piece of code that understands how to buy a product using a credit card • Think “nanotech”; Java components

  14. What Java looks like (in comparison with C…)

  15. Hello, World in C int main(int argc, char **argv) {char *h = “Hello CS”, *j=“ Winter 2005”; int i = 321;printf( “%s%d%s!”, h, i, j ); } • Note that printf is a special type of function called a varargs function:int printf( char *, … );

  16. public class hello { public static void main(String[] args) {String h=“Hello CS”, j=“ Winter 2005”; int i=321; System.out.println( h + i + j ); } } Compiler auto-matically converts i to a String Example: Hello, world

  17. Demo: Java compiler and Java runtime

  18. Java has namespaces • Allow hierarchical folders for source code • Source files use package to specify directory: package edu.pdx public class yourclass • Java program uses “fully qualified class name”: java edu.pdx.yourclass • Directories reflect namespace: edu\pdx\yourclass.class

  19. Demo: Java namespaces

  20. Primitive types in C: integers • Integers: char, signed char, unsigned char, short, short int, signed short, unsigned short, signed short int, unsigned short int, int, signed int, unsigned int, signed long, unsigned long, long int, signed long, unsigned long, signed long int, unsigned long int (29 types) • long >= int >= short

  21. More on integers in C • Typically (but not universally): char is 8 bits, short is 16; int may be 16 or 32, long is 32 bits; the details are specified in limits.h. • char is usually 8 bits, but sometimes 7 or 9 • Compiler implementers can make char signed or unsigned as they choose; see example!

  22. how unsigned chars cause portability problems… /* runs ∞ if char is unsigned… */ int main() {char ch;while ((ch = getchar()) != EOF) putchar(ch); }

  23. Primitive data types in Java • boolean, byte, char, short, int, long, float, and double (8 types) • no notion of “unsigned” • Everything else is an object instance…

  24. Objects (classes) • Java has a class hierarchy with the class Object as the superclass of every other class • Java does not have multiple inheritance; it has single inheritance (i.e., inherited behavior) and interfaces (i.e., specified, required behavior)

  25. Some common classes • String is a very commonly used object • There are “wrapper” classes for primitive types to make them work like objects:Boolean, Byte, Character, Short, Integer, Long, Float, and Double are wrappers for boolean, byte, char, short, int, long, float, and double.

  26. What EVERY Object can do • clone - Creates and returns a copy of this object.  • equals – Compares two objects for equality • finalize - Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. • getClass - Returns the runtime class of an object. • hashCode – Used in hashing algorithms to categorize & sort objects. • notify - Wakes up a single thread that is waiting on this object. • notifyAll - Wakes up all threads that are waiting on this object • wait - Causes current thread to wait until another thread invokes notify or notifyAll. • toString – Provides a string representation of an object

  27. The toString method • All Java objects support a method toString • It returns a String representation of the object • This is most noticeable (and useful) when doing I/O:Object foo = new Object();System.out.println(“foo = “ + foo); • The “toString” is implied in a “string context”

  28. Java Reflection • Array – Dynamically create and access arrays • Constructor – Dynamically construct objects • Field – Provides information about (and access to) a single field (variable) of a class or an interface. • Method – Provides information about a method in a class or interface; allows execution of a method. • Modifier – Provides a way to decode and modify the access of members of an object.

  29. Java Beans (Java Reflection at work) • Java beans are heavily used in web applications • Java beans use conventions for variable definition to provide access to methods and fields.Example: A field called fooBar can be accessed by accessor method getFooBar(); a program using the bean uses reflection (aka introspection) to gain access to fooBar

  30. Simplicity and Clarity revisited C/C++ sources are typically divided into 3 parts: • implementation: the actual C/C++ code,typically in a file with suffix “.cpp” • declarations: the description of the C/C++ code, typically with suffix “.h” • documentation: an explanation of how the code can be used, typically “.man”

  31. Javadoc – developer documentation • Everything you need to know about a class is contained in one file with suffix “.java”. • Specially formatted comments describe the code: /** Slash-star-star starts a javadoc comment and star-slash ends it. */

  32. Javadoc demo

  33. Inheritance and polymorphism in Java class Pet { … } class Dog extends Pet { … } class Cat extends Pet { … } Pet p = new Cat();… p = new Dog(); if ( p instanceof Dog ) ….

  34. Built-in support for dynamic arrays, associative maps, B-trees, sets, …. All collection classes are polymorphic ArrayList al = new ArrayList(); al.add( "hello " ); al.add( "CS321 " ); al.add( "Winter 2005 " ); System.out.println( "al now has " + al.size() + " elements." ); // get things out of a collection StringBuffer sb = new StringBuffer(); for (Iterator iter=al.iterator(); iter.hasNext(); ) { String x = (String) iter.next(); sb.append( x ); } System.out.println( sb.toString() ); Collection classes in Java

  35. Demo: Eclipse Free java development environment available from www.eclipse.org

  36. Garbage Collection in Java • Unlike other languages, you don’t “free” objects in Java. • When an object goes “out of scope”, it’s available for GC:if ( expr == true) { String abc = “abc”; }// abc is “out of scope” HERE. • Also, when all references to an object are removed, it becomes available for GC

  37. An example of garbage collection using Strings String myName = “Phil”; // line 1 // other code… myName = “Miller”; // line 2 • The string “Phil” is an immutable object; once it’s created, it’s not possible to change it. • “Phil” becomes an object with no references, and the garbage collector eventually reclaims it.

  38. What the compiler sees: String abc = “This “ + “is “ + “a “ + “long “ + “string”; This is actually wasteful; five strings are created, then immediately handed over to the garbage collector. What the compiler does:StringBuffer $1 = new StringBuffer();$1.append(“This “); $1.append(“is “);$1.append(“a “); $1.append(“long “);$1.append(“string”);String abc = $1.toString(); An interesting optimization using StringBuffer

  39. Design Patterns • A design methodology that promotes standard solutions to everyday problems. • Provides terminology for discussing designs. This book can be downloaded in PDF form for free:http://www.patterndepot.com/put/8/JavaPatterns.htm

  40. Another example: Hello, world JSP application (“servlet”) • import java.io.*; • import javax.servlet.*; • import javax.servlet.http.*; • public class MyHello extends HttpServlet implements Servlet { • public MyHello() { • super(); • } • protected void doGet(HttpServletRequest req, • HttpServletResponse res) throws ServletException, IOException { • res.setContentType( "text/html" ); • PrintWriter out = res.getWriter(); • out.println( "<HTML>" ); • out.println( "<HEAD><TITLE>Hello, World</TITLE></HEAD>" ); • out.println( "<BODY>" ); • out.println( "<BIG>Hello, World</BIG>" ); • out.println( "</BODY></HTML>" ); • } • protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { • }

  41. Summary • Learn Java to enjoy the good life!

  42. Resources • Java development environment:http://java.sun.com • Integrated development environment:http://eclipse.org • Open-source web server, application server, etc.:http://apache.org • Design Patterns:http://www.dofactory.com/Patterns/Patterns.aspxhttp://www.industriallogic.com/papers/learning.htmlhttp://www.patterndepot.com/put/8/JavaPatterns.htm

More Related