The Nuts and Bolts of the Java Language - PowerPoint PPT Presentation

1 / 39
About This Presentation
Title:

The Nuts and Bolts of the Java Language

Description:

{ public static void main(String[] args) throws java.io.IOException. int count = 0; ... There are two major categories of data types in the Java language: primitive ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 40
Provided by: tzone
Category:

less

Transcript and Presenter's Notes

Title: The Nuts and Bolts of the Java Language


1
The Nuts and Bolts of the Java Language
  • Variables and Data Types
  • Operators
  • Expressions
  • Control Flow Statements
  • Arrays and Strings
  • Other Features of the Character-Counting
    Application

2
The Character-Counting Application
  • class Count
  • public static void main(String args)
  • throws java.io.IOException
  • int count 0
  • while (System.in.read() ! -1)
  • count
  • System.out.println("Input has " count "
    chars.")

3
Variables and Data Types
  • There are two major categories of data types in
    the Java language primitive types and reference
    types.
  • Primitive types contain a single value and
    include types such as integer, floating point,
    character, and boolean.

4
Primitive types
  • Type Size/Format Description
  • (whole numbers-integers)
  • Byte 8-bit two's complement Byte-length
    integer
  • Short 16-bit two's complement Short integer
  • int 32-bit two's complement Integer
  • Long 64-bit two's complement Long integer

5
Primitive types
  • (real numbers)
  • Float 32-bit IEEE 754 Single-precision
    floating point
  • Double 64-bit IEEE 754 Double-precision
    floating point
  • (other types)
  • char 16-bit Unicode character A single
    character
  • boolean true or false A boolean value
    (true or false)

6
Reference types
  • The value of a reference variable is a reference
    (a pointer in other terminology) to the actual
    value or set of values represented by the
    variable.
  • The name of an array is a reference
  • Besides arrays, classes and interfaces are also
    reference types.
  • Thus when you create a class or interface you are
    in essence defining a new data type.

7
primitive types vs. reference types
  • Name args ? evaluates to the address of the
    memory location where the array lives.
  • The count ? evaluates to the variable's actual
    value.

8
Note to C and C Programmers
  • There are three C Data Types Not Supported By the
    Java Language.
  • They are pointer, struct, and union.
  • These data types are not necessary in Java you
    use classes and objects instead.

9
Variable Names
  • In Java, a variable name
  • must be a legal Java identifier comprised of a
    series of Unicode characters.
  • must not be the same as a keyword or a boolean
    literal (true or false)
  • must not have the same name as another variable
    whose declaration appears in the same scope

10
Scope
  • A variable's scope is the block of code within
    which the variable is accessible.
  • A variable's scope determines when the variable
    is created and destroyed.

11
Scope
  • Scope places a variable into one of these four
    categories
  • member variable
  • local variable
  • method parameter
  • exception handler parameter

12
member variable
  • A member variable is a member of a class or an
    object and is declared within a class (but not
    within any of the class's methods).
  • class IntegerClass
  • int anInteger
  • . . .
  • // define methods here
  • . . .

13
Local variables
  • Local variables are declared within a method or
    within a block of code in a method.
  • In general, a local variable is accessible from
    its declaration to the end of the code block in
    which it was declared.

14
Method parameters
  • Method parameters are formal arguments to methods
    and constructors and are used to pass values into
    methods and constructors.
  • The scope of a method parameter is the entire
    method or constructor for which it is a
    parameter.

15
Exception handler parameters
  • Exception handler parameters are similar to
    method parameters but are arguments to an
    exception handler rather than to a method or a
    constructor.

16
Variable Initialization
  • Local variables and member variables can be
    initialized when they are declared.
  • Method parameters and exception handler
    parameters cannot be initialized in this way. The
    value for a parameter is set by the caller.

17
Operators
  • Operators perform some function on either one or
    two operands.
  • Operators that require one operand are called
    unary operators. For example, is a unary
    operator that increments the value of its operand
    by one.
  • Operators that require two operands are binary
    operators

18
Operators
  • Java's unary operators can use either prefix or
    postfix notation.
  • All of Java's binary operators use infix
    notation.
  • In addition to performing the operation, an
    operator also returns a value.
  • The data type returned by the arithmetic
    operators depends on the type of its operands.

19
Operators
  • It's useful to divide Java's operators into these
    categories arithmetic, relational and
    conditional, bitwise and logical, and assignment.

20
Arithmetic Operators
  • Operator Use Description
  • op1 op2 Adds op1 and op2
  • - op1 - op2 Subtracts op2 from op1
  • op1 op2 Multiplies op1 and op2
  • / op1 / op2 Divides op1 by op2
  • op1 op2 Computes the remainder
  • of dividing op1 by op2

21
Arithmetic Operators
  • Note The Java language extends the definition of
    the operator to include string concatenation.
  • The example program uses to concatenate "Input
    has ", the value of count, and " chars."
  • Note that this operation automatically coerces
    the value count to a String.
  • System.out.println("Input has " count "
    chars.")

22
Arithmetic Operators
  • Operator Use Description
  • op Increments op by 1 evaluates to
    value
  • before incrementing
  • op Increments op by 1 evaluates to
    value
  • after incrementing
  • -- op -- Decrements op by 1 evaluates
    to value
  • before decrementing
  • -- -- op Decrements op by 1 evaluates
    to value
  • after decrementing

23
Arithmetic Operators
  • do
  • . . .
  • while (count lt 6)
  • do
  • . . .
  • while (countlt 6)

24
Relational and Conditional Operators
  • Operator Use Returns true if
  • gt op1 gt op2 op1 is greater than op2
  • gt op1 gt op2 op1 is greater than or equal
    to op2
  • lt op1 lt op2 op1 is less than to op2
  • lt op1 lt op2 op1 is less than or equal to
    op2
  • op1 op2 op1 and op2 are equal
  • ! op1 ! op2 op1 and op2 are not equal

25
conditional operators
  • Operator Use Returns true if
  • op1 op2 op1 and op2 are both true
  • op1 op2 either op1 or op2 is true
  • ! ! op op is false

26
Bitwise Operators
  • Operator Use Operation
  • gtgt op1 gtgt op2 shift bits of op1 right by
    distance op2
  • ltlt op1 ltlt op2 shift bits of op1 left by
    distance op2
  • gtgtgt op1 gtgtgt op2 shift bits of op1 right by
    distance
  • op2 (unsigned)
  • op1 op2 bitwise and
  • op1 op2 bitwise or
  • op1 op2 bitwise xor
  • op bitwise complement

27
Bitwise Operators
  • final int VISIBLE 1
  • final int DRAGGABLE 2
  • final int SELECTABLE 4
  • final int EDITABLE 8
  • int flags 0
  • To set the "visible" flag when something became
    visible you would use this statement
  • flags flags VISIBLE
  • To test for visibility, you could then write
  • flags VISIBLE

28
Assignment Operators
  • Operator Use Equivalent to
  • op1 op2 op1 op1 op2
  • - op1 - op2 op1 op1 - op2
  • op1 op2 op1 op1 op2
  • / op1 / op2 op1 op1 / op2
  • op1 op2 op1 op1 op2

29
Assignment Operators
  • Operator Use Equivalent to
  • op1 op2 op1 op1 op2
  • op1 op2 op1 op1 op2
  • op1 op2 op1 op1 op2
  • ltlt op1 ltlt op2 op1 op1 ltlt op2
  • gtgt op1 gtgt op2 op1 op1 gtgt op2
  • gtgtgt op1 gtgtgt op2 op1 op1 gtgtgt op2

30
Expressions
  • Operator Precedence in Java
  • postfix operators . (params) expr
    expr--
  • unary operators expr --expr expr
    -expr !
  • creation or cast new (type)expr
  • multiplicative /
  • additive -
  • shift ltlt gtgt gtgtgt
  • relational lt gt lt gt
    instanceof
  • equality !
  • bitwise AND
  • bitwise exclusive OR
  • bitwise inclusive OR
  • logical AND
  • logical OR
  • conditional ?
  • assignment - /
    ltlt gtgt gtgtgt

31
Control Flow Statements
  • Statement Keyword
  • decision making if-else, switch-case
  • loop for, while, do-while
  • exception try-catch-finally, throw
  • miscellaneous break, continue, label,
    return

32
Exception Handling Statements
  • When an error occurs within a Java method, the
    method can throw an exception to indicate to its
    caller that an error occurred and what the error
    was using the throw statement.
  • The calling method can use the try, catch, and
    finally to catch and handle the exception.

33
Branching Statements
  • breakToHere someJavaStatement
  • To jump to the statement labeled breakToHere use
    this form of the break statement.
  • break breakToHere
  • Note The continue statement can only be called
    from within a loop.

34
Branching Statements
  • public int indexOf(String str, int fromIndex)
  • char v1 value
  • char v2 str.value
  • int max offset (count - str.count)
  • test
  • for (int i offset ((fromIndex lt 0) ? 0
    fromIndex) i lt max i)
  • int n str.count
  • int j i
  • int k str.offset
  • while (n-- ! 0)
  • if (v1j ! v2k)
  • continue test
  • return i - offset
  • return -1

35
Arrays and Strings
  • You cannot have a generic array--the data type of
    its elements must be identified when the array is
    declared.
  • int arrayOfInts
  • Error Message testing.java64 Variable
    arrayOfInts may not have been initialized.
  • int arrayOfInts new int10

36
Arrays and Strings
  • The steps to create an array are similar to the
    steps to create an object from a class
    declaration, instantiation, and initialization.
  • In general, when creating an array, you use the
    new operator, plus the data type of the array
    elements, plus the number of elements desired
    enclosed within square brackets ('' and '').
  • elementType arrayName new elementTypearraySi
    ze

37
Arrays and Strings
  • Notice You can use length property provided for
    all Java arrays to retrieve the current length of
    an array.
  • for (int j 0 j lt arrayOfInts.length j )
  • arrayOfIntsj j
  • System.out.println("j "
    arrayOfIntsj)

38
Arrays and Strings
  • String is implemented in the Java environment by
    the String class (a member of the java.lang
    package)
  • String objects are immutable--that is, they
    cannot be changed once they've been created.
  • The java.lang package provides a different class,
    StringBuffer, which you can use to create and
    manipulate character data on the fly.

39
Arrays and Strings
  • A String variable contains a reference to a
    String object
  • Thus, Arrays containing String objects must be
    used with care
  • String arrayOfStrings new String10
  • for (int i 0 i lt arrayOfStrings.length
    i )
  • arrayOfStringsi new
    String("Hello " i)
Write a Comment
User Comments (0)
About PowerShow.com