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

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

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

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

Java Programming, Java Programming, Second Second EditionEdition

Chapter TwoChapter Two

Using Data Within a ProgramUsing Data Within a Program

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

In this chapter, you will:In this chapter, you will:

Use constants and variablesUse constants and variables Learn about the int data typeLearn about the int data type Write arithmetic statementsWrite arithmetic statements Use the Boolean data typeUse the Boolean data type Learn about floating-point data typesLearn about floating-point data types Understand numeric type conversionUnderstand numeric type conversion Work with the char data typeWork with the char data type Learn about ASCII and UnicodeLearn about ASCII and Unicode

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

Using Constants and VariablesUsing Constants and Variables

ConstantConstant is when the value cannot be is when the value cannot be changed after the program is compiledchanged after the program is compiled

VariableVariable is when the value changes after is when the value changes after compile-timecompile-time

Literal constantLiteral constant is the actual number or is the actual number or value, such as 459value, such as 459

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

Primitive Types of DataPrimitive Types of Data

booleanboolean floatfloat bytebyte intint charchar longlong doubledouble shortshort

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

Primitive TypesPrimitive Types

The eight primitive data types are called The eight primitive data types are called primitive typesprimitive types because they are simple because they are simple and uncomplicatedand uncomplicated

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

Variable DeclarationVariable Declaration

A variable declaration includes the following:A variable declaration includes the following: A data type that identifies the type of data that A data type that identifies the type of data that

the variable will storethe variable will store An identifier that is the variable’s nameAn identifier that is the variable’s name An optional assigned value (initialization vs. An optional assigned value (initialization vs.

assignment), when you want a variable to assignment), when you want a variable to contain an initial valuecontain an initial value

An ending semicolonAn ending semicolon

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

IntegersIntegers

Variables of type Variables of type byte, short, longbyte, short, long and and intint store store integersintegers or whole numbers or whole numbers

intint integers can be any whole number integers can be any whole number from -2,147,483,648 to 2,147,483,647from -2,147,483,648 to 2,147,483,647

Page 8: Java Programming, Second Edition Chapter Two Using Data Within a Program.
Page 9: Java Programming, Second Edition Chapter Two Using Data Within a Program.

Integer Arithmetic OperatorsInteger Arithmetic Operators

Page 10: Java Programming, Second Edition Chapter Two Using Data Within a Program.
Page 11: Java Programming, Second Edition Chapter Two Using Data Within a Program.

Boolean VariablesBoolean Variables

Can hold only one of two values:Can hold only one of two values: True or falseTrue or false

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

Floating-Point Data TypesFloating-Point Data Types

Floating-pointFloating-point numbers contain decimal numbers contain decimal positionspositions

AA float float data type can hold values up to 6 data type can hold values up to 6 or 7 significant digits of accuracyor 7 significant digits of accuracy

AA double double data type can hold 14 or 15 data type can hold 14 or 15 significant digits of accuracysignificant digits of accuracy

Page 13: Java Programming, Second Edition Chapter Two Using Data Within a Program.
Page 14: Java Programming, Second Edition Chapter Two Using Data Within a Program.

Unifying TypeUnifying Type

The type a result is given when The type a result is given when calculations use unlike typescalculations use unlike types

Java implicitly converts different operands Java implicitly converts different operands to the larger unifying typeto the larger unifying type

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

Order for Implicitly Establishing Order for Implicitly Establishing Unifying TypeUnifying Type

1.1. doubledouble

2.2. floatfloat

3.3. longlong

4.4. intint

5.5. shortshort

6.6. bytebyte

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

Type CastingType Casting

The unifying type can be overridden by The unifying type can be overridden by explicitly stating a type castexplicitly stating a type cast

Place the desired type result in Place the desired type result in parentheses followed by the variable or parentheses followed by the variable or constant to be castconstant to be cast

Example:Example:

float weeklybudget = float weeklybudget = (float) bankbalance(float) bankbalance /4;/4;

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

Working with the char Data Working with the char Data TypeType

char data type is used to hold a single char data type is used to hold a single charactercharacter

Uses single quotation marksUses single quotation markschar myInitial = ‘B’;char myInitial = ‘B’;

StringsStrings

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

String Data StructuresString Data Structures

A string can contain a string of charactersA string can contain a string of characters Uses double quotation marksUses double quotation marks

String firstName = “Bryson”;String firstName = “Bryson”;

Escape sequences are used to print Escape sequences are used to print special characters like “, \, tabs, new lines, special characters like “, \, tabs, new lines, etc.etc.

Page 19: Java Programming, Second Edition Chapter Two Using Data Within a Program.
Page 20: Java Programming, Second Edition Chapter Two Using Data Within a Program.

ASCII and UnicodeASCII and Unicode

Both are character setsBoth are character sets ASCII is an 8 bit coding scheme (128 ASCII is an 8 bit coding scheme (128

chars)chars) ASCII is most widely used coding schemeASCII is most widely used coding scheme Unicode is a 16 bit coding scheme (room Unicode is a 16 bit coding scheme (room

for 65536 characters) for 65536 characters) Java programming language uses Java programming language uses

UnicodeUnicode