1 / 14

Java Data Types

Java Data Types. Assignment and Simple Arithmetic. Some recap. You can think of a variable as a container that you could use more than once to hold different values at different times during program execution.

edda
Download Presentation

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. Java Data Types Assignment and Simple Arithmetic

  2. Some recap • You can think of a variable as a container that you could use more than once to 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 such operations in a program. • int a = 2;int b = 3;int c = a * b; // c = 6

  3. Operators • + for addition! • - for subtraction! • * for multiplication! • / for division! • () for grouping! • Order of operations • Multiplication and division are performed before addition and subtraction. • Other than that, operations are performed from left to right. • int a = 3 * 2 + 1 + 8 / 4; // a = 9

  4. Arithmetic with variables and numbers (constants) • Arithmetic may include both variables and numeric values. Given: • int myInteger1 = 10;int myInteger2 = 20;int myInteger3 = 30;double myDouble1 = 10.0;double myDouble2 = 20.0;double myDouble3 = 30.0; • These statements would be valid: • intmyInteger = 4 + myInteger3; • 4 + 30 = 34 • double myDouble = myDouble2 * myDouble3; • 20.0 * 30.0 = 600.0

  5. ARGH! NOT MORE CONVERSION! • What about statements like this? • myDouble1 = myInteger1 – myDouble2; • 10 – 20.0 • Type conversion shows itself once again. • The previous statement is valid, the integer converts to a double and the subtraction is performed, no questions asked. • All types on the right side are promoted to the type present which can hold the most information.

  6. MY MIND!! • This would not be valid: • myInteger1 = myInteger2 / myDouble1; • The results on the right are promoted to a double, but this can’t be stored in an integer. • In general, it’s best not to rely on automatic conversion. • When in doubt, explicitly cast all variables to the type you want. • Like so: • myInteger1 = myInteger2 / (int)myDouble1;

  7. More casting confusion. • Consider these two statements: • myInteger1 = myInteger2 * (int) myDouble1; • myInteger1 = (int)(myInteger2 * myDouble1); • Although they might seem identical, the two statements will output different results, because casting (which may result in data loss) occurs at different points in the statement. • Let’s say: • myInteger2 = 2; • myDouble1 = 1.5; • This would output like so: • 2 * (int) 1.5 = 2 * 1 = 2 • (int)(2 * 1.5) = (int)(2.0 * 1.5) = (int)(3.0) = 3

  8. Division with integers • Consider: • myInteger1 = myInteger2 / myInteger3; • Division potentially leads to an answer with a fractional part. • However, when integers are divided, only the whole part of the answer is kept. The result is a truncated integer value, and it is correct to store it in another integer variable. • Let’s say • myInteger2 = 3; • myInteger3 = 4; • Then: • myInteger1 = (int)(3 / 4) = (int)(0.75) = 0

  9. Modulus • Would make for a really cool name for a robot or something. • But it is also how you can get the remainder of an integer division operation. • % - modulus operator. • Given: • myInteger3 = 30; myInteger2 = 20; • Then: • myInteger1 = myInteger3 % myInteger2; • = 30 % 20 • = (30 / 20) = 1, remainder 10 • myInteger1 = 10;

  10. Incrementing/Decrementing • The variable on the left side of the = sign can be used in the arithmetic on the right: • myInteger1 = myInteger1 + 1; • myDouble1 = myDouble1 – 5.0; • Things like that happen so much that there are shortcuts to it. • myInteger++; //Increments by 1 • ++ can increment a value by 1. -- decrements by one. • myDouble1 -= 5.0; //Decrements by 5 • You could use any arithmetic symbol like this.

  11. Declaration and arithmetic • You are allowed to perform arithmetic as an assignment to a variable you just declared. • int myInteger4 = myInteger2 + myInteger3; • However, this is not recommended. This sort of practice may encourage the declaration of multiple variables with the same name as you go along your code. This would result in a syntax error! • Keep it in mind that each variable you declare MUST have a different name than the others. • It’s recommended that when you’re writing code later on, you declare all of the variables you would need at the top of the code, and then do assignment on the values later on.

  12. Arithmetic inside print statements • Assuming variables have been declared and assigned values, it is possible to write: • System.out.println(myInteger1 + myInteger2); • However, it 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. • This is more acceptable: • intmyAnswer;myAnswer = myInteger1 + myInteger2;System.out.println(myAnswer);

  13. Floating Point Arithmetic Dangers • Recall that numeric data is stored internally as binary numbers. • The results of some decimal arithmetic may not have exact representations in binary. • Ergo, ‘answers’ made by the system may be incorrect. • This is due to the way floating point values are stored in binary. • However, the answer will be so close to what you expect that it is nearly insignificant in most applications. • For example, you may get 0.49999999 when you are expecting 0.5.

  14. Lab • Questions 12-18 on the assignment sheet. • Show what the output of each question would be. • If the question cannot output anything (that is, there is an error in the question), show what the error is. • If there is something stylistically wrong with the code, but you think it would still work, just show the output.

More Related