Constants, Variables, String Manipulation, Data Manipulation - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Constants, Variables, String Manipulation, Data Manipulation

Description:

MINS 116 - Spring 2000. 3. Constants ... MINS 116 - Spring 2000. 4. Constants. In Java, a constant is declared using the keyword final. ... – PowerPoint PPT presentation

Number of Views:312
Avg rating:3.0/5.0
Slides: 28
Provided by: Jim455
Category:

less

Transcript and Presenter's Notes

Title: Constants, Variables, String Manipulation, Data Manipulation


1
Constants, Variables, String Manipulation, Data
Manipulation
  • MINS 116
  • Spring 2000
  • Java Basics

2
Topics
  • Constants
  • Variables
  • Data manipulation
  • String manipulation
  • Constructors

3
Constants
  • A constant is a value which will not change
    throughout the entire lifetime of your program.
  • Constants should be declared for any value
    describing
  • physical constants (e.g. pi, gravitational
    constant, etc.)
  • logical constants (e.g. array bounds, loop
    constraints, etc.)

4
Constants
  • In Java, a constant is declared using the keyword
    final.
  • The final keyword informs the compiler that the
    element being defined will not change.
  • Example
  • private final int MAX_ENTRIES 100

5
Constants
  • The use of the final keyword causes a constant to
    be defined for the scope of the class in which it
    was defined.
  • E.g.
  • public class UserParser
  • private final int MAX_USERS 100
  • defines a constant which can only be seen by
    class UserParser and will only exist while
    UserParser is in memory. There will also be a
    separate copy of MAX_USERS for each instance of
    class UserParser created.

6
Constants
  • The addition of keyword static will define a
    constant which holds across all instances of the
    class and will only take up a single piece of
    program memory. So
  • public class UserParser
  • private static final int MAX_USERS 100
  • defines a constant which can only be seen by
    class UserParser. There will only be a single
    MAX_USERS for all instances of class UserParser
    created. Basically, MAX_USERS is a global
    constant with controlled access.

7
Variables
  • A variable is used to hold information which is
    variable and will change throughout the life of
    the program.
  • Ex
  • public class UserParser
  • ...
  • private int charCount 0 // Number of
    characters processed so far
  • ...

8
Variables
  • charCount can be used throughout the life of
    class UserParser.
  • Variables can be
  • Any of the primitive data types (e.g. int, float,
    char, etc.).
  • Classes included in the Java support files (e.g.
    String, Int, Float, Vector, JFrame, etc.)
  • Classes created by other programmers and classes
    you have created yourself.

9
Variables - Primitive Data Types
  • Java has a defined set of primitive data types.
  • These are data types which are not considered
    classes and have no methods associated with them.

10
Variables - Primitive Data Types
  • Type Size in bits
    Value Range
  • boolean 8 true or false
  • char 16 \u0000 to
    \uFFFF
  • byte 8 -128 to 127
  • short 16 -32,768 to 32,767
  • int 32 -2,147,438,648 to 2,147,438,647
  • long 64 -9,223,372,036,854,775,808 to
  • 9,223,372,036,854,775,808
  • float 32 -3.40292347E38 to
  • 3.40292347E38
  • double 64 -1.79769313486231570 to
  • 1.79769313486231570

11
Data Manipulation
  • Data Manipulation encompasses
  • Arithmetic operations
  • Functions provided by the Java support classes.
  • Logical operations

12
Data Manipulation - Arithmetic Operations
  • Available Arithmetic Operators
  • Assignment
  • Precedence ( )
  • Addition x ab
  • Subtraction - x a-b
  • Multiplication x ab
  • Division / x a/b
  • Modulus x ab

13
Data Manipulation - Arithmetic Operations
  • Arithmetic Expressions obey a set precedence for
    evaluation.
  • ( ) Inside evaluated first. If nested,
    innermost
  • first. Left to right on the same
    level.
  • , / or Evaluated second. Left to right for
    several.
  • or - Evaluated last. Left to right for
    several.

14
Data Manipulation - Arithmetic Operations
  • One thing nice about Java is it what is called
    Strongly Typed. A strongly typed language will
    not allow conversion between data types unless
    there is a well defined conversion routine.
  • Java will, however, let you typecast certain
    variables. Doing this can produce erroneous
    results.

15
Data Manipulation - Arithmetic Operations
  • For instance
  • private int iResult 0
  • private int iVar 12
  • private float fVar 10.59
  • iResult iVar (int)fVar
  • What is in iResult?

16
Data Manipulation - Arithmetic Operations
  • In addition to the standard arithmetic operators,
    arithmetic operation may be combined with the
    assignment operator
  • operator use translation
  • c 5 c c5
  • - c - 5 c c-5
  • c 5 c c5
  • / c / 5 c c/5
  • c 5 c c 5

17
Data Manipulation - Arithmetic Operations
  • And finally, there is the auto operators which
    cause a certain increment or decrement to happen
    either before or after an arithmetic evaluation
  • operator use translation
  • c c c1
  • c (a) c a a a1
  • c (a) a a1 c a
  • -- c-- c c-1
  • c (a--) c a a a-1
  • c (--a) a a-1 c a

18
Data Manipulation - Support Classes
  • Be careful with data type conversions.
  • Java provides some utility classes to help out
    with this. They are
  • Byte Character Double
  • Float Integer Long
  • Number Short String
  • BigDecimal BinInteger

19
Data Manipulation - Logical Operations
  • Logical operations can be performed to direct the
    control of a program. For instance, an if-else
    statement will rely on the evaluation of a
    logical operation to direct the logic of the
    program.

20
Data Manipulation - Logical Operations
  • Available Logical Operators
  • Symbol Meaning Syntax
  • lt Less Than a lt b
  • lt Less Than or Equal a lt b
  • gt Greater Than a gt b
  • gt Greater Than or Equal a gt b
  • Equal a b
  • ! Not Equal a ! b
  • Precedence of logical operators is last and based
    on a left to right evaluation.

21
String Manipulation
  • String manipulation in Java is special. Java
    Strings understand how to do many nice things
    implicitly.
  • When dealing with Strings, the symbol becomes
    the concatenation operator.
  • Ex
  • private String firstString Java
  • private String secondString MINS 116
  • private String thirdString firstStringsecondStr
    ing
  • What is stored in thirdString?

22
String Manipulation
  • This allows strings to be easily put together.
  • Also, many of the data types implicitly know how
    to be represented as a string.
  • Ex
  • private int firstVar 66
  • private String secondVar Route
  • private String output secondVar
    firstVar
  • What is stored in variable output?

23
Constructors
  • Every Java class has a constructor associated
    with it.
  • A constructor is automatically executed upon
    creation of a Java object.
  • Constructors should contain code which is
    required to successfully initialize a class upon
    creation.

24
Constructors
  • Example
  • public class eCustomer
  • private Vector productList
  • public eCustomer( )
  • // Perform necessary initializations
  • productList new Vector( ) //
    Create a new product list
  • ...

25
Constructors
  • Every time a new eCustomer is created, a new
    product list will be allocated.
  • New objects are created using the new
    operation.
  • thisCustomer new eCustomer( )
  • The new operation causes the allocation of
    memory for the object and the execution of the
    objects constructor after the memory has been
    allocated.

26
Constructors
  • new does not need to be called for the
    primitive data types.
  • String can be treated and initialized as though
    it were a primitive data type.
  • E.g.
  • private String name John E. Doe

27
Summary
  • Constants
  • Variables
  • Data manipulation
  • String manipulation
  • Constructors
  • review them in the book!
Write a Comment
User Comments (0)
About PowerShow.com