Data Types A data type is a scheme for using bits (binary digits) to represent values. These values are not just numbers, but any kind of data that a computer can process. All values in a computer are represented using one type of data or another. In any programming language, there are many different data types and even ones you, as a programmer, can create. Some types of data are so fundamental to programmers that Java has built in ways to represent them. These are the primitive data types. The names of the eight primitive data types are: int double long short byte float boolean char These data types are referred to as primitive because they are the foundations upon which larger parts of a program are built. The first six listed above are used to store numeric data; Boolean and char are generally used to store alphabetic or character information. The basic difference between the numeric primitives is in the range of numbers they can store. Many real-world programs deal with massive amounts of data (billions of data items) and, thus, the smaller size types (byte and short) may save significant amounts of space and time Name Description Range Examples int Integers (whole numbers) 42, -3, 0, 926394 double Real numbers -0.25, 9.0, 1253.9856 boolean Logical values True, false char Single text characters a , X, ?, \n It is important to use the proper data types so that you reduce storage needed to run your program. For example, a btye can hold a minimum value of –128 and max 127, where as an int can have a minimum value of -2 147 483 648 (-2^31) and max value of 2 147 483 647 (2^31-1). The less memory you need to run your program the faster it will run!! Fill in four more data types and provide examples in the chart above. Sort out the range for all each data type. For more information on data types go to: http://www.tutorialspoint.com/java/java_basic_datatypes.htm Variables Essentially, a variable is a name for a location in computer memory which uses a particular data type to hold a value. Also, this enables you to change a value or string in several locations by changing just the variable value. Eg. Think about the xPos (x postion) variable in your Khan Academy animal program. Naming Conventions Use a variable name that effectively describes the value being stored within it. Use only the characters 'a' through 'z', 'A' through 'Z', '0' through '9', character '_'. Start with lower case letter. First letter of other words in name are capitalized; eg. myVariable A variable name cannot contain the space character. Do not start with a digit. The length of a variable name is <256 characters. Upper and lower case count as different characters; eg. SUM and Sum are different names. A name cannot be a reserved word. A name must not already be in use in this part of the program. To create a variable for use in a program: The line int myNumber = 123; is a declaration of a variable. A declaration of a variable is where a program says that it needs a variable. For our small programs, declaration statements will be placed between the two braces of the main method. In this example, we have assigned the variable its value. Note: A reserved word is a word which has a predefined meaning in Java. For example int, double, true, and import are reserved words Task Type up the program above. Change the number to 12.5., what do you notice? Can you make the program run with using 12.5. Also, add a statement after the ‘myNumber’ in the code above. Can you do this using only the one line of code. Make sure you save this as L2a_VariableEx_Name_v1 Ways to Declare a Variable A variable declaration may be made in several ways: dataType variableName; The first way declares a variable, declares its data type, and reserves memory for it. It says nothing about what value is put in memory. dataType variableName = initialValue; The second way declares a variable, declares its data type, reserves memory for it, and puts an initial value into that memory. The initial value must be of the correct dataType. dataType variableNameOne, variableNameTwo; The third way declares two variables, both of the same data type, reserves memory for each, but puts nothing in any variable. You can do this with more than two variables, if you want. dataType variableNameOne = initialValueOne, variableNameTwo = initialValueTwo; The fourth way declares two variables, both of the same data type, reserves memory, and puts an initial value in each variable. You can do this all on one line if there is room. Again, you can do this for more than two as long as you follow the pattern. Constants The value of a constant never changes. Constants are useful for setting global states (values that can be used anywhere in a method or object). For example, a fixed interest rate or pi are values you may not want to change. Unlike a variable, a constant must be assigned a value in its declaration. To declare a constant, the Java keyword final appears before the variable declaration. Example: Ways to Declare a Constant Constants follow the same rules as for variable names with two exceptions: Constants are always written in capital letters. For example: RATEOFINTEREST Constants are always initialized when declared. For example: final double RATEOFINTEREST = 0.0345; Task Type up each program above. Add a statement to calculate the circumference of the circle with the given radius. Make sure you save this as L2a_myConstant_Name_v1 Question 1. Although the program runs, what is missing in the user response statement in the above ConstantEX program? 2. What style difference is there between a variable name and a constant name? Answer 1 A correct mathematical response should include the unit squared. i.e. cm 2 Answer 2 The style difference is that constants are declared using capital letters for the variable name. The word final is used in the declaration. Variable names start with a small letter and follow "camel-style" notation. For example: xxXxx, or myInput