5. Write the name three primitives with description.
HDRAW is useful in making a poster?
Answers
• How the computer interprets the string of bits
depends on the context.
• In Java, we must make the context explicit by
specifying the type of the data
Primitive Data Types
• Java has two categories of data:
• primitive data (e.g., number, character)
• object data (programmer created types)
• There are 8 primitive data types:
byte, short, int, long, float, double,
char, boolean
Primitive data are only single values; they
have no special capabilities
Type Description Example of Literals
int integers (whole
numbers) 42, 60634, -8, 0
double real numbers 0.039, -10.2, 4.2E+72
char single characters 'a', 'B', '&', '6'
boolean logical values true, false5
Numbers
Type Storage Range of Values
byte 8 bits -128 to 127
short 16 bits -32,768 to 32,727
int 32 bits -2,147,483,648 to 2,147,483,647
long 64 bits -9x1018 to 9x1018
float 32 bits ±10-45 to ±1038, 7 significant digits
double 64 bits ±10-324 to ±10308, 15 significant digits6
Variables
• A variable is a name for a location in memory
used to store a data value.
• We use variables to save and restore values or
the results of calculations.
• The programmer has to tell Java what type of
data will be store in the variable’s memory
location. Its type cannot change.
• During the program execution the data saved in
the memory location can change; hence the term
“variable”.Variable Declaration
• Before you can use a variable, you must
declare its type and name.
• You can declare a variable only once in a
method.
• Examples:
int numDimes;
double length;
char courseSection;
boolean done;
String lastName;Declaring Variables
• Declaring a variable instructs the compiler to set
aside a portion of memory large enough to hold data
of that type.
int count;
double length;
count length
• No value has be put in memory yet. That is, the
variable is undefined.Assignment Statements
• An assignment statement stores a value into a
variable's memory location:
<variable> = <expression>
Re-Assigning Variables
• A variable must be declared exactly once.
• A variable can be assigned and re-assigned values
many times after it is declared.
• An expression is anything that result in a value.
• It must have a type. Why?
Example: (2 + 3) * 4
Arithmetic operators:
Operator Meaning Example Result
+ addition 1 + 3 4
- subtraction 12 - 4 8
* multiplication 3 * 4 12
/ division 2.2 / 1.1 2.0
% modulo (remainder) 14 % 4 2Division and Modulo
int a = 40; double x = 40.0;
int b = 6; double y = 6.0;
int c; double z;
c = a / b; 6 c = a % b; 4
z = x / y; 6.66666667 c
Operator Precedence
The operators *
, /, % are evaluated before the
operators +, - because *
, /, % have higher
precedence than +, -.
Example: 2 + 4 * 5
• To change the order use parentheses:
Example: (2 + 4) * 5 evaluates to __Evaluating expressions
• When an expression contains more than one
operator with the same level of precedence, they are
evaluated from left to right.
• 2 + 2 + 3 - 1 is (((2 + 2) + 3) - 1) which is 6
• 2 * 4 % 5 is ((2 * 4) % 5) which is 3
• 2 * 3 - 2 + 7 / 4
6 - 2 + 7 / 4
6 - 2 + 1
4 + 1
5
• Widening conversions convert data to another type
that has the same or more bits of storage. E.g.,
• short to int, long (safe)
• int to long (safe)
• int to float, double (magnitude the same but can lose precision)
Mixing Types
• Conversions are done on one operator at a time in
the order the operators are evaluated.
3 / 2 * 3.0 + 8 / 3 5.0
2.0 * 4 / 5 + 6 / 4.0 3.2