Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit 3: Java Data Types Kirk Scott. 3.1 Data Types, Variable Declaration and Initialization 3.2 Assignment and the Relationship Between Numeric Types.

Similar presentations


Presentation on theme: "Unit 3: Java Data Types Kirk Scott. 3.1 Data Types, Variable Declaration and Initialization 3.2 Assignment and the Relationship Between Numeric Types."— Presentation transcript:

1 Unit 3: Java Data Types Kirk Scott

2 3.1 Data Types, Variable Declaration and Initialization 3.2 Assignment and the Relationship Between Numeric Types 3.3 Assignment and Simple Arithmetic 3.4 The Math Class 3.5 The String Class 3.6 Characters and Unicode

3 3.1 Data Types, Variable Declaration and Initialization

4 These are some of the simple numeric data types in Java: intfor integers longfor large integers floatfor single precision floating point values doublefor double precision floating point values

5 There are other numeric types, but none that are needed immediately. For most purposes it is sufficient to be able to use int and double. Learning about long and float gives a more complete understanding of Java.

6 When actually writing numeric values in a program no commas can be used. Any value shown with a decimal point, such as 3.0, will be treated as a floating point value, not an integral value. Exponential notation also exists for handling very large or very small values, but it will not be covered in these notes.

7 The type “double” illustrates the pitfalls of case sensitivity in Java. There is also a system supplied class “Double”. If you mistakenly use a capital letter when giving the variable type in a declaration you will have problems in your code which will lead to mysterious errors. It is extremely difficult to locate the source of the problem when it is simply the difference between a capital and a small “d” in one line of code.

8 Variables are containers for values. In effect, when a variable is declared, a certain amount of space is set aside in the computer’s memory in order to store a value of that type. The following rules and recommendations apply to variables:

9 1. They have to be typed. 2. Ideally, they should be initialized to some concrete value. 3. They should be given a descriptive name. 4. Their names have to follow these rules: – A. They may contain letters, digits, the $ sign, and the underscore (_). – B. They can’t start with a digit. – C. No variable can have the same name as a Java keyword. – D. They are case sensitive. – E. They cannot have blank spaces in them.

10 Rule 4.c is a little unfair for someone just learning Java. It is not possible to know all of the keywords in advance. These notes use the following approach to avoiding keywords or supplied class names: The names of many things written by the author start with “my”. “double” is a keyword, for example, but “myDouble” is not. Therefore “myDouble” is an acceptable name for a variable.

11 Recall that it is conventional for class names to start with a capital letter. Syntactically, variable names are allowed to start with a capital letter, but this should not be done. This way there is no chance of confusing variables and classes. In the previous unit the idea of an object reference was introduced.

12 Although not a simple data type, it should be noted that the declaration of a type and the assignment of a value mean that the object reference functions as a variable. It is especially important to use naming conventions that allow you to distinguish between classes and instances of those classes. Just like variables, the names of object references should also not be capitalized.

13 To make a variable name descriptive, quite often it is desirable to use compound words. Even though this requires more typing, the code is easier to understand and shorter explanatory comments are possible if the naming conventions are clear. Syntactically permissible compounds might take the following forms: payRate, pay_rate, payrate, etc. The dash is not allowed in variable names because the compiler would interpret it as a minus sign or subtraction.

14 It is good practice to declare or declare and initialize variables at the beginning of the block of code where they are used. Some examples follow. The declarations that include initialization are preliminary examples of how to assign values to variables. double pay_rate; double tax_rate =.05; int days_in_a_week; int weeks_in_a_year = 52; int hours_in_a_day, minutes_in_an_hour;

15 As noted above, the declaration of a variable sets aside memory space for a value. If the variable is not expressly initialized, depending on the situation, the variable may be given a default value by the system or it may take on whatever value is in that memory space when the program is in use.

