Skip to main content
ICT
Lesson A3 - Primitive Data Types
 
Main Previous Next
Title Page >  
Summary >  
Lesson A1 >  
Lesson A2 >  
Lesson A3 >  
Lesson A4 >  
Lesson A5 >  
Lesson A6 >  
Lesson A7 >  
Lesson A8 >  
Lesson A9 >  
Lesson A10 >  
Lesson A11 >  
Lesson A12 >  
Lesson A13 >  
Lesson A14 >  
Lesson A15 >  
Lesson A16 >  
Lesson A17 >  
Lesson A18 >  
Lesson A19 >  
Lesson A20 >  
Lesson A21 >  
Lesson A22 >  
Lesson AB23 >  
Lesson AB24 >  
Lesson AB25 >  
Lesson AB26 >  
Lesson AB27 >  
Lesson AB28 >  
Lesson AB29 >  
Lesson AB30 >  
Lesson AB31 >  
Lesson AB32 >  
Lesson AB33 >  
Vocabulary >  
 

B. Primitive Data Types in Java page 4 of 14

  1. Java provides eight primitive data types: byte, short, int, long, float, double, char, and boolean. The data types byte, short, int, and long are for integers, and the data types float and double are for real numbers (numbers with decimal places).

  2. The College Board Advanced Placement (AP) Examinations only require you to know about the int, double, and boolean data types. This curriculum will, from time to time, also use the char type when appropriate.

  3. An integer is any positive or negative number without a decimal point.

    Examples: 7 -2 0 2025

  4. A double is any signed or unsigned number with a decimal point. Doubles cannot contain a comma or any symbols. A double value can be written using scientific notation.

    • Valid numbers: 7.5 -66.72 0.125 5
    • Invalid numbers: $37,582.00 #5.0 10.72%
    • Scientific notation: 1625. = 1.625e3 .00125 = 1.25e-4

(Note: When applying 5 to a double variable, Java will automatically add the decimal point for you.)

  1. The following table summarizes the bytes allocated and the resulting size.

      Size Minimum Value Maximum Value
    byte 1 byte -128 127
    short 2 bytes -32768 32767
    int 4 bytes -2147483648 2147483647
    long 8 bytes -9223372036854775808 9223372036854775807
    float 4 bytes -3.40282347E+38 3.40282347E+38
    double 8 bytes -1.79769313486231570E+308 1.79769313486231570E+308
  2. Character type consists of letters, digits 0 through 9, and punctuation symbols. A character must be enclosed within single quotes.

    Examples: 'A', 'a', '8', '*'

    Java characters are stored using 2 bytes according to the ASCII code. ASCII stands for American Standard Code for Information Interchange. See Handout A3.2, ASCII Characters - A Partial List

    Using ASCII, the character value 'A' is actually stored as the integer value 65. Because a capital 'A' and the integer 65 are physically stored in the same fashion, this will allow us to easily convert from character to integer types, and vice versa.

  3. In Java, you can make a direct assignment of a character value to an integer variable, and vice versa. This is possible because both an integer and a character variable are ultimately stored in binary. However, it is better to be more explicit about such conversions by using type conversions. For example, the two lines of code below assign to position the ASCII value of letter.

  4. Programmers use the single quote to represent char data and double quotes to denote String types. To use either of those characters in a String literal, such as in a System.out.println statement, you must use an escape sequence. Java provides escape sequences for unusual keystrokes on the keyboard. Here is a partial list:

    Character Java Escape Sequence
    Newline '\n'
    Horizontal tab '\t'
    Backslash '\\'
    Single quote '\''
    Double quote '\"'
    Null character '\0'

  5. Data types are provided by high-level languages to minimize memory usage and processing time. Integers and characters require less memory and are easier to process. Floating-point values require more memory and time to process.

  6. The last primitive data type is the type boolean. It is used to represent a single true/false value. A boolean value can have only one of two values:

    true false

    In a Java program, the reserved words true and false always refer to these boolean values.

 

Main Previous Next
Contact
 © ICT 2006, All Rights Reserved.