Chapter 2 - Primitive Data Types
Chapter 2 - Primitive Data Types
[email protected]
Mantra:
Coding is fun!
Chapter 2 Primitive Data Types and
Operations
• Introduce Programming with an Example
• Identifiers, Variables, and Constants
• Primitive Data Types
• byte, short, int, long, float, double, char, boolean
• Expressions
• Operators, Precedence, Associativity, Operand Evaluation Order: +
+, --, *, /, %, +=, -=, *=, /=, %=, ^, &, |, +, -,
• Getting Input from Input Dialog Boxes
• Case Study
• Style and Documentation Guidelines
• Syntax Errors, Runtime Errors, and Logic Errors
Introducing Programming with an
Example
Example 2.1 Computing the Area of
a Circle
This program computes the area of
the circle.
Identifiers
int x; // Declare x to be an
// integer variable;
double radius; // Declare radius to
// be a double variable;
char a; // Declare a to be a
// character variable;
Assignment Statements
x = 1; // Assign 1 to x;
radius = 1.0; // Assign 1.0 to radius;
a = 'A'; // Assign 'A' to a;
Declaring and Initializing
in One Step
• int x = 1;
• double d = 1.4;
• float f = 1.4;
Is this statement correct?
Constants
byte 8 bits
short 16 bits
int 32 bits
long 64 bits
float 32 bits
double 64 bits
Operators
+, -, *, /, and %
System.out.println(1.0 - 0.9);
int i = 34;
long l = 1000000;
double d = 5.0;
Integer Literals
An integer literal can be assigned to an integer
variable as long as it can fit into the variable. A
compilation error would occur if the literal were
too large for the variable to hold. For example,
the statement byte b = 1000 would cause a
compilation error, because 1000 cannot be stored in
a variable of the byte type.
is translated to
(3+4*x)/5 – 10*(y-5)*(a+b+c)/x +
9*(4/x + (9+x)/y)
Shortcut Assignment Operators
suffix
x++; // Same as x = x + 1;
prefix
++x; // Same as x = x + 1;
suffix
x––; // Same as x = x - 1;
prefix
––x; // Same as x = x - 1;
Increment and
Decrement Operators, cont.
byte i = 100;
long k = i*3+4;
double d = i*3.1+k/2;
int x = k; //(Wrong)
long k = x; //(fine,implicit casting)
Type Casting
• double
• float
• long
• int
• short
• byte
Type Casting, cont.
Implicit casting
double d = 3; (type widening)
Explicit casting
int i = (int)3.0; (type narrowing)
Special characters
char tab = ‘\t’;
Unicode Format
Operator Name
< less than
<= less than or equal to
> greater than
>= greater than or equal to
== equal to
!= not equal to
Boolean Operators
Operator Name
! not
&& and
|| or
^ exclusive or
Truth Table for Operator !
Operand !Operand
true false
false true
Truth Table for Operator &&
How to evaluate
3 + 4 * 4 > 5 * (4 + 3) - ++i
Operator Precedence
• var++, var-- • ^ (Exclusive OR)
double doubleValue
=Double.parseDouble(doubleString);
• Appropriate Comments
• Naming Conventions
• Proper Indentation and Spacing
Lines
• Block Styles
Appropriate Comments
• Class names:
• Capitalize the first letter of each word in the name.
For example, the class name ComputeArea.
• Constants:
• Capitalize all letters in constants. For example, the
constant PI.
Proper Indentation and Spacing
• Indentation
• Indent two spaces.
• Spacing
• Use blank line to separate segments of the
code.
Block Styles
End-of-line
style
public class Test {
public static void main(String[] args) {
System.out.println("Block Styles");
}
}
Programming Errors
• Syntax Errors
• Detected by the compiler
• Runtime Errors
• Causes the program to abort
• Logic Errors
• Produces incorrect result
Compilation Errors
System.exit(0);
}
}
Practice
Void
[modifiers] void MethodName ([type arg], [type2 arg2], [type3
arg3], …) {
// Statements
}
Function
[modifiers] return_type MethodName ([type arg], [type2 arg2],
[type3 arg3], …) {
// Statements…
// return
return xxx; //where xxx is same type as return_type
}
public static void main (String [] args) {
} // End of method
double answer = x + y;
return answer;
}