Presentation is loading. Please wait.

Presentation is loading. Please wait.

22-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.

Similar presentations


Presentation on theme: "22-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables."— Presentation transcript:

1 22-Jun-15 Introduction to Primitives

2 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables Operations on primitives The assignment statement How to print results

3 3 Primitives Primitives are the “basic” data values There are eight types of primitives: boolean -- used for true and false values char -- used for single characters (letters, etc.) byte, short, int, long -- four different kinds of integer (whole number) values float, double -- two different kinds of decimal numbers (numbers with a decimal point)

4 4 int The most important integer type is int An int is a “whole” number (no decimal point) Numbers occupy memory in the computer Larger numeric types require more memory byte : 1 byte short : 2 bytes int : 4 bytes long : 8 bytes An int can be between about two billion (two thousand million) and negative two billion If you just write a number, such as 25, Java assumes it is an int Hence it is easier to work with int values than with the other integer types ( byte, short, and long ) Use int in preference to other integer types

5 5 byte and short A byte can be between -128 and 127 A short can be -32768 to 32767 Why these numbers? These are “round numbers” in binary; for example, 0111 1111 1111 1111 is binary for 32767 1000 0000 0000 0000 is binary for -32768 The first bit is the sign bit: a 1 means it’s a negative number Use byte or short only when You know the numbers are all small There are millions of numbers to remember

6 6 long long integers are for when two billion isn’t large enough for your needs A long can be as long as about 19 digits A long occupies twice as much space as an int Arithmetic on long values is slower Use long only when you need really big numbers A long number is written like an int, but with an L suffix Examples: 895000000000L, 25L, 5L, - 123L Even larger numbers are available in Java-- but they are objects, not primitives

7 7 double A double represents a “real” number Also sometimes called “floating point” These are numbers with a decimal point If you just write a real number, such as 1.37, Java assumes it is a double Hence it is easier to work with double values than with float values double s can also be written using scientific notation, where E stands for “times ten to the power” Examples: 7.3E5 = 730000.0, 7.3E-5 = 0.000073 A double can represent numbers in the range 4.9*10 -324 to 1.8*10 308 and has 15 or 16 digits of accuracy Use double in preference to float

8 8 float float is the other kind of “real,” or “floating point” number float has about 8 digits of accuracy Arithmetic with float is not faster Use float only to save space when there are millions of numbers involved A float constant is written like a double constant, but with an F suffix Examples: 1.23F, -9F, 6.7E14F A float can represent numbers in the range 1.4*10 -45 to 3.4*10 38 and has 6 or 7 digits of accuracy

9 9 An aside: approximations Integers are precise, but real numbers are always approximate (inaccurate) Computers always use the binary system internally Many numbers that can be expressed precisely in decimal cannot be represented precisely in binary For example, the numbers 1.1, 1.2, 1.3, and 1.4 can only be approximated in binary Two numbers that look the same may actually be subtly different Never test floating point numbers for equality! Only test for larger or smaller, or for “not larger” or “not smaller” This is not a Java rule—it’s a programming rule

10 10 Giving names to numbers Sometimes you know what a number is You have 10 fingers There are 24 hours in a day π is 3.141592653589793238 Numbers written like this are called literals You can use literals anyplace in Java that you can use a number It’s usually better to use names instead: classSize, myBankBalance, myAge, speedometerReading A literal used without explanation is called a magic number Example: e = 8.987551787E16 * m; Better: energy = speedOfLightSquared * mass; Better yet: final double C = 2.99792458E8; e = m * C * C; Style rule: Avoid magic numbers! Sometimes names are simply more convenient, for example, Math.PI instead of 3.141592653589793238

