COP3252 Advanced Java Programming - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

COP3252 Advanced Java Programming

Description:

History of Java ... Sun sees potential for using Java to add dynamic content (interactivity, animations ... Bytecode runs on an interpreter (Java Virtual Machine) ... – PowerPoint PPT presentation

Number of Views:202
Avg rating:3.0/5.0
Slides: 31
Provided by: robertthor1
Category:

less

Transcript and Presenter's Notes

Title: COP3252 Advanced Java Programming


1
COP3252Advanced Java Programming
  • 10-Jan-08
  • Lecture Set 2
  • Intro Java History, Java vs C, compiling and
    running, Printing Text, Assignment 1

2
History of Java
  • 1991 Sun Microsystems funds internal corporate
    research project (Codename Green)
  • Goal is to gain a stake in the growing
    intelligent consumer-electronic device sector
  • Results in C based language
  • Creator, James Gosling, named language Oak
  • Sun realizes that Oak is already taken
    renames langauge Java while at a coffee shop

3
History of Java
  • Early 90s Green project has a slow start
  • Market for intelligent consumer-electronic
    devices was slow to develop
  • Project is in danger of being canceled
  • Mid 90s WWW explodes in popularity
  • Sun sees potential for using Java to add dynamic
    content (interactivity, animations ) to web
    pages.
  • Green project comes alive again
  • May 1995 Sun Microsystems officially announces
    Java at an industry conference.
  • Today Java is used to provide applications on
    many consumer devices (cell phones, gaming
    systems, pdas, etc)

4
Java vs. C
  • Compiling and Running
  • C
  • Must be fully compiled into machine language
  • Resulting executable is machine-specific
  • Portable only when using non-machine specific
    libraries (but still requires compilation on each
    platform)
  • Generally faster run-time for executable
  • Java
  • Compiled into bytecode (not into machine
    language)
  • Bytecode runs on an interpreter (Java Virtual
    Machine)
  • Bytecode is portable but JVM must be installed
    on platform
  • Reduced runtime speed since it had to run through
    an interpreter

5
Benefits of Java
  • Software development kit (SDK) includes vast
    collection of packages (classes)
  • Easy-to-use API (on Suns website)
  • Standard format for building API docs (Java Docs)
  • Very easy to build GUIs
  • Runs on multiple platforms
  • No pointer notation
  • Objects and arrays are created dynamically
  • Automatic garbage collection / memory management

6
Benefits of C
  • C gives the programmer more control (resource
    allocation/deallocation, control over addresses
    with pointers)
  • Programs run faster (compiled with platforms
    native instructor set, no interpreter)

7
Java Class Libraries
  • Java programs consist of pieces call classes
  • Classes include pieces call methods
  • Methods perform tasks and return information
  • Many existing classes exist in the Java
    Application Programming Interface (API)
  • Learning Java is a combination of learning the
    language (to write your own classes) and learning
    about the classes already available in the Java
    class library.

8
The Java Hello World File
  • public class example
  • public static void main( String args )
  • System.out.println(Hello, World.)

9
Hello World explanation
  • public class example
  • Begins the class declaration for class example
  • Every program in Java consists of at least one
    class declaration (defined by the programmer)
  • class is a reserved word in Java it begins
    the class declaration and expects a class name to
    immediately follow
  • Opening and closing brackets are used to
    denote the start and end of the class

10
Class Naming
  • By convention, all class names in Java begin with
    a capital latter and capitalize the first letter
    of each word they include
  • Example MyClassName
  • Class names are indentifiers
  • String of characters consisting of letters,
    digits, underscores, and dollar signs
  • Do no being with a digit and can not contain
    spaces
  • Note JAVA IS CASE SENSITIVE
  • Test is not the same as test

11
Class Naming public and filenames
  • Example begins with word public
  • This keyword will be used (for now) when defining
    all of our classes . But will not be explained
    until later
  • When declaring a class public, we are required
    to name the source code file with the same name
    as the class.

12
Hello World explanation
  • public static void main (String args)
  • main method declaration
  • Starting point for every Java application
  • JVM will not execute the application if the above
    line is not present
  • All method bodies open with a and close with a

13
Hello World explanation
  • System.out.println(Hello, world.)
  • One of Javas command line print statements
  • Instructs Java to print a String to the command
    line.
  • Calls the println function standard output
    object.
  • Moves cursor to next line in the command window.
  • Strings in Java are denoted by a set of opening
    and closing double quotes
  • All statements end with a semicolon.

14
Creating your first .java file
  • Open an editor on one of the CS machines and
    input the source code
  • Save the file as example.java
  • Exit your text editor
  • Now you will compile (make the code executable)
    from the command line

15
Compiling a JAVA Program
  • JAVA compiler command javac
  • should be a valid .java file
  • If there are no errors, the compiler will create
    your object code (.class file)
  • If there are errors, the compiler will return a
    list of line numbers and the corresponding error
    messages.

16
Compiling an error free JAVA program
  • Create a file called example.java using the code
    from an earlier slide
  • Compile the program with the command javac
    example.java
  • Use the ls command to verify that the compiler
    has created an example.class file

17
Running a Compiled JAVA Program
  • The command to execute a compile program is
    java
  • is the name of the class of the
    program you wish to run OR the name of the
    .class file without the .class extension
  • Example To run example.class, you would type
    java example

18
Submitting a Program
  • Use the Digital Dropbox on Blackboard
  • Student tools - Digital Dropbox
  • Click the send file button
  • Fill in the name
  • Click the choose file button to find your file
  • Click the submit button

19
The Java JRE and SDK
  • Both are available from http//java.sun.com
  • JRE (Java Runtime Environment) Allows you to
    run already compiled programs (.class files) on a
    system
  • SDK (or JDK Java Development Kit) Allows you
    to compile and run Java programs (includes the
    JRE)
  • The JRE and JDK is available for all popular
    operating systems.
  • The JDK must be installed on your personal
    machine if you wish to use an IDE such as
    NetBeans, JCreator, JGrasp, or Eclipse.

20
JAVA Comments
  • Comments are used to document the code and import
    the readability.
  • There are two types of comments in JAVA
  • Single-line comments line begins with //
  • Example
  • //This is a single-line comment
  • Multi-line (or block) comments begins with /
    and ends with /
  • Example
  • / This is a
  • multi-line comment/

21
JAVA Comments Example
  • /Name R. Thornton
  • Date 16-Jan-07
  • Program Comment Example/
  • //Begin example class
  • public class example
  • //Begin main method
  • public static void main (String args)
  • System.out.println(Hello) //Print a string
  • //end main method
  • end example class

22
JAVA Comments
  • You MUST comment all of your code in this course.
  • Proper commenting will account for 10 to 20 of
    each program assignment grade.
  • Commenting guidelines
  • It is recommended that you include enough
    comments in your code so that someone completely
    unfamiliar with programming can read through your
    code and figure out what it is doing.
  • It is better to have too many comments than to
    not have enough comments!

23
Programming Style
  • A portion of every assignment grade will be based
    on your programming style.
  • Programs should be easy to read and properly
    indented
  • Examples of proper indentation will be show in
    all example code given in this class.

24
JAVA Printing text to the screen
  • We use the standard output object to print text
    in to the screen.
  • There are two standard output object methods
  • System.out.print(a string)
  • Prints the string and leaves the cursor on the
    same line
  • System.out.println(a string)
  • Prints the string and moved the cursor to the
    next line

25
JAVA Printing text to the screen Cont.
  • These two methods take a string as input.
  • The input of a method is denoted by an opening
    and closing parenthesis ( and ).
  • A string is denoted by an opening and closing set
    of double-quotes and
  • Anything between the quotes is printed as-is
  • except for escape sequences (discussed later)
  • The end of a line of code is denoted by a
    semi-colon

26
JAVA Printing text to the screen Cont.
  • The following code
  • System.out.print(Hello.)
  • System.out.println( Welcome to)
  • System.out.println(JAVA)
  • Prints this to the screen
  • Hello. Welcome to
  • JAVA
  • _

27
JAVA Printing text to the screen Escape
Sequences
  • JAVA has a list of escape sequences that
    represent special formatting options
  • The backslash (\) is an escape character and
    denotes the start of an escape sequence.
  • The character immediately following the backslash
    determines which escape sequence will be used.

28
JAVA Printing text to the screen Escape
Sequences
29
JAVA Printing text to the screen Example
  • print, println, and escape sequence examples can
    be found in the PrintExamples.java file on the
    course website

30
Assignment 1 / Next Class
  • See assignments page.
  • Due Tuesday, 15th of Jan, 2008.
  • Assignment 2 will be assigned on Tuesday.
  • Also on Tuesday
  • Variables, printf, control structures
Write a Comment
User Comments (0)
About PowerShow.com