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.
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation
Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement.Téléchargez la présentation en cliquant sur ce lien.Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.
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
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; • intdays_in_a_week; • intweeks_in_a_year = 52; • inthours_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.
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.
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 */
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.
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.
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;
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.
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;
Connexion au compte
Connectez-vous pour publier votre contenu sur SlideServe.