16 Express initialization is a good habit since you don’t want calculations to depend on values that result from random system conditions. The compiler will try to warn you about variables that have not had values assigned to them. However, to avoid errors later, it can be helpful to initialize variables yourself to suitable default values.

17 It is possible to declare constants. This is the syntax for initializing a variable to a given value that cannot be changed later on in the program: final int WEEKS_IN_A_YEAR = 52;

18 Unless you are traveling from planet to planet, the number of weeks in a year does not change. With a declaration of “final”, after the first assignment, any subsequent assignment to the variable will result in a compiler error. It is customary to use all capital letters when declaring constants. This is one exception to the rule that variable names should not begin with a capital letter.

19 3.2 Assignment and the Relationship Between Numeric Types

20 Assuming that a variable has been declared, it can be assigned a value. In a typical program, the sequence might be as follows: int myInteger;/* declaration */ … myInteger = 7;/* assignment */

21 The Java language uses the single “=” sign as the assignment operator. The value that occurs on the right hand side of the operator is stored in the memory location designated by the variable name on the left hand side.

22 A problem arises when the value on the right is not of the same type as the variable on the left. In some cases the system automatically converts the value on the right to the type of the variable on the left. In other cases the assignment is syntactically invalid.

23 If the data type of the value on the right cannot contain more information than the data type on the left, then the assignment is allowed. Automatic conversion will occur. However, if the data type on the right can contain more information than the one on the left, such an automatic conversion could lead to the loss of information. This is not automatically allowed.

24 The following rules apply: An integer can be stored in a long. A float can be stored in a double. An integer or a long can be stored in either a float or a double. A long can’t be stored in an integer. A double can’t be stored in a float. Neither a float nor a double can be stored in either an integer or a long because any information to the right of the decimal place would be lost.

25 In general, the rules can be summarized as follows: You can store the value of a smaller container in a bigger container; but you can't store the value of a bigger container in a smaller container.

26 Given these type declarations: double taxRate =.05; int weeksInaYear = 52; Here are two simple examples of what is and is not allowed: taxRate = weeksInaYear;/* valid */ weeksInaYear = taxRate;/* not valid */

27 The invalid statement is now modified with syntax that makes it valid. This new syntax is called casting and it explicitly causes the conversion of one type to another: weeksInaYear = (int) taxRate;

28 The value on the right is cast to the type on the left by putting the desired type in parentheses in front of the quantity on the right. This is required of the programmer because it functions like a contract. When casting, the programmer is taking responsibility for anything that might be lost due to the fact that the type on the left cannot hold all of the information on the right.

29 When going from a floating point type to an integer type, casting results in truncation. Rounding does not occur, and all data to the right of the decimal point is lost.

30 Here is an illustration: double taxRate =.05; int weeksInaYear = 52; weeksInaYear = (int) taxRate; Everything to the right of the decimal place in taxRate is lost and the end result is that weeksInaYear contains the integer value 0.

31 3.3 Assignment and Simple Arithmetic

32 One way of understanding variables is that they are containers which can be used more than once and can hold different values at different times during program execution. Variables are commonly used to store the results of arithmetic operations and an assignment statement is the place for doing arithmetic operations in a program.

33 The simple arithmetic operators are +, -, *, and /, with () for grouping. Unless the order of operations is changed by parentheses, multiplication and division are performed before addition and subtraction. All else being equal, operations are performed in order from left to right.

34 Arithmetic may include both variables and numeric values. Given such declarations as these: int myInteger1 = 10; int myInteger2 = 20; int myInteger3 = 30; double myDouble1 = 10.0; double myDouble2 = 20.0; double myDouble3 = 30.0; The following statements are valid: myInteger = 4 + myInteger3; myDouble = myDouble2 * myDouble3;

35 Questions of type conversion also occur with arithmetic. In some cases it is possible to mix the types of operands without trouble. In other cases problems can result. For example, the following statement is valid: myDouble1 = myInteger1 – myDouble2;