11 11 Variables Before you use a variable, you must declare it (tell Java what type it is: int, double, char,...) There are two reasons for this: Different types require different amounts of space So Java can prevent you from doing something meaningless (adding 5 and true, or multiplying two dates together) Before you use a variable, you must also define it (tell Java what value it has) It makes no sense to print out your bankBalance, or to add 100.00 to your bankBalance, if you don’t have a meaningful, well-defined initial value for bankBalance to start with You might assign an initial value to your variable, or compute a value, or read a value in; but you have to get one somehow

12 12 Declaring variables You declare variables like this: int classSize; double myBankBalance; When you declare a variable to be a primitive type, Java automatically finds space for it The amount of space Java needs to find depends on the type of the variable Think of a variable as a specially shaped “box,” designed to hold a value of a particular type An int variable is four bytes long and there’s a special place for the sign bit A float variable is also four bytes long, but the bits are used differently--some are used to tell where the decimal point goes

13 13 Giving values to variables A variable is just a name for some value You have to supply the actual value somehow Java tries to prevent you from using a variable that you haven’t given a value You can assign values like this: classSize = 57; myBankBalance = 123.01; // no "$"!

14 14 Number “width” Numeric types are considered wider or narrower than other numeric types This is based partly on number of bytes occupied Also based on how large a number it can hold Java doesn’t mind if you assign a narrow value to a wide variable: int n = 3; Java is not happy if you assign a wide value to a narrow variable: byte b = 7139946; // illegal

15 15 Widening and narrowing You can always widen (assign a narrower type to a wider type): double wide; int narrow; wide = narrow; But if you want to narrow (assign a wider type to a narrower type), you have to cast it: narrow = (int)wide; double float long int short byte

16 16 Casts You can convert (cast) one numeric type to another When you widen, no explicit cast is necessary But it doesn’t hurt When you narrow, an explicit cast is required This requirement is made to help avoid accidental loss of precision Casting tells Java that the value in the wider type will fit in the narrower type Java checks to make sure that the cast works, and gives you an error if it didn’t

17 17 Example casts short s = 0; int i = 0; double d = 0.0; d = i; // legal d = s; // legal i = s; //legal i = d; // illegal s = d; // illegal s = i; // illegal i = (int) d; // legal s = (short) d; // legal s = (short) i; // legal d = 3.7E20; i = 50000; // The following give // runtime errors: s = (short) i; i = (int) d;

18 18 The fifth integer type The primitive type char refers to a single, two-byte Unicode character There is no good reason this should be a numeric type......but characters were numbers in C You can use characters in arithmetic (they will automatically be converted to int ) char ch = 'A'; char ch2 = (char) (ch + 1); // cast result back to char System.out.println(ch + " " + ch2 + " " + (ch + 1)); A B 66 To assign a char to a byte, or a byte to a char, you must use a cast

19 19 Mixed types If you mix numeric types, the narrower type is automatically promoted (widened) to the wider type int narrow = 5; double wide; double anotherWide = wide + narrow; Integer division is when you divide one integer type by another The fractional part is discarded Example: narrow = 19 / 5; // result is 3 Example: narrow = -19 / 5; // result is -3

20 20 Math methods Converting a double to an int just discards the fractional part: (int)17.93 is 17 (int) –17.93 is -17 double Math.floor(double) Given a double, returns (as a double ) the largest integral value not greater than the argument Math.floor(17.93) returns 17.0 Math.floor(-17.93) returns –18.0 double Math.ceil(double) Given a double, returns (as a double ) the smallest integral value not smaller than the argument Math.ceil(17.93) returns 18.0 Math.ceil(-17.93) returns –17.0

21 21 Method parameters When you send a message to an object with a numeric parameter, and the object needs to promote the parameter in order to use a method, it will do so Example: double twice(double n) { return 2.0 * n; } twice(5) returns 10.0 This promotion will only occur if necessary Example 2: double half(double n) { return n / 2; } int half(int n) { return n / 2; } half(25) returns 12

22 22 The End “I think there is a world market for maybe five computers.” --Thomas Watson, chairman of IBM, 1943


Download ppt "22-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables."

Similar presentations


Ads by Google