1 / 108

Unit 3: Java Data Types

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.

Download Presentation

Unit 3: Java Data Types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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: • int for integers • long for large integers • float for single precision floating point values • double for 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; • intdays_in_a_week; • intweeks_in_a_year = 52; • inthours_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: • intmyInteger; /* 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; • intweeksInaYear = 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; • intweeksInaYear = 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;

More Related