java: week 1

Upload: happyruss

Post on 14-Apr-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 Java: Week 1

    1/30

    Introduction to Java

  • 7/30/2019 Java: Week 1

    2/30

    What is Java

    Developed in the early 1990s by Sun

    Meant to be highly portable so it could beembedded into microwave ovens, cell

    phones, remote controls, etc

    Java was written with the C language

    Has similar syntax to C, C++, and JavaScript

    (JavaScript is totally different than Java)

  • 7/30/2019 Java: Week 1

    3/30

    Java is Platform Independent

    Java can be run on any platform or any

    processor that has a JVM built for it. Java is an interpreted language, meaning

    that there is a machine/processor-dependent

    interpreter called the JVM that translates the

    standard Java code into instructions for thespecific processor

  • 7/30/2019 Java: Week 1

    4/30

    Java Architecture

  • 7/30/2019 Java: Week 1

    5/30

    Platform Independence

    In C++, only the source code is platform

    independent; the compiler and executableare platform-dependent

    Microsoft.NET products are Platform

    Dependent but Language Independent to a

    degree

  • 7/30/2019 Java: Week 1

    6/30

    Garbage collection and the JVM

    In addition to converting your common

    bytecode to machine-dependent instructions,the JVM provides memory cleanup known as

    garbage collection

    The JVM will clean up after a program once

    components are no longer needed (i.e. youdont need stuff like set x = nothing).

  • 7/30/2019 Java: Week 1

    7/30

    Java Libraries and Open Source

    Java includes many reusable tools and

    objects that are part of the language Additional open-source tools and classes can

    be found on the web that have been built by

    the open source community and can be re-

    used in your applications

  • 7/30/2019 Java: Week 1

    8/30

    Java is object-oriented

    Concept Description Example

    Class Job Description Instructor

    Method Task doLecture()Package Collection of classes itt.common

    Object Instance of a class russellDobda

    All code must be written as classes!

    Java is Case Sensitive!

  • 7/30/2019 Java: Week 1

    9/30

    3 types of Java Programs

    1. Java Application

    Standalone; run from command line2. Java Applet

    Runs in a web browser

    Downloaded from server and run client-side

    3. Java Servlet

    Resides and runs on web server

  • 7/30/2019 Java: Week 1

    10/30

    Java Servlets and JSP

  • 7/30/2019 Java: Week 1

    11/30

    System Development

    Always plan before you code. Understand the

    problem and plan a solution.

    Always test. Test driven development encourages

    writing tests first and creating automated build

    scripts that run the tests

    Dont wait until the last minute! Count on building a

    program over several days.

    Assume you will be rethinking and refactoring as

    you go; just like writing a novel

  • 7/30/2019 Java: Week 1

    12/30

    Phases of the Software Development Life Cycle

    Needs Analysis: The Why

    Define the problem Requirements Analysis: The What

    Define the solution in terms of businessrequirements

    Consideration of Who and Where

    Design: The How High Level and Detailed Design Flow charts, psuedocode

    Development

    Construction of the Software and unit testing Unit Testing (Unit, Integration)

    Testing: User Acceptance, System Testing Implementation

  • 7/30/2019 Java: Week 1

    13/30

    Programming in Java

  • 7/30/2019 Java: Week 1

    14/30

    Java Coding - Comments

    //this is a comment

    /* this is amulti-line comment */

    /** this is javadoc documentation* that contains standard information* @author Russell Dobda*/

  • 7/30/2019 Java: Week 1

    15/30

    Java Coding Class definitions

    public class MyClass {//your code here

    }

    Every java program must contain at least one

    Usually, each .java file will contain a singleclass

    The filename must match the class name

  • 7/30/2019 Java: Week 1

    16/30

    Java Application main() method

    public class MyClass{

    public static void main(String args[])

    {

    System.out.println(Hello World!);

    }

    }Question: what are the inputs and outputs of

    the main method?

  • 7/30/2019 Java: Week 1

    17/30

    Standards

    Java ignores whitespace, so you can use as

    much or as little as you want, but pleasemaintain readibility!!

    Bracket positioning (two standards, use

    either)

  • 7/30/2019 Java: Week 1

    18/30

    Primitive data types

    Java includes 8 primitive (non-object) data types. They

    begin with a lower-case

    boolean int

    byte long

    char float

    short double

    See page 50

    for detailed

    descriptions

  • 7/30/2019 Java: Week 1

    19/30

    Declaring variables in Java

    data_type variable_name;

    Examplesfloat balance;

    float balance = 2000.0;

    float balance, deposit, withdrawal;

    float balance=2000.0, deposit=50.0;

    You should always initialize your variables to

    at least null or 0

  • 7/30/2019 Java: Week 1

    20/30

    The String Class

    Java class that has many built-in methods forreplace, find, case, etc

    Concatenate strings with the + operator

    String instructorName = Mr. Dobda;

    System.out.println(Your instructoris + instructorName);

    Note that System.out.println allows you towrite a line of text to the output console

  • 7/30/2019 Java: Week 1

    21/30

    Escape sequences for strings

    Sequence Result Example

    \ Quotes value = He said \Hello\

    \n New

    line

    value = Are you sure\n

    you want to do this?

    \\ Back-

    slash

    Value = Go to c:\\My

    Documents

  • 7/30/2019 Java: Week 1

    22/30

    Final variables (aka constants)

    These values cannot be modified

    Use all caps with underscores

    public final String SCHOOL_LOCATION = ITT;

    If public, it can be referenced from other classes

    System.out.println(You go to +

    MyClass.SCHOOL_LOCATION);

  • 7/30/2019 Java: Week 1

    23/30

    Naming conventions

    Class: Descriptive noun or noun phrases.Starts with caps and uses caps for each word

    Method: start with lower case, use caps foreach subsequent word

    Variable: same as methods. Be descriptive!

    Use single letter variable names only for loopindexes

    Final variable: CAPS_WITH_UNDERSCORE

  • 7/30/2019 Java: Week 1

    24/30

    Java Operators

    See page 57 for full list

    and precedence

    Question: what is the

    precedence of the

    following:

    fTemp = 9.0/5.0 *cTemp + 32.0;

    ! Logical not

    == Same as

    != Not same as

    && Conditional and

    || Conditional or= Assignment

  • 7/30/2019 Java: Week 1

    25/30

    Assignment operator (=)

    //valid assignments

    a = 1;b = 2;

    a = b = c = 0;

    double x = 32.0;//invalid assignment

    32 = z;

  • 7/30/2019 Java: Week 1

    26/30

    Casting Variables

    You can cast variables from one type toanother when direct assignment is not an

    option:float f = (float)9.8765

    Java considers everything with a decimal to be adouble. Since float takes up less memory and

    holds up to 7 decimal places, you can cast 9.8765as a float.

    There are better real-world examples out therethan this, especially when you get into objects

    int i = (int) f //what is i?

  • 7/30/2019 Java: Week 1

    27/30

    Java Integer Arithmetic

    int a = 16, b=3, c, d;

    c = a / b;d = a % b;

    The result of 16/3 is 5 remainder of 1;

    therefore, c = 5, d = 1

  • 7/30/2019 Java: Week 1

    28/30

    Java Arithmetic of differing types

    int a = 3;

    double b = 2.0, c; //note decimal!c = a / b;

    When doing this math, Java will promote thenumber with less precision to the higher

    precision (i.e. int to double) to get a result ofthe higher precision (see table 2.10, pg 66)

    c ends up as .75

  • 7/30/2019 Java: Week 1

    29/30

    Increment and Decrement Operators

    These provide a quick way to add or subtract

    1 from a variable

    i++ or ++i will add 1 to i

    i-- or --i will subtract 1 from i

    When assigning values, there is a difference

    between i++ and ++i (see table 2.12 p70)

  • 7/30/2019 Java: Week 1

    30/30

    Accumulation Operators

    Operator Example Result

    += sum += x sum = sum + x

    -= sum -= x sum = sum x

    *= sum *= x sum = sum * x

    /= sum /= x sum = sum / x

    Note that you dont NEED to use

    accumulation operators, but they are a nice

    shorthand.