36 Why can an integer and a double variable be combined without trouble? Automatic type conversion happens on the right. When mixing types, all are promoted to the type present which can hold the most information. In other words, myInteger1 is treated as a double, the result is a double, and this can be stored in myDouble1.

37 The following statement is not valid: myInteger1 = myInteger2 / myDouble1; The results on the right are promoted to a double, but this cannot be stored in an integer variable. In general, the best rule is not to rely on automatic conversion. When in doubt, explicitly cast all variables to the type you want in the arithmetic statements where they occur.

38 Another question to consider is the result of the following: myInteger1 = myInteger2 / myInteger3; Division potentially leads to an answer with a fractional part. However, when integers are divided in Java, only the whole part of the answer is kept. In other words, the result is a truncated integer value, and it is correct to store it in another integer variable.

39 It is also possible to obtain the remainder of integer division. The arithmetic symbol for this operation is “%” and is known as the modulus operator. In the following example the value 10 would be assigned to myInteger1, the remainder upon dividing 30 by 20. myInteger1 = myInteger3 % myInteger2;

40 Part of the power of variables is that they can be assigned new values that depend on their current values. In other words, the following statements are valid: myInteger1 = myInteger1 + 1; myDouble1 = myDouble1 - 5.0;

41 Such incrementing and decrementing operations are so common that a shorthand syntax exists for them. The statements shown below are equivalent statements to the ones above, respectively. myInteger1++; myDouble1 -= 5.0;

42 Two minus signs can be substituted for the two plus signs to decrement by one. Changing the single minus sign in the second statement to a plus sign will change the operation to incrementing by a floating point value of 5.0.

43 There are a few remaining loose ends to tie up concerning simple arithmetic. Although not recommended, from previous material it is probably apparent that a variable can be declared when needed. Thus, a statement such as this is syntactically acceptable: int myInteger4 = myInteger2 + myInteger3;

44 Aside from the lack of foresight evident in such programming, this practice is the cause of another type of error. If instead of declaring all needed variables once at the top you are in the habit of declaring them as you go, you are liable to declare the same variable more than one time. This is a syntax error.

45 It is also possible to do arithmetic inside of print statements. Assuming that the variables have been declared and assigned values, this is a valid statement: System.out.println(myInteger1 + myInteger2); This also shows lack of foresight and it has the shortcoming that the result of the arithmetic is not available for future use since it has not been saved in another variable.

46 There is one final, important observation to make about arithmetic in Java. As with the vast majority of languages, numeric values in Java are internally stored as binary numbers. The results of some decimal arithmetic may not have exact representations in binary. Some languages try to make life more convenient by showing result values in a form that the user might expect.

47 Java takes the approach of showing the user the exact value as manipulated in the program. When doing simple arithmetic and expecting a result of 0.5, for example, you may find that your output shows a value such as 0.49999999. You should not be surprised. There are ways of explicitly formatting output so that you see the value 0.5 instead, but they are not covered in these notes. It is simply necessary to keep in mind that Java tells no lies on this account.

48 3.4 The Math Class

49 The Java system makes available many math constants and functions. It does this by means of a class named Math. For complete information on this class and what it contains, refer to the Java API documentation. In this section a subset of these features will be discussed which illustrate the characteristics of the class. First of all, to make use of the class in a program, do the following at the top: import java.lang.Math;

50 The Math class contains certain mathematical constants such as pi and e. These constants can be used in your programs using the names Math.PI and Math.E. The variable names are capitalized because they’re constants. Assuming that the variables area and radius have been declared and a value has been assigned to radius, here is an example of the use of one of these constants: area = Math.PI * radius * radius;

51 In this example the name of the class is followed by a dot, which is followed by the name of the variable. Although it is contained in the Math class, the variable is available for use in a program without making use of a method to obtain it. Such a variable is called a static variable, which differs from other variables mentioned elsewhere in these notes. More details on static variables will be given later.

