













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!














L E C T U R E 0 4
New Mexico Tech: CSE/IT 111
New Mexico Tech: CSE/IT 111
New Mexico Tech: CSE/IT 111
Once a person has understood
the way variables are used in programming,
he has understood the quintessence of programming.
--E.W. Dijkstra (1930-2002),
Notes on Structured Programming
(1969)
New Mexico Tech: CSE/IT 111
eggsPerBasket = 6;
01/28/
New Mexico Tech: CSE/IT 111 y
Data type
A specification for^ Ù
a
set of possible values
, stored in
memory in the same way, and Ù
the
operations
defined on these values
y
Class type
A data type for objects of a class^ Ù
Name of the class or interface used inthe object’s declaration
y
Primitive type
A data type for a basic value, such as asingle number or a single character, thatdoes not have smaller parts
y
Integer
A whole number, such as
5
or
Common data type:
int
y
Floating-point number
A number with a fractional part, such as 9.
or
Common data type:
double
y
Character
A single character (letters, digits, orpunctuation) that appears on thecomputer keyboard, such as
‘a’
or
‘H’
Ù
Enclose in single quotes Ù
Case sensitive
Data type:
char
y
Boolean
Either
true
or
false
Common data type:
boolean
01/28/
TypeName New Mexico Tech: CSE/IT 111
Kind of Value
MemoryUsed
Range of Values
byte
Integer
1 byte
to
127
short
Integer
2 bytes
-32,
to
32,
int
Integer
4 bytes
-2,147,483,
to
2,147,483,
long
Integer
8 bytes
-9,223,372,036,854,775,
to
9,223,372,036,854,775,
float
Floating-point
4 bytes
±3.40282347×
to
±1.40239846×
double
Floating-point
8 bytes
±1.79769313486231570×
to
±4.94065645841246544×
char
Single character(Unicode)
2 bytes
All Unicode values from
0
to
65,
boolean
1 bit
true
or
false
y New Mexico Tech: CSE/IT 111
Keyword
Ù
y
Some words have predefined meaning but are notkeywords
y New Mexico Tech: CSE/IT 111
Assignment statement
A statement in a program that assigns a value to a variable
Variable
=
Expression
;
answer
=
42;
y
Assignment operator
The operator in an assignment statement that separates a variable from theexpression whose value is assigned to the variable^ Ù
Java: equal sign
=
y
Expression
A combination of operands and operators that represent a computation^ Ù
Can be another variable, a number, or a more complicated expression made using arithmetic operators
to combine variables and numbers
firstInitial
=
‘B’;
score
= numberOfCards
+
handicap;
count
= count
+
10;
import New Mexico Tech: CSE/IT 111
java.util.Scanner;
public
class
EggBasket
{
public
static
void
main(String[]
args)
{
int
numberOfBaskets,
eggsPerBasket,
totalEggs;
Scanner
keyboard
= new
Scanner(System.in);
System.out.println("Enter
the
number
of
eggs
in
each
basket:");
eggsPerBasket
=
keyboard.nextInt();
System.out.println("Enter
the
number
of
baskets:");
numberOfBaskets
=
keyboard.nextInt();
… … …
}
}
Gets the
Scanner
class from the package
(library)
java.util
Sets up things so theprogram can acceptkeyboard input
Reads one whole numberfrom the keyboard
import New Mexico Tech: CSE/IT 111
java.util.Scanner;
public
class
EggBasket
{
public
static
void
main(String[]
args)
{
int
numberOfBaskets,
eggsPerBasket,
totalEggs;
… … … System.out.println("If
you
have");
System.out.println(eggsPerBasket
+
"
eggs
per
basket
and");
System.out.println(numberOfBaskets
+
"
baskets,
then");
System.out.println("the
total
number
of
eggs
is
" +
totalEggs);
… … …
}}
JavaClass
Object
Method
Arguments
Concatenate (not addition)
New Mexico Tech: CSE/IT 111
y
Integers are exactquantities
y
Floating-point numbersare only approximations
y
Memory only has room fora limited number of digits
y New Mexico Tech: CSE/IT 111
Named constants
Mechanism that allows you to define a variable, initialize it, and fix the value so itcannot be changed^ Ù
Gives a name to a constant Ù
public
: no restrictions on where you can use the name
Ù
final
: the program is not allowed to change the value
Syntax^ Ù
Declaration must occur within the class definition but outside of any methoddefinitions, including
main
method
public
static
final
Type Variable
=
Constant
;
Conventions^ Ù
Name of constant using all uppercase letters, with an underscore symbol _ betweenwords
Examples
public
static
final
int
MAX_STRIKES
=
3;
public
static
final
double
PI
= 3.14159;
y New Mexico Tech: CSE/IT 111
Type cast
An operation that temporarily changes the data type of a value from its normal typeto some other type^ Ù
Does not change the value of the source variable
Syntax
(Type_Name) Expression
Examples
double
money
=
7.83;
int
dollars
=
(int)money;
double
money
=
(double)dollars;
//same as “double money = dollars;” due to assignment compatibility
y
Truncate
To discard the part after the decimal point in a number NOT ROUNDING!
Assignment of integers has no connection to themeaning of the characters
Characters are encoded using the Unicode numberingsystem (same as the ASCII system for English)