Presentation is loading. Please wait.

Presentation is loading. Please wait.

Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2.

Similar presentations


Presentation on theme: "Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2."— Presentation transcript:

1 Primitive Data Types

2 int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2 32 ) and 2,147,483,647 (2 31 - 1) (inclusive) Remember that dividing two ints will truncate the result, meaning you’ll lose anything after the decimal point (also known as rounding down)  E.g. 7/2 = 3 Stored in 32 bits

3 long Similar to an int, just has a larger range of storage Stores an integer value (whole number) between -9,223,372,036,854,775,808 (-2 63 ) and 9,223,372,036,854,775,807 (2 63 - 1) (inclusive) Same rules with division as an int To create a long literal (a number that you physically type into the source code), type the number and put an L at the end  E.g. 9128 is an int, 9128L is a long  This is important if your literal won’t fit in an int, otherwise you’ll get a compile error for having an int too large Stored in 64 bits

4 short Similar to an int and a long, just the other way around – has a smaller range of storage than an int. Stores an integer value (whole number) between -32,768 (-2 15 ) and 32,767 (2 15 - 1) (inclusive) Same rules with division as an int Stored in 16 bits Used mostly to save memory in some cases We will not be using short very much, if at all, in this class. You should know that it exists and that it’s a 16-bit integer type, but unless you have a very good reason, it shouldn’t be used in programs for this class.

5 byte Similar to an short, just even more extreme Stores an integer value (whole number) between -128 (-2 7 ) and 127 (2 7 - 1) (inclusive) Same rules with division as an int Stored in 8 bits Also used mostly to save memory in very special cases We will not be using byte very much, if at all, in this class. You should know that it exists and that it’s an 8-bit integer type, but unless you have a very good reason, it shouldn’t be used in programs for this class.

6 double I’ve briefly mentioned doubles in class, but here are some details on them Doubles are called a “floating point” type. Specifically, a double is a “double- precision floating-point value” Doubles store decimal values  E.g. 4.3, 8.27, -91.5, 8.0, 0.45, -7.2 They are not perfectly precise: doubles have extremely high max values, but their precision decreases as the value approaches the max value. For example, a double can’t store 0.1 exactly. This is a limitation in the way floating point values work – we will go over the specifics of how they work in more detail later in this class. Stored in 64 bits

7 double (cont.) Doubles allow you to perform addition, subtraction, multiplication, or division on decimal numbers Division works as you’d expect: 7.0 / 2.0 = 3.5 If either of the values in a division operation are doubles, the division will use double division (e.g. 7.0 / 2.0 = 3.5, 7 / 2.0 = 3.5, 7.0 / 2 = 3.5, 7 / 2 = 3) Because doubles aren’t perfectly precise, there are some things you can’t do with them  Never use doubles for currency or anything else that requires reliable precision and accuracy  Comparing doubles for equality does not always work the way one would expect

8 double Equality Doubles can be compared for less than or greater than in the same way as an int ( ), but equality doesn’t always work as expected Take, for instance, the code to the right: One may expect this to print out “True”, 0.1, and 0.1 In reality, it prints “False”, 0.09999999999999998, 0.1 This is because 0.63 – 0.53 isn’t exactly 0.1 when dealing with doubles Type this code in yourself and run it!

9 double Equality (cont.) So how do we compare doubles for equality? We use something known as an epsilon! In programming, an epsilon is a value small enough such that we consider two doubles the same if they differ by no more than the epsilon The value of an epsilon depends on context: since doubles’ precision changes as the values get higher or lower, different epsilons may be needed for different contexts We’re going to use a function from the math library known as Math.abs  This function takes a double and returns the absolute value of that double  In other words, if the value is 0 or positive, it returns the value. If the value is negative, it will return -1 * value.  E.g. Math.abs(42) = 42, Math.abs(-10) = 10, Math.abs(0) = 0, Math.abs(-5.4) = 5.4, etc.

10 double Equality (cont.) So to compare two doubles for equality, we check to see if the absolute value of the difference of the doubles is less than our epsilon.  if(Math.abs(value1 – value2) < eps) This will be true if value1 is up to eps greater than value2, or if value2 is up to eps greater than value1. Since we’ve said that we consider value1 and value2 the same if they differ by less than eps, this means we consider them essentially equal. We can represent doubles in scientific notation using the letter “e”  E.g. 3.6e-3 = 0.0036, 5e2 = 500, 1e3 = 1000, 1e-5 = 0.00001 As I mentioned earlier, epsilon values vary wildly based on context, but a good starting point for the types of numbers we’re likely to be working with is around 1e-12

11 float Floats are to doubles as shorts are to ints Floats are also known as “single-precision floating-point value” and are less precise than doubles They are stored using 32 bits We will not be using float very much, if at all, in this class. You should know that it exists and that it’s a 32-bit floating-point type, but unless you have a very good reason, it shouldn’t be used in programs for this class – double should almost always be preferred.

12 boolean A boolean stores exactly one of two possible values: true or false You’ve been using booleans for awhile even if you didn’t know it – all if statements operate on Boolean values We will discuss booleans in more depth after Test 1

13 char A char stores a single 16-bit Unicode character Examples: ‘a’, ‘0’, ‘Z’, ‘*’ Strings are built up of chars We will discuss chars in more depth after Test 1


Download ppt "Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2."

Similar presentations


Ads by Google