Java Primitive Data Types and Operations

building java programs chapter 2 n.w
1 / 18
Embed
Share

Explore Java's primitive data types and operators, from integers and characters to logical values. Learn about arithmetic operators and how to evaluate complex expressions using precedence rules. Dive into the world of data types, expressions, and division in Java programming, with practical examples and explanations.

  • Java Programming
  • Data Types
  • Arithmetic Operators
  • Expressions
  • Primitive Data

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS

  2. OBJECTIVES Identify Java s primitive data types and operators. Evaluate complex expressions using rules of precedence and mixed types. 2 2

  3. DATA TYPES A name for a category of data values that are all related. Different data types support different operations and behave differently. You can multiply numbers, but can you multiply strings? Can you add them? Stored differently in computer memory as well. 27 = 00011011 hello = 01101000 00000101 1101100 1101100 01101111 3 3

  4. JAVAS PRIMITIVE DATA TYPES Name Description Example integers (whole numbers) int 42, -3, 18, 20493, 0 real numbers double 7.35, -19.83423, 18.0 single characters char 'a', 'X', '3', '\n' logical values boolean true, false Variations of int: byte, short, long Variations of double: float 4 4

  5. JAVAS ARITHMETIC OPERATORS Operator Meaning Example Result addition + 2 + 2 4 subtraction 53 18 35 multiplication * 3 * 8 24 division / 4.8 / 2.0 2.4 mod (remainder) % 19 % 5 4 Most behave exactly like you would expect them to in math class, but we ll learn about the exceptions soon. 5 5

  6. EXPRESSIONS An expression is a simple value or a set of operations that produces a value. Simplest expressions are literals of the data types we know. E.g. 27, Seattle... Yay!", -1.7, false More complex expressions can use operators and parentheses. E.g.: 5 * 2 7 - 3 * (1.3 (5.7 3)) System.out.println( 5 * 2 / 3 + 6 ); 6 6

  7. FLOATING-POINT DIVISION When dividing double or floatvalues, it s just like math class, but with some rounding errors here and there 5.0 / 2.0 = 2.5 10.0 / 3.0 = 3.3333333333333335 (rounding error!) 4.0 / 5.0 = 0.8 8.0 / 3.0 * 3.0 = 8.0 7 7

  8. INTEGER DIVISION WEIRD When dividing integers, we keep the whole part, but discard the fractional part. not 2.5 5 / 2 = 2 not3.333 10 / 3 = 3 not 0.8 4 / 5 = 0 8 / 3 * 3 = not 8! ? 2 * 3 = ? 6 8 8

  9. INTEGER MOD (%) The mod operator computes the remainder of a division 6 / 4 = 1, but it would have had a remainder 2 Therefore, 6 % 4 = 2 20 % 7 = 15 % 4 = 3 6 13857 % 2 = 13856 % 2 = 0 1 8374 % 10 = 8374 % 100 = 4 74 36 % 6 = 7 % 9 = 7 0 9 9

  10. THINK, PAIR, SHARE In your notebook 1. Write four expressions using only % and / that will get me the four individual digits of 7382. = 2 a. 7382 b. 7382 c. 7382 d. 7382 ????? % 10 = 8 / 10 % 10 ????? = 3 / 100 % 10 ????? = 7 / 1000 % 10 ????? *** Will your expressions work for other four-digit numbers? *** 10 10

  11. OPERATOR PRECEDENCE Usually, we evaluate expressions left-to-right, but certain operations take precedence over others and get evaluated first. Parentheses can always override precedence. Precedence table: Description Operators unary operators multiplicative operators additive operators +, - *, /, % +, - 11 11

  12. PRECEDENCE EXAMPLES 2 + 2 * 5 = 12 not 20 3 * 3 + 2 * 2 = 13 not 22 7 + 5 / 2 * 3 4 = 9 not 14 +3 * -4 = -12 3 + -4 = -1 3 - -4 = 7 12 12

  13. MIXING TYPES When doing an operation on an int and a double, the int gets promoted to a double. 1 * 4.682 = 4.682 7 / 2.0 = 3.5 5 * 1.0 / 2 = 3.0 7.0 / 2 7 / 2 = ? 3.5 3 = ? 0.5 13 13

  14. CASTING You can explicitly convert a value from one data type to another by casting. (double) 99 = 99.0 (int) 2.5 = 2 ((double) 7) / 2 = 3.5 (int) 2.5 * 3.0 = 6.0 (int) (2.5 * 3.0) = 7 14 14

  15. IN YOUR NOTEBOOK 4.0 / 2 * 9 / 2 2.0 * 9 / 2 18.0 / 2 9.0 15 15

  16. IN YOUR NOTEBOOK 12 / 7 * 4.4 * 2 / 4 1 * 4.4 * 2 / 4 4.4 * 2 / 4 8.8 / 4 2.2 16 16

  17. IN YOUR NOTEBOOK 9 / 2.0 + 7 / 3 3.0 / 2 4.5 + 7 / 3 3.0 / 2 4.5 + 2 3.0 / 2 4.5 + 2 1.5 6.5 1.5 5.0 17 17

  18. IN YOUR NOTEBOOK 53 / 5 / (0.6 + 1.4) / 2 + 13 / 2 53 / 5 / 2.0 / 2 + 13 / 2 10 / 2.0 / 2 + 13 / 2 5.0 / 2 + 13 / 2 2.5 + 13 / 2 2.5 + 6 8.5 18 18

More Related Content