A Caffeinated Primer on Java - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

A Caffeinated Primer on Java

Description:

Jokes about caffeine, coffee, etc get really old really fast ... Latest and greatest J2SE (Java 2 Standard Edition) SDK from http://java.sun.com (free) (38MB) ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 22
Provided by: gar67
Category:

less

Transcript and Presenter's Notes

Title: A Caffeinated Primer on Java


1
A Caffeinated Primer on Java
  • By Garabed (Garo) Yeriazarian

2
Object - Orientation
  • Purely object-oriented
  • Everything is an object (well, sort of)
  • Single Inheritance only
  • Multi Interface Inheritance
  • Always Polymorphic (C virtual keyword)
  • Standardized Exceptions
  • Easy.asPie()

3
Whats so special about Java (the language, not
the beverage)?
  • C style syntax and constructs (but simpler)
  • Write Once Run Anywhere (Sun gets a nickel every
    time someone says this)
  • Virtual Machine provides security and garbage
    collection among other things
  • Full-featured API (less work for you!!!)
  • Built-in documentation mechanism (javadoc)
  • Packages (C namespaces) for organization

4
However Java pitfalls (come on, you know there
had to be some)
  • Java byte-code is interpreted by the VM (can be
    slow depending on the system)
  • Multi-platform is nice, but deployment is not
    always easy (without buying tools)
  • Jokes about caffeine, coffee, etc get really old
    really fast
  • Requires Virtual Machine (10MB download) on the
    client machine for deployment

5
Tools You Want / Need to Use Java
  • Borland JBuilder 8 Personal (free)
  • You need to register and get a free key too
  • Contains the J2SE SDK 1.4.1
  • Dont forget to download the docs separately
    (56MB)
  • IDE for easy Java development
  • Windows (50MB), Solaris (60MB), and Linux (70MB)
  • Latest and greatest J2SE (Java 2 Standard
    Edition) SDK from http//java.sun.com (free)
    (38MB)
  • Get the documentation too (necessity!!!) (32MB)
  • These are command-line tools only, no IDE
  • Makes you love high speed internet access, eh?
    ,-)

6
Howdy World!
/////////////////////////////////////////////// //
HowdyWorld.java Obligatory Intro
program //////////////////////////////////////////
///// public class HowdyWorld public static
void main(String args)
System.out.println(HOWDY WORLD!!!)
7
Compiling and Executing
  • Create the HowdyWorld.java file (case matters)
  • Make sure your path is set to include the Java
    bin folder where java.exe and javac.exe reside
  • javac HowdyWorld.java (case matters)
  • This will generate the HowdyWorld.class file
  • java HowdyWorld (case matters)
  • Itll grind a bit, then you get HOWDY WORLD!!!

8
The Basics of Java Console Apps
  • A Java program can be made up of one or more
    .java files. Each .java file contains one class
    (not required, but highly recommended)
  • To be able to execute a Java application, the
    entry point class must contain the main method
    public static void main(String args)
  • Static means that this method is executed
    independent of any instances of the class (same
    meaning as in C)

9
Lets See Something A Little More Complicated
(Im getting bored)
  • Fine, check out the Wordifier.java file from the
    samples on the presentation page
  • This program will read in a line of text, parse
    it into words, and spit out the results along
    with a word count and average word length
  • The program is heavily commented, so you
    shouldnt have too much trouble following it
  • This program will illustrate reading in from the
    console, writing to the console, use of several
    primitive types, use of strings, and use of
    classes
  • ltDemo of Wordifiergt

10
((int)(11)) Types of Variables
  • Primitive Types are like standard C types, they
    are automatically allocated and freed by the
    Virtual Machine
  • Objects are instances of classes, they are NOT
    automatically allocated by the Virtual Machine,
    you must explicitly instantiate them using the
    new operator

11
Java Primitive (oogah) Types
12
Classes and Objects
  • Data types are called Classes, instances of a
    class are called Objects
  • In Java, every object is a reference
  • In C, you could have objects that were
    allocated / deallocated on the stack
    automatically by the compiler.
  • So what? Well, you have to be careful with the
    operator, the next slide will explain why

