

































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The Integer class has 2 special "int" values: Integer.MIN_VALUE is the minimum value of "int" type. Integer.MAX_VALUE is the maximum value of "int" type.
Typology: Study notes
1 / 41
This page cannot be seen from the preview
Don't miss anything!


































James Brucker
A primitive data type has only a value, such as a number. Primitive types are things the CPU can directly manipulate. Example: 2 + 3 (cpu can add int) Java has 8 primitive types, such as: boolean char int long double
Arithmetic (result is int) a + b a - b a * b a / b a % b a modulo b Comparison (result boolean) a < b a > b a <= b a >= b a == b a != b Operations that shift bits a <
The Integer class has 2 special "int" values: Integer.MIN_VALUE is the minimum value of "int" type. Integer.MAX_VALUE is the maximum value of "int" type.
Data Type Size in Memory Range of Values boolean 1 byte true false char 2 bytes 0 (null) - \t'uFFFF (Unicode) byte 1 byte -128 to 127 short 2 bytes -32,768 to 32, int int 4 bytes -2,147,483,648 to 2,147,483, long 8 bytes -9,223,372,036,854,775,808L 9,223,372,036,854,775,807L float 4 bytes ±3.402823E+ double 8 bytes ±1.797693134623157E+
Java has a class named Double -- not same as primitive type double. Double (class) has some special values: Double.POSITIVE_INFINITY Double.NEGATIVE_INFINITY Double.NaN Double.MAX_VALUE = 1.7976931348523E+ Double.MIN_VALUE = 4.9E- and some useful static methods: Double.parseDouble("2.14") // returns primitive 2. Double.toString(2.14) // returns String "2.14"
______________ 6010541234 (in Java: 6010541234L) ______________ 3.14159 ( what is this? ) ______________ 3E+ ______________ 3000.0F ______________ true ______________ '2' ______________ "2" ______________ 'ด' ______________ 3 == 4
If your code contains: 2+ then Java sees that you are adding int + int and produces an int result ( 5 ). But, if your code contains: 2+3. it means to add "int" + "double" values. In this case, Java will convert 2 to a double (2.0) and add 2.0+3.0. The result is a double. Type conversion may also occur when you call a method. For example: Math.sqrt(2) The sqrt method requires a double parameter, so Java "promotes" 2 (int) to 2.0 (double).
If you do arithmetic on different data types, Java "promotes" one argument to the type with widest range. Example Promotion Result 2 + 4L 2 -> (long)2L 6L (long) 2 * 4.0 2 -> (double)2.0 6.0 (double) 2F + 3 3 -> (float)3F 5.0F (float) 2.0 * 3 3 -> (double)3.0 5.0 (double) Weird: 'a'+1 'a' -> int (97) 98 double float long int short,char byte
This is done to prevent accidental errors. A classic error in C programming is: int n = 1; if (n = 2) printf("its true!"); // set n=2, result is true! should be: if (n == 2)... ;
Here are some common errors. What is the mistake? How to correct it? // Compute typing speed in words/minute int wordsTyped = 38; // number of words typed int time = 45; // time in seconds double speed = wordsTyped/time * 60.0; // speed = 0 // The midterm exam has a maximum of 90 points. // "Normalize" the score to be 0-100 (e.g. 90 -> 100%). int midtermScore = 85; double score = 100.0 * (midtermScore / 90);