Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Stream processing wikipedia , lookup
Object-oriented programming wikipedia , lookup
Reactive programming wikipedia , lookup
Abstraction (computer science) wikipedia , lookup
Data-intensive computing wikipedia , lookup
C Sharp syntax wikipedia , lookup
Go (programming language) wikipedia , lookup
Introductory Concepts in Programming 1.1 Representing Data Data representation Computers have changed a lot from the mainframes (big irons) of the 1950 to laptops and mobile devices, which are prevalent today. Not only have they changed in size, but advances in technology have greatly enhanced user experience through better graphical user interface and interaction design. One thing that has not changed though is the way data is represented in computers. Traditionally, computer data is represented using two states—0 and 1—in a binary (or base 2) system. Each of these is known as a bit—a contraction of binary digit. Computers are binary systems which use sequences of 0s and 1s to represent values. A sequence of 8 bits is known as a byte, from where we get the following units of data representation: kilobyte, megabyte, gigabyte, terabyte, etc. The value of each of these can be represented as a power of 2 (since it is a binary number) as follows: Unit kilobyte megabyte gigabyte terabyte Number of Bytes 210 = 1024 220 (over 1 million) 230 (over 1 billion) 240 (over 1 trillion) Symbol KB MB GB TB The Java programming language also uses hexadecimal (base 16) and octal (base 8) numbers. For more details on number systems, see Mathematics 1. Types and identifiers The Java programming language uses variables to represent values. A variable name refers to the specific location in memory where the value is stored. This could be visualised as follows, if we assign the value 3 to the variable Total. int Total = 3; Total 3 Every variable has a data type, which determines the values that could be stored in it. Data types in Java fall into two main categories: primitive and class. Class types will be discussed in week 6. 1 Introductory Concepts in Programming There are eight primitive (or built-in) data types in Java. Four of these are used to represent integers—byte, short, int, long Two are used for real (floating point) numbers—double, float One is the character data type—char The final one is used for representing a boolean values—boolean A variable must declared as a specific data type before it is used. For example, the code below prints out the number of sides in a regular polygon. The name of the variable is sides and its data type is int. The highlighted line shows how to make a declaration in Java; comments are included after the pair of forward slashes (//). public class PolygonSides { public static void main (String[] args) { int sides; // declaration of the variable sides sides = 7; // initialization of variable sides System.out.println ("A heptagon has " + sides + " sides"); sides = 10; // sides is assiged a new value System.out.println ("A decagon has " + sides + " sides"); sides = 12; // sides is assiged a another value System.out.println ("A dodecagon has " + sides + " sides"); } } Here is what the output looks like once the code is compiled and run. A heptagon has 7 sides A decagon has 10 sides A dodecagon has 12 sides 2 Introductory Concepts in Programming Variables of different types require different memory sizes. The data type byte uses one byte (eight bits) of storage. 0 1 1 1 1 1 1 1 It is important to determine at the start what data type is needed to ensure that the values of the variables will fit correctly at all stages of the program. The table below shows the sizes and ranges of the primitive data types. Type byte short int long float Storage size (in bytes) 1 2 4 8 4 double 8 boolean char 1 2 Range -27 to 27 (-128 to 127) -215 to 215 (-32,768 to 32,767) -231 to 231 -263 to 263 a single-precision 32-bit IEEE 754 floating point (-3.4x1038 to 3.4x1038 approximately) a double-precision 64-bit IEEE 754 floating point (-1.8x10308 to 1.8x10308 approximately) true or false Unicode character set (includes ASCII) Information on when each of these data types should be used can be found here. Final variables The keyword final is used to ensure that the quantity of a variable cannot be changed. Java programmers traditionally use all upper case with words separated by an underscore for naming constants. For example, we can assume that the income tax rates are fixed values, and so it makes sense to define these as constants. final double BASIC_RATE = 0.2; // initialization and declaration of the constant BASIC_RATE final double HIGHER_RATE = 0.4; // initialization and declaration of the constant HIGHER_RATE 3