CSC 313 Java Chapter 2 Data Types Notes to accompany Beginning Java 2 by Ivor Horton - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

CSC 313 Java Chapter 2 Data Types Notes to accompany Beginning Java 2 by Ivor Horton

Description:

Notes to accompany Beginning Java 2 by Ivor Horton. Java Data Types. Java has two kinds of types: ... No variable can have an undefined value. ... – PowerPoint PPT presentation

Number of Views:114
Avg rating:3.0/5.0
Slides: 20
Provided by: gar59
Category:

less

Transcript and Presenter's Notes

Title: CSC 313 Java Chapter 2 Data Types Notes to accompany Beginning Java 2 by Ivor Horton


1
CSC 313 JavaChapter 2 Data Types Notes to
accompany Beginning Java 2 by Ivor Horton
2
Java Data Types
  • Java has two kinds of types
  • fundamental, primitive or simple
  • derived or composite
  • No variable can be used until it is defined.
  • Java is a strongly typed language.
  • Sun Tutorial on Data Types

3
Java Primitive Data Types
4
Data Types, Default Values
  • No variable can have an undefined value.
  • Instance or class variables are implicitly
    initialized to a standard default value.
  • Local variables are not implicitly initialized to
    a default value and must be explicitly
    initialized.

5
Identifier Names
  • An identifier can be any length, but it must
    start with a letter, an underscore, or a dollar
    sign.
  • The rest of an identifier can include any
    characters except those used as operators in Java
    (such as , -, or ).
  • It is generally better if you stick to letters,
    digits, and underscore characters.
  • Java is a case-sensitive language

6
Integer Variables
  • Declaring and Initializing
  • int myNumber 12345
  • byte luckyNumber 7
  • short smallNumber 1234
  • long bigOne 999999999L
  • long big 999999999L, largeNum 100000000L
  • long xCoord0L, yCoord0L

7
Decimal Constants
  • A decimal constant must not begin with a 0. An
    octal constant begins with a 0 and a hexadecimal
    constant begins with 0x or 0X.
  • The java.math package has a BigInteger class for
    arbitrary-precision integer arithmetic.

8
Floating-point types
  • The floating-point types are float (32 bits) and
    double (64 bits).
  • A floating-point constant such as 3.14 is of type
    double, whereas 3.14F and 3.14f are of type
    float.

9
Floating point Variables
  • As with integer calculations, floating point
    calculations in Java will produce the same
    results on any computer. Declaring and
    Initializing
  • double sunDistance 1.496E8
  • float hisWeight 185.2F

10
Mixed Arithmetic Expressions
  • The way mixed expressions are treated is
    governed by some simple rules that apply to each
    operator in such an expression. The first rule
    that applies is the one that is used
  • If either operand is of type double, the other is
    converted to double before the operation is
    carried out.
  • If either operand is of type float, the other is
    converted to float before the operation is
    carried out.
  • If either operand is of type long the other is
    converted to long before the operation is carried
    out.
  • If neither operand is double, float or long, they
    must be int, short, or byte, so they use 32-bit
    arithmetic.

11
Mathematical Functions
  • Java provides a range of methods that support
    such functions as part of the standard library
    stored in the package java.lang, and all these
    are available in your program automatically.
  • The methods that support various additional
    mathematical functions are implemented in the
    class Math.
  • To reference a particular function you need to
    write Math and a period in front of the function
    name. To use sqrt(), which calculates the square
    root of whatever you place between the
    parentheses, you would write
    Math.sqrt(aNumber)

12
Mathematical Functions
  • Sun Documentation
  • Math class

13
Explicit Casting
  • Sometimes the default treatment of mixed
    expressions is not what you want. For example, if
    you have a double variable result, and you
    compute its value using two int variables three
    and two with the values 3 and 2, with the
    statement,
  • result  1.5  three/two
  • the value stored will be 2.5. You may have wanted
    the term three/two to produce the value 1.5 so
    the overall result would be 3.0. You could do
    this using an explicit castresult  1.5  (doubl
    e)three/two

14
Explicit Casting
  • This causes the value stored in three to be
    converted to double before the divide operation
    takes place. Then rule 1 applies for the divide
    operation, and the operand two is also converted
    to double before the divide is executed.
  • Hence the value of result will be 3.0.

15
Explicit Casting
  • You can cast any of the basic types to any other,
    but you need to take care that you don't lose
    information when you do so.
  • Casting from a larger integer type to a smaller
    has the potential for losing information, as does
    casting any floating point value to an integer.
  • Casting from double to float can also produce
    effective infinity when the original value is
    greater than 1038.

16
boolean Variables
  • Recall that boolean expressions evaluate either
    true of false.
  • You can use boolean variables to store the result
    of evaluating a boolean expression boolean
    midDay (hour 12)
  • Unlike C you can not assign a numeric value to
    boolean data, although you can use the values of
    true false. boolean flag false

17
Casting
  • In a cast operation, the target type is enclosed
    in parentheses.
  • Some restrictions imposed on casts. For example,
    a boolean value such as true cannot be cast to
    any other type, and no nonboolean value can be
    cast to boolean.
  • Casts should be used with great caution.

18
Data type char
  • char variables can store any character in the
    Unicode character set
  • This includes any keyboard character in addition
    to many others.
  • You must enclose char values in single quotes
  • E.g. b, 1,
  • E.g. char letter a
  • Some special Unicode characters are known as
    escape sequences
  • E.g. \n is the new-line character

19
Thats All Folks!
Write a Comment
User Comments (0)
About PowerShow.com