52 It is also important to observe that the Math class is used without constructing an instance of it. The variables in the Math class are available for use without having an object in existence. A class may serve as a model for the creation of objects, but it may also serve as a container for useful things. The Math class exists only as a container, and the constants are some of the useful things it contains.

53 The Math class also contains higher mathematical operators, such as the trigonometric functions, etc. Several will be used as examples here. Just like with the mathematical constants, it is not necessary for there to be an instance of the Math class in order to use its methods. Such methods are referred to as static methods.

54 In the examples of making method calls given previously, the model was: object.method(parameters); When using static methods there is no object to call the method on. All information which goes into the method goes in as parameters, and a return value is expected in order to capture the result. The model is: returnValue = ClassName.method(parameters);

55 The returnValue variable has to be correctly typed to match what the method will return and the parameters also have to agree with the types expected by the method. Here are schematics of some of the functions. Notice that the schematic begins with a declaration of the type of the return value, and the parameters are declared with their types inside the parentheses.

56 double exp(double x) This returns e raised to the x power, e x. double pow(double x, double y) This returns x raised to the y power, x y. double log(double x) This returns the natural logarithm of x, ln x.

57 double sqrt(double x) This returns the square root of x. double abs(double x) This returns the absolute value of x, |x|.

58 Assuming that the Math class is available for use and the rest of the program is in place here is a fragment illustrating the use of one of these methods. : double someValue = 3.2; double resultValue; resultValue = Math.sqrt(someValue);

59 A mistake that you might be tempted to make might be something along these lines: resultValue = someValue.sqrt(); // No! No! No! This is wrong on two counts: Math class methods cannot be called on objects; the thing you want to operate on has to be passed in as a parameter; also, someValue is a variable, not an object, and it is not possible to call a method on a variable.

60 Here is another illustration of the use of one of these methods, writing this familiar formula in Java code: A = πr 2. area = Math.PI * Math.pow(radius, 2.0);

61 Keep in mind that these methods are black boxes. The user just has to know how to use them, not how they work. You can find out the required types of the parameters and the types of the return values in the Java API documentation. You will not find the code for the methods there.

62 3.5 The String Class

63 Strings are sequences of characters that are treated together as a group. They are among the most commonly manipulated data items, along with simple numeric variables. We have already encountered string constants. Anything enclosed in double quotes is a string, so the following is a string constant: “Hello World”

64 In Java there is a String class. It is possible to construct instances of this class and obtain references to them. Because strings are so commonly used, and because the double quotes are so closely associated with them, there is a special syntax for constructing strings. Here is an example: String myString = "Hello World";

65 Or: String myString; myString = "Hello World";

66 In other words, it is not necessary to make use of the keyword new in order to create an instance of a string. The reference to the string can now be used in the following way, for example, with the expected results: System.out.println(myString);

67 Once it has been declared, the reference, like a variable, can be re-used. In other words, it is now possible to do the following to myString: myString = "A new string";

68 We initially saw this model for calling methods on objects: object.method(parameters); Then we saw this model for making use of static methods: returnValue = Class.method(parameters);

69 Since String is a class, it has methods associated with it. One useful method allows you to find out the number of characters in a string object at any given time. The following illustrates its use: int stringLength; stringLength = myString.length();

70 This shows that methods called on objects can also return values. In this example the value returned depends entirely on the object the method is called on. No parameters are needed inside the parentheses. This is an example of a method that returns the contents of some instance variable maintained inside the object.

71 More generally, method calls on objects that return values may also take parameters. This will be explored in more detail later. This is the general model for these of calls: returnValue = object.method(parameters);

72 Although not immediately apparent, the String class has a special characteristic. If you examined all of the methods of the class, you would not find any that allowed you to modify a string. You can obtain information about a string object, you can obtain its contents, and you can assign a new string to an existing reference, thereby, effectively getting rid of whatever string the reference originally referred to. But you cannot change the string itself. This property of a string is referred to as immutability.

