25
Programming techniques Lec 6 Data types

Programming techniques

  • Upload
    garry

  • View
    18

  • Download
    0

Embed Size (px)

DESCRIPTION

Programming techniques. Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together with a set of operations for creating and manipulating them DT= class of Dos+ Set of Operations. - PowerPoint PPT Presentation

Citation preview

Page 1: Programming techniques

Programming techniques

Lec 6

Data types

Page 2: Programming techniques

•Variable: Its data object that is defined and named by the programmer explicitly in a program.•Data Types:It’s a class of Dos together with a set of operations for creating and manipulating them•DT= class of Dos+ Set of Operations

Page 3: Programming techniques

There are two types of operations1-primitive Operations: They are specified as part of the language definition.2-Programmer - defined Operations:- They are defined in form of subprogram or method declarations as part of class definitions.An Operation is a mathematical function for a given input argument it has a well-defined and uniquely determined result. 

Page 4: Programming techniques

•The domain of operation is the set of possible input argument which it is defined.•The rang of Operation is set of possible results that it may produce.•The action of the Operation defines the result produced for any given set of arguments.•The algorithm is a Common method for specifying the action of an Operation .

Page 5: Programming techniques

The Basic elements of a specification of a DT are:1-The attributes that distinguish data object of that type.2- The values that Dos of that type may have.3- The Operations that define the possible manipulations of Dos of that type.Example // The Specification of an array data typeAttributes: the number of dimensions, the subscript rang for each dimension.Values: The set of numbers that from valid values for array components.Operations: include subscripting to select in divided array compounds and possibly. Other operations to create arrays, to change their shape , to access attributes such as upper and lower bounds of subscripts, and perform arithmetic on pairs of arrays.

Page 6: Programming techniques

The basic elements of the implementation of a data type1- The storage representation that is used to represent the Dos of the DT in the computer during program execution.2- The algorithms or procedures that manipulate the chosen storage representation of the Dos.

Page 7: Programming techniques

Declarations-The name of DO.-The type of DO.-The life time of DO.-The information about operations.Declaration is a program statement that serves to communicate to the language translator information about the name and type of Dos needed during program execution.Purposes of Declerations1-choice of storage representation.2-Storage management (life time)3-Polymorphic operations (the same Symbol for multi Operation). 

Page 8: Programming techniques

Java Data types

•Primitive Data Types•Reference/Object Data Types

•1. Primitive Data Types:•There are eight primitive data types

supported by Java. Primitive data types are predefined by the language and named by a keyword .

Page 9: Programming techniques

byte:Byte data type is an 8-bit signed two's complement integer.Minimum value is -128 (-2^7)Maximum value is 127 (inclusive)(2^7 -1)Default value is 0Byte data type is used to save space in large arrays, mainly in place of integers, since a byte is four times smaller than an int.Example: byte a = 100, byte b = -50 

 

Page 10: Programming techniques

short:Short data type is a 16-bit signed two's complement integer.Minimum value is -32,768 (-2^15)Maximum value is 32,767 (inclusive) (2^15 -1)Short data type can also be used to save memory as byte data type. A short is 2 times smaller than an intDefault value is 0.Example: short s = 10000, short r = -20000

Page 11: Programming techniques

•int:•Int data type is a 32-bit signed two's complement integer.•Minimum value is - 2,147,483,648.(-2^31)•Maximum value is 2,147,483,647(inclusive).(2^31 -1)•Int is generally used as the default data type for integral values unless there is a concern about memory.•The default value is 0.•Example: int a = 100000, int b = -200000• •long :•Long data type is a 64-bit signed two's complement integer.•Minimum value is -9,223,372,036,854,775,808.(-2^63)•Maximum value is 9,223,372,036,854,775,807 (inclusive). (2^63 -1)•T his type is used when a wider range than int is needed.•Default value is 0L.•Example: long a = 100000L, int b = -200000L• 

Page 12: Programming techniques

float:Float data type is a sing le-precision 32-bit IEEE 754 floating point.Float is mainly used to save memory in large arrays of floating point numbers.Default value is 0.0f.Float data type is never used for precise values such as currency.Example: float f1 = 234.5f double:double data type is a double-precision 64-bit IEEE 754 floating point.This data type is generally used as the default data type for decimal values, generally the default choice.Double data type should never be used for precise values such as currency.Default value is 0.0d.Example: double d1 = 123.4

Page 13: Programming techniques

boolean:Boolean data type represents one bit of information. There are only two possible values: true and false.This data type is used for simple flags that track true/false conditions.Default value is false.Example: boolean one = true char:Char data type is a sing le 16-bit Unicode character.Minimum value is '\u0000' (or 0).Maximum value is '\uffff' (or 65,535 inclusive).Char data type is used to store any character.Example: char letterA ='A'

Page 14: Programming techniques

. Reference Data Types:Reference variables are created using defined constructors of the classes. They are used to access objects. These variables are declared to be of a specific type that cannot be changed. For example,Employee,

Page 15: Programming techniques

Java Literals:A literal is a source code representation of a fixed value. They are represented directly in the code without anycomputation.Literals can be assigned to any primitive type variable. For example:Byte a = 68;char a = 'A' int decimal = 100;int octal = 0144;int hexa= 0x64;

Page 16: Programming techniques

String literals in Java are specified like they are in most other languages by enclosing a sequence of charactersbetween a pair of double quotes. Examples of string literals are:"Hello World""two\nlines""\"This is in quotes\"" String and char types of literals can contain any Unicode characters. For example:Char a = '\u0001';String a = "\u0001";

Page 17: Programming techniques
Page 18: Programming techniques

JAVA - ARRAYS

Java provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

Page 19: Programming techniques

Example:The following code snippets are examples of this syntax:

Creating Arrays:You can create an array by using the new operator with the following syntax:

Page 20: Programming techniques

The above statement does two things:It creates an array using new data Type[arraySize];It assigns the reference of the newly created array to the variable arrayRefVar.

Page 21: Programming techniques

Example:Following statement declares an array variable, myList, creates an array of 10 elements of double type and assigns its reference to myList:

Page 22: Programming techniques

Processing Arrays:

Example:Here is a complete example of showing how to create, initialize and process arrays:

Page 23: Programming techniques
Page 24: Programming techniques

Passing Arrays to Methods:Just as you can pass primitive type values to methods, you can also pass arrays to methods. For example, the following method displays the elements in an int array:

You can invoke it by passing an array. For example, the following statement invokes the printArray method to display 3, 1, 2, 6, 4, and 2:

Page 25: Programming techniques

Returning an Array from a Method:A method may also return an array. For example, the method shown below returns an array that is the reversal ofanother array: