unit 3: java data types

Post on 03-Jan-2016

45 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Unit 3: Java Data Types. Kirk Scott. 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. - PowerPoint PPT Presentation

TRANSCRIPT

Unit 3: Java Data Types

Kirk Scott

• 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.1 Data Types, Variable Declaration and Initialization

• These are some of the simple numeric data types in Java:

• • int for integers• long for large integers• float for single precision floating point values• double for double precision floating point

values

• 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.

• 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.

• 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.

• 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:

• 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.

• 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.

• 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.

• 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.

• 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.

• 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;

• 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.

• 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.

• 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;

• 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.

3.2 Assignment and the Relationship Between Numeric Types

• 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 */

• 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.

• 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.

• 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.

• 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.

• 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.

• 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 */

• 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;

• 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.

• 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.

• 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.

3.3 Assignment and Simple Arithmetic

• 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.

• 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.

• 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;

• 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;

• 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.

• 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.

• 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.

• 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;

• 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;

• 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;

• 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.

• 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;

• 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.

• 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.

• 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.

• 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.

3.4 The Math Class

• 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;

• 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;

• 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.

• 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.

• 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.

• 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);

• 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.

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

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

• 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);

• 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.

• Here is another illustration of the use of one of these methods, writing this familiar formula in Java code:

• A = πr2.• • area = Math.PI * Math.pow(radius, 2.0);

• 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.

3.5 The String Class

• 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”

• 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";•

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

• 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);

• 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";

• 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);

• 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();

• 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.

• 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);

• 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.

• 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

• 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.

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

• 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.

• 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");

• 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.

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

• 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);

• 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);

• 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.

• 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.

• 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.

• 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

• 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.

• 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.

• 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);

• 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.

3.6 Characters and Unicode

• 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.

• 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.

• 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.

• 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");

• 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.

• 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.

Unicode Values from 0 to 31, Control Characters

Code Name Description Code Name Description0 NUL Null 16 DLE Data link escape1 SOH Start of heading 17 DC1 Device control 12 STX Start of text 18 DC2 Device control 23 ETX End of text 19 DC3 Device control 34 EOT End of transmission 20 DC4 Device control 45 ENQ Enquiry 21 NAK Negative acknowledge6 ACK Acknowledge 22 SYN Synchronous idle7 BEL Bell 23 ETB End of transmission

block8 BS Backspace 24 CAN Cancel9 HT Horizontal tab 25 EM End of medium10 LF Line feed 26 SUB Substitute11 VT Vertical tab 27 ESC Escape12 FF Form feed 28 FS File separator13 CR Carriage return 29 GS Group separator14 SO Shift out 30 RS Record separator15 SI Shift in 31 US Unit separator

• 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.

• 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.

Unicode Values from 32 to 127, Printable Characters

Code Char Code Char Code Char Code Char Code Char Code Char32 Space 48 0 64 @ 80 P 96 ` 112 p33 ! 49 1 65 A 81 Q 97 a 113 q34 “ 50 2 66 B 82 R 98 b 114 r35 # 51 3 67 C 83 S 99 c 115 s36 $ 52 4 68 D 84 T 100 d 116 t37 % 53 5 69 E 85 U 101 e 117 u38 & 54 6 70 F 86 V 102 f 118 v39 ‘ 55 7 71 G 87 W 103 g 119 w40 ( 56 8 72 H 88 X 104 h 120 x41 ) 57 9 73 I 89 Y 105 i 121 y42 * 58 : 74 J 90 Z 106 j 122 z43 + 59 ; 75 K 91 [ 107 k 123 {44 , 60 < 76 L 92 \ 108 l 124 |45 - 61 = 77 M 93 ] 109 m 125 }46 . 62 > 78 N 94 ^ 110 n 126 ~47 / 63 ? 79 O 95 _ 111 o 127 Delete

• 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.

• 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:

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

• 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.

• 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.

• 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:

• 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.

The End

top related