1.92k likes | 3.83k Views
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.
E N D
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. Monster fred = new Monster(); Monster sally = new Monster();
What is a reference? Monster fred = new Monster(); fred 0xF5 0xF5 Monster fred stores the address of the Monster
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
What is a variable? int days = 102; days 102 days stores an integer value
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
Identifiers Which of these would be legal variable identifiers? int 1stYear; double jump Up; double feet2Inches; IDENTIFIER is a fancy word for variable.
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!)
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
The 8 Java Primitive Data Types byte short int long float double char boolean int num = 9; double total = 3.4;
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.
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.)
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); } }
The Real Data Type Float has 7 digits of precision and double has 15 digits of precision.
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.)
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); } }
The Char Data Type Characters store letters. char let = ‘A’; char is a 16-bit unsigned integer data type.
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
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);
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); } }
The Boolean Data Type The boolean data type can store true or false only. boolean x = true; boolean y = false;
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 ); } }
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";
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;
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";
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;
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
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 ); } }
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!