1 / 28

Primitive Data Types

Primitive Data Types. Identifiers. What word does it sound like?. Identifiers. Name that will be used to describe anything a programmer is required to define. classes, methods, constants, variables; Examples Name of objects marker, pencil Methods turnRight, forward, move.

jmable
Download Presentation

Primitive 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. Primitive Data Types

  2. Identifiers • What word does it sound like?

  3. Identifiers • Name that will be used to describe anything a programmer is required to define. • classes, methods, constants, variables; • Examples • Name of objects • marker, pencil • Methods • turnRight, forward, move

  4. Rules for Identifiers • Must start with a letter • After the letter, can be any combination of letters, numbers, or _ • No SPACES!!! • Cannot be a reserved word (words with special meanings in java [see handout])

  5. Example Identifiers • Are these okay? • myPerson • m_person • person1 • my Person • 1person • person#1

  6. Example Identifiers • These are fine • myPerson • m_person • person1 • These are NOT • my Person • 1person • person#1

  7. Java is Case Sensitive • Person ≠ person ≠ perSon

  8. Good Identifiers • Don’t use single letters • Make them descriptive • grades instead of g • Variable names should be meaningful but concise • gpa instead of gradePointAverageForStudentsAtThisSchool

  9. Some Conventions • Class names start with capitals • DrawingTool • Variable names start with lowercase • marker • Multiple word names have a capital letter at the beginning of each new word • turnRight • Constants (value never changes) are in all capitals • MAXSCORE

  10. Data Types • Depending on what you want to store in java, you need to tell it what type it is. • Why do you think it matters if something is a number or a letter?

  11. Type Matters • Math • You can’t add the number 5 to the word “Happy” • Depending on the type, java has to make a given amount of space for it.

  12. Primitive Data Types • int – integers – whole numbers • -5 0 86 • double – decimals • 3.14 5.0 -1.2 6.02e23 • scientific notation - 6.02e23 = 6.02x10^23 • boolean – true or false • char – holds one character • ‘a’ ‘%’ ‘6’

  13. Invalid Numbers • Don’t do this • $5.06 • #3.0 • 86%

  14. You Might Also see • long and short are like int • float is like double

  15. Declaring variables • Remember me? • DrawingTool marker; • Other variables are the same: • int number; • number = 86; • int number = 86; • You only declare the type once! • First time> DrawingTool marker; • After> marker.

  16. Ascii • The characters are secretly stored as integer values. Thus ascii value 65 is the captial ‘A’

  17. System.out • One way to print out to the screen • System.out.print • Print and don’t skip a line • System.out.print(“Hello”); System.out.print(“World”); • prints HelloWorld • System.out.println • Print and skip a line • System.out.println(“Hello”); System.out.println(“World”); • prints Hello World

  18. Examples int number = 5; char letter = 'E'; double average = 3.95; boolean done = false; System.out.println("number = " + number); System.out.println("letter = " + letter); System.out.println("average = " + average); System.out.println("done = " + done); System.out.print("The "); System.out.println("End!"); Run output: number = 5 letter = E average = 3.95 done = false The End!

  19. What does + mean? • Inside System.out.println("number = " + number); • + means add the words “number = “ to the value of number

  20. Escape Characters Character Java Escape Sequence Newline '\n' Horizontal tab '\t' Backslash '\\' Single quote '\'' Double quote '\"' Null character '\0' System.out.println(“This is a\ntest and only\’ a test.”); Run output: This is a test and only’ a test.

  21. Interesting Differences System.out.println( 2 + 2); //Output: 4 System.out.println(“2 + 2”); //Output: 2 + 2 System.out.println(“2” + “2”); //Output: 22

  22. Casting char letter = 'A'; int number = 75; System.out.println("letter = " + letter); System.out.print("its ASCII value = "); System.out.println((int)letter); System.out.print("ASCII value 75 = "); System.out.println((char)number); Run output: letter = A its ASCII value = 65 ASCII value 75 = K

  23. Assignment (=) • The = sign works RIGHT to LEFT only! • a = 5; • Means the variable a gets the value 5 • 5 = a; DOES NOT WORK!!! • a = 3; b = 5; a = b; a now equals? b now equals?

  24. Variables with = • On the LEFT side, mean save the answer here • a = 5 + 3; • On the RIGHT side, means look up the value • b = 6; • a = b + 2;

  25. You can do math • a = 5 + 3; • Adding is + • Subtracting is – • Multiplication is * • Division is / • Modulus is %

  26. Careful with division • If you divide by an integer, java will round down • 15/4 = 3! • If you divide by a decimal, java will give you an exact answer • 15/4.0 = 3.75

  27. Careful How you Save • If you save a decimal in to an int, the decimal part of the number will be lost • int x = 3.14;

  28. Mod (%) • Remember how you learned division? • 13/4 = 3R1 • Mod just means give me the remainder from dividing. • 13%4 = 1

More Related