73 In Java, the individual characters in a string can be identified numerically by their position, starting at the left beginning with 0. This is illustrated below: String myString = “Hello”; Hello 01234

74 The String class has a method that returns a reference to a selected portion of the string. It takes two integer parameters. The first parameter tells the starting position of the substring of interest in the string. The second parameter tells the position immediately past the last character of interest.

75 Here is an example illustrating its use: String anotherString; anotherString = myString.substring(0, 4); System.out.println(anotherString); Hell

76 The model for this method can be given as: otherString = someString.substring(start, past_end); Notice that the method does not trim the original string. The original remains unchanged. The method returns a reference to the desired substring, which can be captured in another named string reference.

77 In addition to selecting substrings from strings, it is possible to put more than one string together. This is called concatenation. In fact, we have already seen it. When doing output, it was possible to do something like this: System.out.println("Hello" + " " + "World");

78 The parameters in that call are simply string constants, and the “+” sign is serving as the symbol for concatenation of strings. As a result, it should be no surprise that the following sequence of actions is possible with the expected results.

79 String myString1, myString2, myString3, myString4; myString2 = "Hello"; myString3 = " "; myString4 = "World"; myString1 = myString2 + myString3 + myString4; System.out.println(myString1);

80 Also as seen in output, when doing concatenation it is possible to mix strings and numerical values. You can do this: System.out.println("The result is: " + 27);

81 The following sequence of actions gives the same result: int result = 27; String myString = "The result is: "; String anotherString = myString + result; System.out.println(anotherString);

82 When shown as a parameter to the println() method, the “+” sign appears to be a punctuation mark, like a comma. However, it is an operator, but with a special characteristic. When used only on numeric types, it does addition. When used only on strings, it does concatenation.

83 When used on a mixture of numeric types and strings, it converts the numeric types to strings and does concatenation. The fact that the same operator does different things depending on what its operands are is referred to as overloading. This topic will come up again later on.

84 Because the “+” works in this way you can convert numeric values to strings in general. A pair of double quotes with nothing between them (not even a space) is referred to as the empty string. If you concatenate this with any numeric variable, the result is a string containing only the sequence of symbols that represented the value.

85 In other words, the following sequence would give the result shown: double myDouble = 3.75; String myString1 = "" + myDouble; String myString2 = myString1.substring(2, 3); System.out.println("The digit in the 10ths place is: " + myString2); The digit in the 10ths place is: 7

86 As mentioned earlier, there are classes with the names Integer and Double. Like the Math class, these classes contain useful methods. For example, it is possible to convert numbers without using the trick given above.

87 import java.lang.Double; … double myDouble = 3.75; String myString = Double.toString(myDouble); The toString() method converts a numeric value into a string containing the number.

88 These Integer and Double classes also contain methods that allow you to turn the contents of strings into numeric values, assuming the strings contain sequences of symbols which form valid numbers. Here is an example: import java.lang.Integer; … String myString = “375”; int myInteger = Integer.parseInt(myString);

89 There are similar classes for numeric types other than int and double. They all contain similar methods, allowing conversion to and from strings and numeric variables, as well as other methods. Details can be found in the Java API documentation.

90 3.6 Characters and Unicode

91 Familiar characters, like the letters of the alphabet and all other printable characters are represented internally in the computer with numeric codes. The coding system that Java relies on is known as Unicode. Unicode assigns an integer value to each separate printable character. Unicode is based in part on a previous coding system, known as ASCII.

92 Unicode handles thousands of characters from many different languages and alphabets. This brief introduction to the topic only covers the first 128 codes, which include the letters of the English alphabet, the symbols typically found on a keyboard, and some codes which do not represent printable characters.

93 The first thirty-two codes are referred to as control codes. They are not printable. They are referred to as control codes because they originated as instructions for communicating with or controlling a device, such as a teletype, receiving a stream of text. Most of these codes are of no use to the programmer.

