Computer Programming 1: Using Data
Computer Programming 1: Using Data
Computer Programming 1: Using Data
Programming 1
Using Data
Learning Objectives
Learning Objectives
System.out.println(459);
Declaring and Using Constants and Variables
●It is a literal constant because its value is taken literally at each use.
●It is a numeric constant as opposed to a character or string
constant.
●It is an unnamed constant as opposed to a named one, because no
identifier is associated with it.
Declaring and Using Constants and Variables
Instead of using constant data, you can set up a data item to be variable. A
variable is a named memory location that can store a value. A variable can
hold only one value at a time, but the value it holds can change. For
example, if you create a variable named ovenTemperature, it might hold 0
when the application starts, later be altered to hold 350, and still later be
altered to hold 400.
Declaring and Using Constants and Variables
The eight data types are called primitive because they are simple and
uncomplicated. Primitive types also serve as the building blocks for more
complex data types, called reference types, which hold memory addresses.
A primitive data type specifies the size and type of variable values, and it
has no additional methods.
Declaring Variables
Declaring Variables
●A data type that identifies the type of data that the variable will
store
●An identifier that is the variable’s name
●An optional assignment operator and assigned value, if you want a
variable to contain an initial value
●An ending semicolon
Declaring Variables
You declare a variable just once in a method, but you might assign new
values to it any number of times. (A compiler error message will be
displayed when there is a conflict between two variables with the same
name.) You can declare multiple variables of the same type in separate
statements. You also can declare two or more variables of the same type in
a single statement by separating the variable declarations with a comma,
as shown in the following statement:
A variable is a named memory location for which the contents can change.
If a named location’s value should not change during the execution of a
program, you can create it to be a named constant. A named constant is
also known as a symbolic constant. A named constant is similar to a
variable in that it has a data type, a name, and a value. A named constant
differs from a variable in several ways:
● A named constant can be assigned a value only once, and then it cannot
be changed later in the program. Usually you initialize a named
constant when you declare it; if you do not initialize the constant at
declaration, it is known as a blank final, and you can assign a value
later. Either way, you must assign a value to a constant before it is
used.
● A named constant can be assigned a value only once, and then it cannot
be changed later in the program. Usually you initialize a named
constant when you declare it; if you do not initialize the constant at
declaration, it is known as a blank final, and you can assign a value
later. Either way, you must assign a value to a constant before it is
used.
The byte data type can store whole numbers from -128 to 127. This can be
used instead of int or other integer types to save memory when you are
certain that the value will be within -128 and 127:
Short
The short data type can store whole numbers from -32768 to 32767:
Int
The int data type can store whole numbers from -2147483648 to
2147483647. In general, and in our tutorial, the int data type is the
preferred data type when we create variables with a numeric value.
Long
Besides assigning true and false, you also can assign a value to a Boolean
variable based on the result of a comparison. Java supports six relational
operators that are used to make comparisons. A relational operator
compares two items; it is sometimes called a comparison operator. The
value of an expression that contains a relational operator is always true or
false.
Using the Boolean Data Type
Learning about Floating-point
Data Types
Float
The float data type can store fractional numbers from 3.4e−038 to
3.4e+038. Note that you should end the value with an "f":
Double
The double data type can store fractional numbers from 1.7e−308 to
1.7e+308. Note that you should end the value with a "d":
Using the char Data Type
Using the char Data Type
You use the char data type to hold any single character. You place constant
character values within single quotation marks because the computer
stores characters and integers differently. For example, the following are
typical character declarations:
char middleInitial = ‘M’;
char gradeInChemistry = ‘A’;
char aStar = ‘*’;
Using the char Data Type
A variable of type char can hold only one character. To store a string of
characters, such as a person’s name, you must use a data structure called a
String. In Java, String is a built-in class that provides you with the means
for storing and manipulating character strings.
Using the char Data Type
An input dialog box asks a question and provides a text field in which the
user can enter a response. You can create an input dialog box using the
showInputDialog() method. Six versions of this method are available, but
the simplest version uses a single argument that is the prompt you want
to display within the dialog box. The showInputDialog() method returns a
String that represents a user’s response; this means that you can assign the
showInputDialog() method to a String variable and the variable will hold
the value that the user enters.
Using Input Dialog Boxes
An input dialog box asks a question and provides a text field in which the
user can enter a response. You can create an input dialog box using the
showInputDialog() method. Six versions of this method are available, but
the simplest version uses a single argument that is the prompt you want
to display within the dialog box. The showInputDialog() method returns a
String that represents a user’s response; this means that you can assign the
showInputDialog() method to a String variable and the variable will hold
the value that the user enters.
Using Input Dialog Boxes
● The message the user will see before entering a value. Usually this
message is a String, but it actually can be any type of object.
● The title to be displayed in the title bar of the input dialog box.
Using Input Dialog Boxes
● The message the user will see before entering a value. Usually this
message is a String, but it actually can be any type of object.
● The title to be displayed in the title bar of the input dialog box.
Using Input Dialog Boxes
● A class field describing the type of dialog box; it can be one of the
following:
○ ERROR_MESSAGE,
○ INFORMATION_MESSAGE,
○ PLAIN_MESSAGE,
○ QUESTION_MESSAGE,
○ WARNING_MESSAGE.
Using Confirm Dialog Boxes
Using Using Confirm Dialog Boxes
Sometimes, the input you want from a user does not have to be typed from
the keyboard. When you present simple options to a user, you can offer
buttons that the user can click to confirm a choice. A confirm dialog box
that displays the options Yes, No, and Cancel can be created using the
showConfirmDialog() method in the JOptionPane class.
Using Using Confirm Dialog Boxes
Four versions of the method are available; the simplest requires a parent
component (which can be null) and the String prompt that is displayed in
the box. The showConfirmDialog() method returns an integer containing
one of three possible values: JOptionPane.YES_OPTION,
JOptionPane.NO_OPTION, or JOption Pane.CANCEL_OPTION.
Using Using Confirm Dialog Boxes
You can also create a confirm dialog box with five arguments, as follows:
●The parent component, which can be null
●The prompt message
●The title to be displayed in the title bar
●An integer that indicates which option button will be shown; it
should be one of the constants YES_NO_CANCEL_OPTION or
YES_NO_OPTION
●An integer that describes the kind of dialog box; it should be one of
the constants ERROR_MESSAGE, INFORMATION_MESSAGE,
PLAIN_MESSAGE, QUESTION_MESSAGE, or
Using Using Confirm Dialog Boxes
Highest
Lowest
Automatic Type Conversion
When two unlike types are used in an expression, the unifying type is the
one that is higher in the list in the previous figure. In other words, when
an operand that is a type lower on the list is combined with a type that is
higher, the lower-type operand is converted to the higher one. For
example, the addition of a double and an int results in a double, and the
subtraction of a long from a float results in a float.
Explicit Type Conversions
Automatic Type Conversion
● You use the char data type to hold any single character. You type
constant character values between single quotation marks and String
constants between double quotation marks. You can store some
characters using an escape sequence, which always begins with a
backslash.
● You can use the Scanner class and the System.in object to accept user
input from the keyboard. Several methods are available to convert
input to usable data, including nextDouble(), nextInt(), and nextLine().