compsci 6 5.1 primitive types primitive types (base types) built-in data types; native to most...
TRANSCRIPT
![Page 1: CompSci 6 5.1 Primitive Types Primitive Types (base types) Built-in data types; native to most hardware Note: not objects (will use mostly first](https://reader036.vdocument.in/reader036/viewer/2022082613/5697c0091a28abf838cc70f6/html5/thumbnails/1.jpg)
CompSci 6 5.1
Primitive Types Primitive Types (base types)
Built-in data types; native to most hardware Note: not objects (will use mostly first four)
boolean (1bit)int (4 bytes)double (8 bytes)char (2 bytes) Constants/Literals (by example):boolean f = false;
int i = 32769;
double d = 0.333333;
char c = ’x’;
byte (1 byte = 8 bits)short (2 bytes)
long (8 bytes)
float (4 bytes)
byte b = 33;short s = 21;long l = 289L;float = 3.141592F;
![Page 2: CompSci 6 5.1 Primitive Types Primitive Types (base types) Built-in data types; native to most hardware Note: not objects (will use mostly first](https://reader036.vdocument.in/reader036/viewer/2022082613/5697c0091a28abf838cc70f6/html5/thumbnails/2.jpg)
CompSci 6 5.2
Named Constants Probably should name most constants
Should have no “Magic Numbers” in program By convention, use all caps in the identifier
Use the final keyword Keeps you from accidentally changing value;
final boolean CORRECT = true;
final int SQUAD_SIZE = 12;
final double MIN_GPA = 2.5;
final char FAIL = ’F’;
final String AUTHOR = ”Dietolf Ramm”;
Note Constants provided by Java: (from API)
Math.PI, Math.E, Integer.MAX_VALUE
![Page 3: CompSci 6 5.1 Primitive Types Primitive Types (base types) Built-in data types; native to most hardware Note: not objects (will use mostly first](https://reader036.vdocument.in/reader036/viewer/2022082613/5697c0091a28abf838cc70f6/html5/thumbnails/3.jpg)
CompSci 6 5.3
Arithmetic Operators Arithmetic
o +, -, *, /, % (remainder or mod) Increment/Decrement
o e.g., k++, k-- , ++k, --k Logical (results in boolean value)
o <, <=, ==, !=, >=, >o Used only for numbers except == and !=o For boolean only: !, &&, ||
String Concatenationo “I’m “ + 19 + “ years old and live in “ + city
Assignmento variable = expressiono variable op= expressiono ( shorthand for: variable = variable op expression )
![Page 4: CompSci 6 5.1 Primitive Types Primitive Types (base types) Built-in data types; native to most hardware Note: not objects (will use mostly first](https://reader036.vdocument.in/reader036/viewer/2022082613/5697c0091a28abf838cc70f6/html5/thumbnails/4.jpg)
CompSci 6 5.4
Operators Arithmetic
+, -, *, /, % (remainder or mod) Work for both integers and reals Except watch / and % for integers
o What is 13/5 ? 13%5 ? 3/5 ? 3%5 ? Increment/Decrement
e.g., k++, k–- o Written ask++; not k = k++; !!
Can write code like k = 3 * p++ - m / 5; Usually not a good idea: confuses. Use a
separate line for increment of p.
![Page 5: CompSci 6 5.1 Primitive Types Primitive Types (base types) Built-in data types; native to most hardware Note: not objects (will use mostly first](https://reader036.vdocument.in/reader036/viewer/2022082613/5697c0091a28abf838cc70f6/html5/thumbnails/5.jpg)
CompSci 6 5.5
Operator Precedence Determines order of operation
For arithmetic, matches grammar school learningo multiplication and division before addition and
subtractiono what is the value of 4.0 + 5.0 / 9.0 * 27.0 ?o (what is the value for the integer version?)
Parentheses override precedence rules (and don’t do harm when not needed)
For equal precedence (e.g., * and /) work strictly left to right
except for assignment and prefix operations which work right to left
Some of the operators, grouped from high to low:++ --, * / %, + -, < <= > >=, == !=, &&, ||, = op=
![Page 6: CompSci 6 5.1 Primitive Types Primitive Types (base types) Built-in data types; native to most hardware Note: not objects (will use mostly first](https://reader036.vdocument.in/reader036/viewer/2022082613/5697c0091a28abf838cc70f6/html5/thumbnails/6.jpg)
CompSci 6 5.6
Operators Combining Assignment and Arithmetic
variable op= expression ( shorthand for: variable = variable op expression )
Thus the following lines contain equivalent statements
k = k – 1; k -= 1; k--;
q = q * r; q *= r;
s = s / 2; s /= 2;
![Page 7: CompSci 6 5.1 Primitive Types Primitive Types (base types) Built-in data types; native to most hardware Note: not objects (will use mostly first](https://reader036.vdocument.in/reader036/viewer/2022082613/5697c0091a28abf838cc70f6/html5/thumbnails/7.jpg)
CompSci 6 5.7
Assignment (Familiar by now)
x = 13.3;
k = k + 7;
area = 2.0 * Math.PI * radius * radius; Vocalize as “gets” or “becomes” Don’t use “equals”: Not Equality
Casting: (type)int k = 3; double t;
t = k; // OK, No information lostint m; double s = 3.5;
m = s; // ILLEGAL, information lost (accidentally?)
m = (int) s; // OK, information lost – intent shown
![Page 8: CompSci 6 5.1 Primitive Types Primitive Types (base types) Built-in data types; native to most hardware Note: not objects (will use mostly first](https://reader036.vdocument.in/reader036/viewer/2022082613/5697c0091a28abf838cc70f6/html5/thumbnails/8.jpg)
CompSci 6 5.8
Casting Implicit Casting
Types on two sides of operator differ (int and double) or (double and int)
Promotes calculation double Can cascade down the expression Compare the following two lines;tempC = 5 / 9 * (tempF + 40) – 40;
tempF = 9.0 / 5 * (tempC + 40) – 40; Try for 212OF or 100OC
![Page 9: CompSci 6 5.1 Primitive Types Primitive Types (base types) Built-in data types; native to most hardware Note: not objects (will use mostly first](https://reader036.vdocument.in/reader036/viewer/2022082613/5697c0091a28abf838cc70f6/html5/thumbnails/9.jpg)
CompSci 6 5.9
The Math class Contains useful static methods
static means the method does not operate on an object
Use class name (Math) rather than object name when using the dot operator.
Contains common math functions Look up Math class in Java API Use of some common methodsdouble x = Math.sqrt(y) * Math.cos(theta);
long j = Math.round(3.8 * Math.pow(h, n));