Introduction to PrimitivesOverviewPrimitivesintbyte and shortlongdoublefloatAn aside: approximationsGiving names to numbersVariablesDeclaring variablesGiving values to variablesNumber “width”Widening and narrowingCastsExample castsThe fifth integer typeMixed typesMath methodsMethod parametersThe EndJan 14, 2019Introduction to Primitives2OverviewToday we will discuss:The eight primitive types,especially int and doubleDeclaring the types of variablesOperations on primitivesThe assignment statementHow to print results3PrimitivesPrimitives are the “basic” data valuesThere are eight types of primitives:boolean -- used for true and false valueschar -- used for single characters (letters, etc.)byte, short, int, long -- four different kinds of integer (whole number) valuesfloat, double -- two different kinds of decimal numbers (numbers with a decimal point)4intThe most important integer type is intAn int is a “whole” number (no decimal point)Numbers occupy memory in the computerLarger numeric types require more memory byte: 1 byte short: 2 bytes int: 4 bytes long: 8 bytesAn int can be between about two billion (two thousand million) and negative two billionIf you just write a number, such as 25, Java assumes it is an intHence 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 types5byte and shortA byte can be between -128 and 127A short can be -32768 to 32767Why 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 numberUse byte or short only whenYou know the numbers are all smallThere are millions of numbers to remember6longlong integers are for when two billion isn’t large enough for your needsA long can be as long as about 19 digitsA long occupies twice as much space as an intArithmetic on long values is slowerUse long only when you need really big numbersA long number is written like an int, but with an L suffixExamples: 895000000000L, 25L, 5L, -123LEven larger numbers are available in Java--but they are objects, not primitives7doubleA double represents a “real” numberAlso sometimes called “floating point”These are numbers with a decimal pointIf you just write a real number, such as 1.37, Java assumes it is a doubleHence it is easier to work with double values than with float valuesdoubles 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.000073A double can represent numbers in the range 4.9*10-324 to 1.8*10308 and has 15 or 16 digits of accuracyUse double in preference to float8floatfloat is the other kind of “real,” or “floating point” numberfloat has about 8 digits of accuracyArithmetic with float is not fasterUse 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 suffixExamples: 1.23F, -9F, 6.7E14FA float can represent numbers in the range 1.4*10-45 to 3.4*1038 and has 6 or 7 digits of accuracy9An aside: approximationsIntegers are precise, but real numbers are always approximate (inaccurate)Computers always use the binary system internallyMany numbers that can be expressed precisely in decimal cannot be represented precisely in binaryFor example, the numbers 1.1, 1.2, 1.3, and 1.4 can only be approximated in binaryTwo numbers that look the same may actually be subtly differentNever 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 rule10Giving names to numbersSometimes you know what a number isYou have 10 fingersThere are 24 hours in a dayπ is 3.141592653589793238Numbers written like this are called literalsYou can use literals anyplace in Java that you can use a numberIt’s usually better to use names instead:classSize, myBankBalance, myAge, speedometerReadingA literal used without explanation is called a magic numberExample: 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.14159265358979323811VariablesBefore 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 spaceSo 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 withYou might assign an initial value to your variable, or compute a value, or read a value in; but you have to get one somehow12Declaring variablesYou declare variables like this: int classSize; double myBankBalance;When you declare a variable to be a primitive type, Java automatically finds space for itThe amount of space Java needs to find depends on the type of the variableThink of a variable as a specially shaped “box,” designed to hold a value of a particular typeAn int variable is four bytes long and there’s a special place for the sign bitA float variable is also four bytes long, but the bits are used differently--some are used to tell where the decimal point goes13Giving values to variablesA variable is just a name for some valueYou have to supply the actual value somehowJava tries to prevent you from using a variable that you haven’t given a valueYou can assign values like this: classSize = 57; myBankBalance = 123.01; // no "$"!14Number “width”Numeric types are considered wider or narrower than other numeric typesThis is based partly on number of bytes occupiedAlso based on how large a number it can holdJava doesn’t mind if you assign a narrow value to a wide variable: int n = 3;Java is not happy if you
View Full Document