1 / 40

Java Variables and Data Types

Java Variables and Data Types. Reference Variable Keyword Identifier Data type int float char double. boolean Object Initializing a variable Assignment operator. Vocabulary & Terms. What is a reference?. A reference variable refers to a location in memory that stores an object.

hoyt-mccray
Télécharger la présentation

Java Variables and 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 Variables and Data Types

  2. Reference Variable Keyword Identifier Data type int float char double boolean Object Initializing a variable Assignment operator Vocabulary & Terms

  3. What is a reference? A reference variable refers to a location in memory that stores an object. Monster fred = new Monster(); Monster sally = new Monster();

  4. What is a reference? Monster fred = new Monster(); fred 0xF5 0xF5 Monster fred stores the address of the Monster

  5. What is a variable?

  6. What is a variable? A variable is a storage location for some type of value. int days = 102; double taxrate = 7.75; char grade; days taxrate 102 7.75

  7. What is a variable? int days = 102; days 102 days stores an integer value

  8. How do you name variables when defining them?

  9. Variable Names • When naming a variable follow these rules and conventions: • • Make the variable name meaningful. That means that L is not a meaningful variable name but length is meaningful. • • Start all variable names with a lower-case letter. • • Variable names may also contain digits. • • If a variable name is more than one word long, capitalize each of the other words without adding any spaces. • Below are some examples of valid variable names: • number sum32 testAverage areaOfTrapezoid • and below are some examples of invalid variable names: • 2ndValue test Average question#2

  10. Identifiers Which of these would be legal variable identifiers? int 1stYear; double jump Up; double feet2Inches; IDENTIFIER is a fancy word for variable.

  11. Case-Sensitivity Java is case sensitive. Brandon does not equal brandon. Brandon != brandon Java is case sensitive.(Yes, it’s so important that I said it twice!)

  12. Keywords are reserved words that the language uses for a specific purpose. You cannot use keywords as identifier names. int double return void static long break continue You can find the complete list of keywords here:http://java.sun.com/docs/books/tutorial/java/nutsandbolts/_keywords.html

  13. What is a data type?

  14. The 8 Java Primitive Data Types byte short int long float double char boolean int num = 9; double total = 3.4;

  15. The Integer Data Type

  16. The Integer Data Type

  17. The Integer Data Type What is a bit? Memory consists of bits and bytes. 1 byte = 8 bits 8 bits = 1001 0010 16 bits = 0101 1001 0100 1001 The more bits you have the more you can store.

  18. The Integer Data Type OUTPUT 120987123 int one = 120; int two = 987123; System.out.println(one); System.out.println(two); (Type the code above into the interactions pane in Dr. Java and check your output.)

  19. The Integer Data Type Type the following code into Dr. Java exactly as it is written. Compile the code and run the program. Are the results what you expected? //integer example public class Integers { public static void main(String args[]) { int one = 120; //legal assignment int two = 987123; int three = 999999999; System.out.println(one); System.out.println(two); three = three * 3; //creates an overflow error at runtime System.out.println(three); } }

  20. The Real Data Type

  21. The Real Data Type Float has 7 digits of precision and double has 15 digits of precision.

  22. The Real Data Type OUTPUT 99.573217.0 23.32 double one = 99.57; double two = 3217; float three = 23.32f; System.out.println(one); System.out.println(two); System.out.println(three); (type the code above into the interactions pane in Dr. Java and check your output.)

  23. The Real Data Type Type the following program into Dr. Java exactly as it is written. Compile the code and then run the program. Are the results what you would have expected? //real number example public class Reals { public static void main(String args[]) { double one = 99.57; double two = 3217; //float ten = 234.234; //not legal float three = 23.32f; //F states that 23.32 is a float value System.out.println(one); System.out.println(two); System.out.println(three); } }

  24. The Char Data Type

  25. The Char Data Type Characters store letters. char let = ‘A’; char is a 16-bit unsigned integer data type.

  26. The Char Data Type char is a 16-bit unsigned int data type. For example, here is a 16 bit pattern: 000000000110011 char let = 65; ASCII VALUES YOU SHOULD KNOW!!! ‘A’ – 65 ‘a’ – 97 ‘0’ - 48

  27. The Char Data Type OUTPUT A A 67 C char alpha = 'A'; char ascii = 65; char sum = 'B' + 1; System.out.println(alpha); System.out.println(ascii); System.out.println('B'+1); System.out.println(sum);

  28. The Char Data Type Type the following program in Dr. Java exactly as it is written. Compile the code and run the program. Are the results what you expected? public class Char { public static void main(String args[]) { char alpha = 'A'; char ascii = 65; char sum = 'B' + 1; System.out.println(alpha); System.out.println(ascii); System.out.println('B'+1); //char is an integer type System.out.println(sum); } }

  29. The Boolean Data Type

  30. The Boolean Data Type The boolean data type can store true or false only. boolean x = true; boolean y = false;

  31. The Boolean Data Type Type the following program into Dr. Java exactly as it is written. Compile the code and run the program. Are the results what you expected? public class Boolean { public static void main(String args[]) { boolean stop = true; boolean go = false; System.out.println( stop ); stop = go; System.out.println( go ); } }

  32. Objects In JAVA, you have 8 primitive data types that take up very little memory. Everything else in Java in an Object. Strings are objects. String temp = "abc";

  33. The Assignment Statement receiver = 57; receiver is assigned( = ) the value 57. In an assignment statement, the receiver is always on the left of the assignment symbol ( = ). Assignment statements examples : total = 10; amount = 100.34;

  34. Initializing Shortcut You can initialize more than one variable in one statement if they have the same type. Just separate them using a comma. int number = 75, it=99; float taxrate = 7.75; char letter = ‘A’, newlet = ‘a’; boolean isprime = false; String sone = "abc";

  35. Mixing Data

  36. Mixing Data Java is a strong typed language. As a result, you must be very careful to look at data types when assigning values. int one=90; char letter= ‘A’; char let= 65; one=letter; letter=let; one=let;

  37. Mixing Data int one = 90; double dec = 234; char letter = 'A'; System.out.println( one ); one = letter; //char to int System.out.println( one ); one = 'A'; //char to int System.out.println( one ); System.out.println( dec ); dec = one; //int to double System.out.println( dec ); OUTPUT 90 65 65 234.0 65.0

  38. Type the following program into Dr. Java. Compile the code and run the program. Are the results what you expected? //strong typed language example public class MixingData { public static void main(String args[]) { int one = 90; double dec = 234; char letter = 'A'; System.out.println( one ); one = letter; //char to int System.out.println( one ); one = 'A'; //char to int System.out.println( one ); System.out.println( dec ); dec = one; //int to double System.out.println( dec ); System.out.println( letter ); //letter = dec; //double to int - not legal //System.out.println( letter ); } }

  39. Now, it’s time for some practice! You are to complete the assignment named Variables Worksheet. Type your answers directly onto the worksheet. Make sure you enter your name on the top of the sheet. Drop the worksheet into the appropriate Dropbox named Variable Worksheet. Good luck!

More Related