13
Snafu about Objects
  • Say you have the following code snippetPoint a,
    ba new Point(1,1)b aSystem.out.println(
    Point A a)System.out.println(Point B
    b)a.x 3System.out.println(Point A
    a)System.out.println(Point B b)
  • Output (not quite what you expect)Point A
    (1,1)Point B (1,1)Point A (3,1)Point B
    (3,1)

14
Stringin Yall Along
  • Strings get preferential treatment in Java
  • For examples, see all the sample programs
  • A String is immutable
  • To modify a string, you must make a new one or
    look into the StringBuffer class
  • Every class has a toString() method to convert an
    object into its string representation
  • String concatenation with operator
    (automatically calls the toString() method of
    non-string objects
  • Remember, a string is still an object
    reference!!!
  • To copy a string strNew new String(strOld)

15
Everybody Loves Arraymond
  • Cmon, you know you want to know all about
    arrays!
  • Java treats arrays as objects
  • Array dimensions are defined ONLY at runtime (not
    compile time like C)
  • int intArray new int10int int2DArray
    new int1020
  • The dimensions dont have to be constants
  • intArray.length 10int2DArray.length
    10int2DArray0.length 20
  • All arrays are, like C, 0-based

16
Important Thing about Classes!!!
  • One interesting thing about Java is that all
    classes automatically inherit from
    java.lang.Object
  • Uh, so what?
  • Well, glad you asked ,-) in Java, all methods
    are (in C terms) virtual (you could also say
    polymorphic)
  • So, you could set up an array of Objects, then
    assign any object to any element of that array
  • Java also maintains runtime type information
    (RTTI in C)
  • Java also provides an operator instanceof that
    you can use to test the type-compatibility of an
    objectif (MyObject instanceof String) // do
    stuff

17
Sheeyooot, dats purdy cool!
  • I guess you wanna test it out, eh? Heh, I knew
    you would, so I made a nice little test program
    called the UberArray (UberArray.java and
    TestUberArray.java)
  • This example serves multiple purposes
  • To show you how to use multiple classes in your
    programs
  • To show you how to do Arrays
  • To show an example of Java polymorphism
  • Say you wanted to make (oh, I dont know) a queue
    (hint hint, nudge nudge), then you could design
    it to be able to hold any arbitrary type (object
    type, not primitive type unless you use the class
    wrappers)

18
Using Multiple .class Files Together
  • Java makes it super-easy to reuse existing code.
  • You dont need access to the source files, just
    the compiled .class file
  • Drop it into the same folder as the main program
    you are writing, then use the class as you would
    any other class
  • No need for specific import statements (see
    UberArrray example for an example)
  • Example You have a class called MyDataClass
  • Compile to get MyDataClass.class
  • In DoData.java, say you want to use MyDataClass,
    you just declare a variable of type MyDataClass
    and the VM will grab the right .class file, load
    it and use it

19
Try Not to Lose Interface
  • Java classes are single-inheritance only!!!
  • However, you can use interfaces (C pure
    abstract base classes) to use a cleaner type of
    multiple inheritance and allow objects to be used
    in a more general manner
  • Example (Informable.java)public interface
    Informable public String getInformation()
  • This can be used to develop a standard plug-in
    system for your application or to further
    separate interface from implementation

20
Thats It for the Primer
  • So, by now, you should have a basic understanding
    of Java (hopefully, unless I did a bad job) ,-)
  • Hopefully, yall will try out all the samples I
    put together on your own to get a feel for how
    Java works
  • Most likely all this talk about Java fired up a
    craving for some Frappuchinos from Starbucks

21
Ok, So Where Do I Go From Here?
  • Check out the Sun Java Tutorialshttp//java.sun.
    com/docs/books/tutorial/
  • Buy some books on Java (check the links on the
    presentation web page)
  • Stuff I didnt cover (out of scope for this
    primer)
  • GUIs, Events, Threads, Packages, File I/O,
    Networking
  • Cmon, yall are smart people, read up on this
    stuff in books
  • Or, you can email me on WebCT (Garabed
    Yeriazarian) or real email GaroY_at_aol.com
  • Although the best thing to do would be to post
    your questions / comments in the Java Forum for
    everyone to see
Write a Comment
User Comments (0)
About PowerShow.com