94 However, several, such as line feed and carriage return, may be useful in programs that generate printed output. As a matter of fact, we have already encountered one way in which these control characters can be indirectly utilized in a program. In a previous unit it was noted that a line of code such as this would cause printing to start on a new line: System.out.print("\n");

95 The escape sequence "\n" causes the pair of control codes, line feed and carriage return, to be generated. Similarly, "\t" causes a horizontal tab to be generated. Although they are not literally printable, if put in a string to be printed, these characters affect the appearance of the output.

96 The control characters can also be manipulated directly instead of by means of an escape sequence. Direct manipulation is shown below after the printable characters are introduced. But first, here is a table of the first 32 codes, in decimal, along with their names and descriptions.

97 Unicode Values from 0 to 31, Control Characters CodeNameDescriptionCodeNameDescription 0NULNull16DLEData link escape 1SOHStart of heading17DC1Device control 1 2STXStart of text18DC2Device control 2 3ETXEnd of text19DC3Device control 3 4EOTEnd of transmission20DC4Device control 4 5ENQEnquiry21NAKNegative acknowledge 6ACKAcknowledge22SYNSynchronous idle 7BELBell23ETBEnd of transmission block 8BSBackspace24CANCancel 9HTHorizontal tab25EMEnd of medium 10LFLine feed26SUBSubstitute 11VTVertical tab27ESCEscape 12FFForm feed28FSFile separator 13CRCarriage return29GSGroup separator 14SOShift out30RSRecord separator 15SIShift in31USUnit separator

98 The Unicode values from 32 to 127 are printable, with the exception of 127, which stands for deletion. The letters of the alphabet, small and capital, and the various punctuation marks are arranged as they are for historical reasons. The sort order for characters or text items is based on their Unicode values.

99 Below is a table of these codes, in decimal, and their corresponding characters. It can be seen in the table that the capital letters come before the small letters, and various punctuation marks come before, between, and after the letters.

100 Unicode Values from 32 to 127, Printable Characters CodeCharCodeCharCodeCharCodeCharCodeCharCodeChar 32Space48064@80P96`112p 33!49165A81Q97a113q 34“50266B82R98b114r 35#51367C83S99c115s 36$52468D84T100d116t 37%53569E85U101e117u 38&54670F86V102f118v 39‘55771G87W103g119w 40(56872H88X104h120x 41)57973I89Y105i121y 42*58:74J90Z106j122z 43+59;75K91[107k123{ 44,60< 76L92\108l124| 45-61=77M93]109m125} 46.62> 78N94^110n126~ 47/63?79O95_111o127Delete

101 It is now possible to consider the character data type, char. The character type is effectively an integer type. It is possible to cast between something typed char and something typed int. The underlying value is always an integer.

102 The type determines whether the integer value or the Unicode character is displayed. The following program illustrates the idea that an integer can be cast to a character and printed out as such:

103 public class UnicodeChars { public static void main(String[] args) { char myChar; int i; i = 65; myChar = (char) i; System.out.println(myChar + ""); }

104 Its output consists of the capital letter A. In the println() call, the character has to be concatenated with the empty string because println() only accepts strings and simple types as parameters. However, as you see, once the integer value 65 has been converted to a character type, turning that character type into a string simply gives the string consisting of the one character, A.

105 If you want to use character values directly, they are set off by single quotes. Do not mistakenly use double quotes in this context. Double quotes signify a string.

106 The following program illustrates going in the other direction from the previous example program. This program starts with a character and turns it into an integer containing the corresponding Unicode value:

107 public class CharToInt { public static void main(String[] args) { char mychar = 'A'; int i = (int) myChar; System.out.println(i); } The output of the program consists of the integer value 65.

108 The End


Download ppt "Unit 3: Java Data Types Kirk Scott. 3.1 Data Types, Variable Declaration and Initialization 3.2 Assignment and the Relationship Between Numeric Types."

Similar presentations


Ads by Google