Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Programming, Second Edition Chapter Two Using Data Within a Program.

Similar presentations


Presentation on theme: "Java Programming, Second Edition Chapter Two Using Data Within a Program."— Presentation transcript:

1 Java Programming, Second Edition Chapter Two Using Data Within a Program

2 In this chapter, you will:  Use constants and variables  Learn about the int data type  Write arithmetic statements  Use the Boolean data type  Learn about floating-point data types  Understand numeric type conversion  Work with the char data type  Learn about ASCII and Unicode

3 Using Constants and Variables  Constant is when the value cannot be changed after the program is compiled  Variable is when the value changes after compile-time  Literal constant is the actual number or value, such as 459

4 Primitive Types of Data  boolean  float  byte  int  char  long  double  short

5 Primitive Types  The eight primitive data types are called primitive types because they are simple and uncomplicated

6 Variable Declaration A variable declaration includes the following:  A data type that identifies the type of data that the variable will store  An identifier that is the variable’s name  An optional assigned value (initialization vs. assignment), when you want a variable to contain an initial value  An ending semicolon

7 Integers  Variables of type byte, short, long and int store integers or whole numbers  int integers can be any whole number from -2,147,483,648 to 2,147,483,647

8

9 Integer Arithmetic Operators

10

11 Boolean Variables  Can hold only one of two values:  True or false

12 Floating-Point Data Types  Floating-point numbers contain decimal positions  A float data type can hold values up to 6 or 7 significant digits of accuracy  A double data type can hold 14 or 15 significant digits of accuracy

13

14 Unifying Type  The type a result is given when calculations use unlike types  Java implicitly converts different operands to the larger unifying type

15 Order for Implicitly Establishing Unifying Type 1. double 2. float 3. long 4. int 5. short 6. byte

16 Type Casting  The unifying type can be overridden by explicitly stating a type cast  Place the desired type result in parentheses followed by the variable or constant to be cast  Example: float weeklybudget = (float) bankbalance /4;

17 Working with the char Data Type  char data type is used to hold a single character  Uses single quotation marks char myInitial = ‘B’;  Strings

18 String Data Structures  A string can contain a string of characters  Uses double quotation marks String firstName = “Bryson”;  Escape sequences are used to print special characters like “, \, tabs, new lines, etc.

19

20 ASCII and Unicode  Both are character sets  ASCII is an 8 bit coding scheme (128 chars)  ASCII is most widely used coding scheme  Unicode is a 16 bit coding scheme (room for 65536 characters)  Java programming language uses Unicode


Download ppt "Java Programming, Second Edition Chapter Two Using Data Within a Program."

Similar presentations


Ads by Google