1 CS1001 Lecture 13. 2 Overview Java Programming Java Programming.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
-
Upload
nelson-moody -
Category
Documents
-
view
212 -
download
0
Transcript of 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
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
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
Primitive Types of DataPrimitive Types of Data
booleanboolean floatfloat bytebyte intint charchar longlong doubledouble shortshort
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
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
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
Integer Arithmetic OperatorsInteger Arithmetic Operators
Boolean VariablesBoolean Variables
Can hold only one of two values:Can hold only one of two values: True or falseTrue or false
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
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
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
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;
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
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.
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