0% found this document useful (0 votes)
9 views24 pages

Chapter 2

Chapter 2 covers the declaration and use of constants and variables in Java, explaining the concepts of variables, data types, and naming conventions. It details primitive and reference types, variable declaration, assignment, and the use of the Scanner class for input. The chapter also discusses arithmetic operations, operator precedence, and the implications of floating-point precision.

Uploaded by

2sdhhtp5sd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views24 pages

Chapter 2

Chapter 2 covers the declaration and use of constants and variables in Java, explaining the concepts of variables, data types, and naming conventions. It details primitive and reference types, variable declaration, assignment, and the use of the Scanner class for input. The chapter also discusses arithmetic operations, operator precedence, and the implications of floating-point precision.

Uploaded by

2sdhhtp5sd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Chapter 2:

Using Data
Declaring and Using Constants and
Variables (cont’d.)
• Variable
– A named memory location
– Used to store a value
– Can hold only one value at a time
– Its value can change
• Data type
– A type of data that can be stored
– How much memory an item occupies
– What types of operations can be performed on data

Java Programming, Eighth Edition 2


Declaring and Using Constants and
Variables (cont’d.)
• Primitive type
– A simple data type
• Reference types
– More complex data types

Java Programming, Eighth Edition 3


Declaring and Using Constants and
Variables (cont’d.)

Java Programming, Eighth Edition 4


Declaring Variables
• Name variables
– Use naming rules for legal class identifiers
• Variable declaration
– A statement that reserves a named memory location
– Includes:
• Data type
• Identifier
• Optional assignment operator and assigned value
• Ending semicolon

Java Programming, Eighth Edition 5


Declaring Variables (cont’d.)
• Assignment operator
– The equal sign (=)
– The value to the right is assigned to the variable on the left
• Initialization
– An assignment made when declaring a variable
• Assignment
– An assignment made after a variable is declared
• Associativity
– The order in which operands are used with operators

Java Programming, Eighth Edition 6


Declaring Variables (cont’d.)
• Declare multiple variables of the same type in
separate statements on different lines
int myAge = 25;
int yourAge = 19;
• When declaring variables of different types, you
must use a separate statement for each type

Java Programming, Eighth Edition 7


Declaring Named Constants
• A named constant:
– Should not change during program execution
– Has a data type, name, and value
– Has a data type preceded by the keyword final
– Can be assigned a value only once
– Conventionally is given identifiers using all uppercase
letters

Java Programming, Eighth Edition 8


Concatenating Strings to Variables
and Constants
• print() or println() statement
– Use alone or in combination with a String
• Concatenated
– A numeric variable is concatenated to a String using the
plus sign
– The entire expression becomes a String
• The println() method can accept a number or
String

Java Programming, Eighth Edition 9


Concatenating Strings to Variables
and Constants (cont’d.)
• Use a dialog box to display values
JOptionPane.showMessageDialog()
– Does not accept a single numeric variable
• Null String
– An empty string: ""

Java Programming, Eighth Edition 10


Learning About Integer Data Types
• int data type
– Stores an integer, or whole number
– Value from –2,147,483,648 to +2,147,483,647
• Variations of the integer type
– byte
– short
– long
• Choose appropriate types for variables

Java Programming, Eighth Edition 11


Using the boolean Data Type
• Boolean logic
– Based on true-or-false comparisons
• boolean variable
– Can hold only one of two values
– true or false
boolean isItPayday = false;
• Relational operator (comparison operator)
– Compares two items

Java Programming, Eighth Edition 12


Using the boolean Data Type
(cont’d.)

Java Programming, Eighth Edition 13


Learning About Floating-Point
Data Types
• Floating-point number
– Contains decimal positions
• Floating-point data types
– float
– double
• Significant digits
– Refers to mathematical accuracy

Java Programming, Eighth Edition 14


Using the char Data Type
• char data type
– Holds any single character
• Place constant character values within single
quotation marks
char myMiddleInitial = 'M';
• String
– A built-in class
– Stores and manipulates character strings
– String constants are written between double quotation
marks
Java Programming, Eighth Edition 15
Using the Scanner Class to
Accept Keyboard Input
• System.in object
– Standard input device
– Normally the keyboard
– Access using the Scanner class
• Scanner object
– Breaks input into units called tokens

Java Programming, Eighth Edition 16


Using the Scanner Class to
Accept Keyboard Input (cont’d.)

Java Programming, Eighth Edition 17


Pitfall: Using nextLine() Following One
of the Other Scanner Input Methods
• There is a problem when using one numeric
Scanner class retrieval method or next()method
before using the nextLine()method
• Keyboard buffer
– Location in memory that stores all keystrokes, including
Enter
• To avoid issues, add an extra nextLine()method
call to retrieve the abandoned Enter key character
after numeric or next() inputs

Java Programming, Eighth Edition 18


Using Input Dialog Boxes
• Input dialog box
– Asks a question
– Provides a text field in which the user can enter a response
• showInputDialog() method
– Six overloaded versions
– Returns a String representing a user’s response
• Prompt
– A message requesting user input

Java Programming, Eighth Edition 19


Performing Arithmetic
• Standard arithmetic operators
– Perform calculations with values in programs
• Operand
– A value used on either side of an operator
• Integer division
– Involves integer constants or integer variables
– The result is an integer
– Any fractional part of the result is lost

Java Programming, Eighth Edition 20


Performing Arithmetic (cont’d.)

Java Programming, Eighth Edition 21


Associativity and Precedence
• Operator precedence
– The rules for the order in which parts of mathematical
expressions are evaluated
– First multiplication, division, and remainder (modulus),
then addition or subtraction

Java Programming, Eighth Edition 22


Pitfall: Not Understanding Imprecision
in Floating-Point Numbers
• Integer values are exact
– But floating-point numbers frequently are only
approximations
• Imprecision leads to several problems
– Floating-point output might not look like what you expect
or want
– Comparisons with floating-point numbers might not be
what you expect or want

Java Programming, Eighth Edition 23


Automatic Type Conversion
• Automatically converts nonconforming operands to
the unifying type
• Order for establishing unifying types between two
variables (highest to lowest):
1. double
2. float
3. long
4. int

Java Programming, Eighth Edition 24

You might also like