ch03 - Variables and Data Types

Views:
 
Category: Education
     
 

Presentation Description

No description available.

Comments

Presentation Transcript

Outline : 

1 Outline Variables Primitive Data Types Arrays The Application of Arrays Summary

Variables : 

2 Variables

What is variables? : 

3 What is variables? Variables are locations in memory in which values can be stored. Every variable has a name, a type, a size and a value. Before you can use a variable, you have to declare it. After it is declared, you can then assign values to it.

Variable Types : 

4 Variable Types The variable type can be one of the three things: One of the eight basic primitive data types The name of a class An array

Three kinds of variables : 

5 Three kinds of variables instance variables class variables local variables no global variables

Instance variables : 

6 Instance variables Attributes or the state of a particular object Store information needed by multiple methods in the object Have different values for each object

Class variables : 

7 Class variables Similar to instance variables Apply to all class’s instances Apply to the class itself

Local variables : 

8 Local variables Inside method definitions Store information needed by a single method

Declaring variables : 

9 Declaring variables Variable declarations consist of a type and a variable name, e.g.: int count; String name; boolean validated; Variable declarations can go anywhere in a method definition Most commonly declared in the beginning of the definition before they are used

Sample code : 

10 Sample code

The same type variables : 

11 The same type variables The variables with the same type can be stringed together, e.g.: double x, y, z; String fname, lname;

Variables’ initial value : 

12 Variables’ initial value The initial value of a variable can be given in declaration, e.g.: int a = 10, b = 5; String myname = “Maida” Only the last variable has initial value if there are multiple variables on the same line, e.g.: double x, y, z = 10.0;

Default initial values : 

13 Default initial values Local variables have no initial value, they should be given values before they can be used. Instance and class variable definitions do not have the restriction, their initial value depends on the type of the variable: null for instances and classes 0 for numeric variables ‘\0’ for characters false for booleans

Sample code : 

14 Sample code

Sample code : 

15 Sample code

Variable Names : 

16 Variable Names Start with a letter or underscore (_), or dollar sign ($) Do not start with a number After the first character, any letter or number can be included in the name Unicode is supported

Notes about variable names : 

17 Notes about variable names Case-sensitive maida is not a MaiDa is not a MAIDA Meaningful names int countOfStudentNumber boolean currentWeatherIsGood int intUserAge String strAdminName

Primitive Data Types : 

18 Primitive Data Types

Eight primitive data types : 

19 Eight primitive data types byte short int long char float double boolean

Integer : 

20 Integer Octal 03, 07 Hexadecimal 0xab 0X1a

Sample code : 

21 Sample code

Sample code : 

22 Sample code

Char : 

23 Char Unicode provides a unique number for every character, no matter what the platform, no matter what the program, no matter what the language. Incorporating Unicode into client-server or multi-tiered applications and websites offers significant cost savings over the use of legacy character sets.

Special codes : 

24 Special codes

ASCII character set : 

25 ASCII character set

Sample code : 

26 Sample code

Floating-point types : 

27 Floating-point types 18.0f ≠ 18.0d = 18.0 18. = 1.8e1 = .18E2 A float value can be assigned to a double variable. A double value can not be assigned to a float variable

Sample code : 

28 Sample code

Sample code : 

29 Sample code

Boolean types : 

30 Boolean types true/false

Sample code : 

31 Sample code

Arrays : 

32 Arrays

What is array? : 

33 What is array? Arrays is a way to store a list of items. Each element of the array holds an individual item. You can place items into and remove items from those slots as you need to.

Notes about arrays : 

34 Notes about arrays Any type of value can be contained in an array. A single array can only store one type of item. Array names follow the same conventions as other variable names. A subscripted array name is an lvalue—it can be used on the left side of an assignment to place a new value into an array element.

The position number of arrays : 

35 The position number of arrays The first element in every array has position number zero (sometimes called the zeroth element). The first element of array a is a[ 0 ] The second element of array a is a[ 1 ] In general, the ith element of array a is a[ i - 1 ]

Three steps of creating arrays : 

36 Three steps of creating arrays Declare a variable to hold the array. Create a new array object and assign it to the array variable. Store things in the array.

Declaring array variables : 

37 Declaring array variables Array types indicate the type of object the array will hold and the name of the array, followed by empty brackets ([]), e.g.: String names[]; int fibonacci[]; The empty brackets can be put after the type instead of after the variable, e.g.: String[] names; int[] fibonacci;

Creating array objects : 

38 Creating array objects Two ways can be chose to create an array object and assign it to the variable: Using new Directly initializing the content of the array

Using new : 

39 Using new When new is used to create a new array object, the size of the array should be indicated, e.g.: String[] names = new String[5]; int fibonacci = new int[10]; When new is used to create a new array object, all it’s elements are initialized: 0 for numeric arrays ‘\0’ for character arrays null for everything else

Sample code : 

40 Sample code

Sample code : 

41 Sample code

Directly initialize : 

42 Directly initialize Instead of using new, you can enclose the elements of the array inside braces, separated by comma, e.g.: String[] names = { “a”, “b”, “c”, “d”, “e” }; Each of the elements inside the bracket must be the same type as the variable that hold the array. int fibonacci[] = { 0, 1, 1, 2, 3, 5, 8 };

Sample code : 

43 Sample code

Accessing array elements : 

44 Accessing array elements To get a value stored within an array, use the array subscript expression: array[i]. Array subscript starts with 0, so an array with ten elements has array values from subscript 0 to 9.

Sample code : 

45 Sample code

How to get an array’s length? : 

46 How to get an array’s length? An array’s length can be get through using the length instance variable which is available for all objects, regardless of type: int len = array.length;

Changing array elements : 

47 Changing array elements To change the value of array elements, put an assignment statement after the array access expression Arrays of primitive types copy the values from one slot to another. Arrays of object is just an array of references to those objects.

Sample code : 

48 Sample code

Changing array length : 

49 Changing array length

Multidimensional arrays : 

50 Multidimensional arrays Multidimensional arrays are not directly supported by Java, but an array of arrays can be declared and created, e.g.: int roomno[][] = new int[3][10];

Sample code : 

51 Sample code

Init multidimensional arrays : 

52 Init multidimensional arrays A multiple-subscripted array in which each row has a different number of columns can be allocated dynamically as follows: int array[][]; array = new int[2][]; // allocate rows array[0] = new int[5]; // allocate columns for row 0 array[1] = new int[3]; // allocate columns for row 1 A multiple-subscripted array can also be allocated directly: int array[][] = { { 1, 2 }, { 3, 4, 5 } };

Sample code : 

53 Sample code

The Application of Arrays : 

54 The Application of Arrays

Bubble sort : 

55 Bubble sort Sorting data (i.e., placing the data into some particular order such as ascending or descending) is one of the most important computing applications. The smaller values gradually “bubble” their way to the top of the array (i.e., toward the first element) like air bubbles rising in water, while the larger values sink to the bottom (end) of the array.

Sample code : 

56 Sample code

Binary search : 

57 Binary search It may be necessary to determine whether an array contains a value that matches a certain key value. The process of locating a particular element value in an array is called searching.

Sample code : 

58 Sample code

Summary : 

59 Summary

Variables : 

60 Variables Three kinds of variables Variables’ initial value Variable name

Primitive data types : 

61 Primitive data types Eight primitive data types Type conversion Type promoting

Arrays : 

62 Arrays Three steps of creating arrays Using new to create array objects Directly initialize array elements

The application of arrays : 

63 The application of arrays Bubble sort Binary search

Thanks! : 

64 Thanks!