Presentation is loading. Please wait.

Presentation is loading. Please wait.

MIT AITI 2003 - Lecture 2 The of Java In the following lectures you will learn the basic structures that make up the Java programming language: »Variables.

Similar presentations


Presentation on theme: "MIT AITI 2003 - Lecture 2 The of Java In the following lectures you will learn the basic structures that make up the Java programming language: »Variables."— Presentation transcript:

1 MIT AITI 2003 - Lecture 2 The of Java In the following lectures you will learn the basic structures that make up the Java programming language: »Variables and Data Types »Operators »Control Structures »Arrays and Vectors

2 Variables and Primitive Data Types MIT AITI - 2003

3 Variables

4 What is a Variable? Variables are a place where information can be stored while a program is running Their value can be changed at any point in the program In order to create a variable, you must give it a name and identify what type of information it will store

5 Creating Variables You must create the variable by declaring its name and the type of information it will store The type is listed first, followed by the name For example: int highScore ; type name

6 More examples: String userName ; booleangameOver ; bytesmallerValue ; double testGrade ; You’ll learn more about the types used in these examples in the following lectures

7 Naming Variables The name that you choose for a variable is called an identifier An identifier can be of any length, but must start with: - a letter (a – z) - an underscore ( _ ) - or a dollar sign ($) The rest of the identifier can include any character except those used as operators in java such as +, -, *. You’ll learn more about operators in following lectures

8 In addition, there are certain keywords reserved in the Java language which can never be used as identifiers. Here is a table of the keywords: abstractassertbooleanbreakbyte casecatchcharclassconst continuedefaultdodoubleelse extendsfinalfinallyfloatfor gotoifimplementsimportinstanceof intinterfacelongnativenew packageprivateprotectedpublicreturn shortstaticstrictfpsuperswitch synchronizedthisthrowthrowstransient tryvoidviolatewhile

9 Java is a case sensitive language – the capitalization of letters must be consistent. A rose is not a Rose is not a ROSE It is good practice to select variable names that give a good indication of the sort of data they hold –For example, if you want to record the size of a hat, hatSize is a good choice for a name whereas qqq would be a bad choice

10 When naming a variable, the following convention is commonly used: –The first letter of a variable name is lowercase –Each successive word in the variable name begins with a capital letter –All other letters are lowercase Here are some examples: pageCount loadFile anyString threeWordVariable

11 POP QUIZ Which of the following are valid variable names? 1)$amount 2)6tally 3)my*Name 4)salary 5)_score 6)first Name 7)total#

12 Statements A statement is a simple command written in a programming language that causes something to happen. All statements in Java are separated by semicolons ; Some statements produce a value and are known as expressions

13 Variables and Statements You use statements to create variables and assign values to them One way is to declare a variable and then assign a value to it with two statements int e; //here we are declaring a variable e = 5; //here we are assigning a value to a declared variable Another way is to write a single definition statement int e = 5; //here we are declaring AND assigning

14 Java is a Strongly-Typed Language This means that it is a language in which each type of data is predefined as part of the programming language ALL variables defined in a program must be declared with one of the data types. Certain operations may be allowable only with certain data types. The language compiler enforces the data typing.

15 Primitive Data Types

16 There are eight built-in (primitive) data types in the Java language – 4 integer types ( byte, short, int, long ) – 2 floating point types ( float, double ) – Boolean ( boolean ) – Character ( char )

17 Integer Data Types There are four data types that can be used to store integers. The one you choose to use depends on the size of the number that we want to store. Data TypeDescription byte Variables of this kind can have a value from: -128 to +127 and occupy 8 bits in memory short Variables of this kind can have a value from: -32768 to +32767 and occupy 16 bits in memory int Variables of this kind can have a value from: -2147483648 to +2147483647 and occupy 32 bits in memory long Variables of this kind can have a value from: -9223372036854775808 to +9223372036854775807 and occupy 64 bits in memory

18 Here are some examples of when you would want to use integer types: –byte smallValue; smallValue = -55; –short pageCount = 1250; –long bigValue = 823337123351231513L;

19 Note: In the last example we named a long integer type and the value had an L at the end: 823337123351231513L That is because: if the number had been small enough to be counted as an int type instead, then the program would have automatically considered it an int instead of a long By adding an L to the end of the value, the program is “forced” to consider the value to be of a type long even if it was small enough to be an int

20 Floating Point Data Types There are two data types that can be used to store decimal values. The one you choose to use depends on the size of the number that we want to store. Data TypeDescription float Variables of this kind can have a value from: 1.4e(-45) to 3.4e(+38) double Variables of this kind can have a value from: 4.9e(-324) to 1.7e(+308)

21 Here are some examples of when you would want to use floating point types: –doubleg = 7.7e100 ; –doubletinyNumber = 5.82e-203; –floatcostOfBook = 49.99F; Note: In the last example we added an F to the end of the value. Without the F, it would have automatically been considered a double instead.

22 Boolean Data Type There is a data type that can be used in situations where there are two options, either true or false. These are called booleans. For example: –boolean monsterHungry = true; –boolean fileOpen = false;

23 Character Data Types There is a data type that can be used to store information for single characters such as letters, numbers, punctuation, and other symbols. For example: –char firstLetterOfName = ‘e’ ; –char myQuestion = ‘?’ ; Note that you need to use singular quotation marks when assigning char data types. Otherwise the program might look for another variable name instead of a letter, or assign an int type instead of a number

24 Introduction to Strings To store an entire string of characters as information, Java makes use of a “string”. Strings consist of a series of characters inside double quotation marks, as in the following statements: –String coAuthor = “Yaron Binur” ; –String password = “swordfish786” ; You will learn more about strings and their uses in following lectures

25 POP QUIZ What data types would you use to store the following types of information?: 1)The number of students in the room 2)Location of a point on screen 3)Speed of light 4)Open/Closed status of a file 5)The first letter of your name 6)$237.66


Download ppt "MIT AITI 2003 - Lecture 2 The of Java In the following lectures you will learn the basic structures that make up the Java programming language: »Variables."

Similar presentations


Ads by Google