Data Types
Data Types
Data Types
Constants
Variables
What is a Constant?
456—a literal numerical constant
System.out.println(456); // Java
Console.writeline(456); // Visual C#
C:\Java>_
This program would produce the
same output
Public class NumbersPrintln
{
public static void main(String[] args)
{
int billingDate = 5;
System.out.println(“Bills are sent on the “ +
billingDate + “th\nNext bill: October “ +
billingDate);
}
}
Simple Arithmetic Operators
• * / % (multiplication, division,
modulus)
• + - (addition, subtraction—on a lower
level of the precedence hierarchy)
• int result = 2 + 3 * 4;
• Is result 14 or 20??
• int result = (2 + 3) * 4
Binary Operators
The simple arithmetic operators are also
called binary operators because they have
two operands exactly
Never three
Never one
Using the Boolean data type
•Boolean variables can hold only one of
two values—true or false
Boolean isItPayday = false;
Boolean areYouBroke = true;
Comparison operators
The result is boolean, always
< less than
> greater than
== equal to
<= less than or equal to
>= greater than or equal to
!= not equal to
Boolean examples
boolean is SixBigger = (6 > 5);
// value stored in is SixBigger is true
Boolean is SevenSmaller = (7 <= 4);
// value stored in is SevenSmaller is false
Data formats
The character format—uses an assigned
decimal value
The integer format
The floating point format—consists of an
exponent part and a mantissa part—for
example the 4-byte floating point word
might have a 1-byte exponent and a 3-
byte mantissa.
What happens when you try to do
arithmetic with different data types?
The lower-level data type is converted to
the higher-level data type before the
binary operation is performed
1. double
2. float
3. long
4. int
Example
int hoursWorked = 37;
Double payRate = 6.73;
Double grossPay = hoursWorked * payRate;
Mean???