Presentation is loading. Please wait.

Presentation is loading. Please wait.

Primitive Types and Expressions

Similar presentations


Presentation on theme: "Primitive Types and Expressions"— Presentation transcript:

1 Primitive Types and Expressions

2 Variables Named containers used to store data such as numbers and letters. The number, letter, or other data item in a variable is called its value. The values held by variables can be changed any number of times while the program is being run. Example: numberOfChildren = 6;

3 Primitive Variable Types
In order to run a program information must be given that tells the computer how much space is required to store each variable and how the variable is to be represented. This information can be determined if the type of the variable is known. Primitive types include int (for integers), char (for characters), and double (for real numbers)

4 Declaring Variables In Java the type of each variable must be declared before it is used. A declaration statement is used for this purpose. It has the form: Type Variable_1, Variable_2, ; Examples: int styleNumber, numberOfChecks; char answer; double amount, interestRate;

5 Java Identifiers The technical term for a name in a programming language, like the name of a variable, is an identifier. An identifier can contain letters, digits, and the underscore character (_), but cannot start with a digit. (No spaces, or other characters like dots (.) can be used. There is no limit to the length of an identifier.

6 Java Identifiers (cont’d)
Java is case sensitive, so the names mystuff, myStuff, and MyStuff are all different. By convention identifiers for primitive types begin with lowercase letters. Also, by convention, in multiword identifiers, the first letter of each word after the first is capitalized, e.g., theTimeOfDay, hotCar, totalNumberOfEggs, etc.

7 Java Identifiers (cont’d)
Some names which are “keywords” in Java like “public”, “class”, “static” cannot be used as identifiers for variables. Some names like “main” or “println” are not reserved keywords but they have predefined meanings. Although technically you can use these names and thereby change their meaning, it is not usually a good idea to do so.

8 Primitive Types Type Name Kind of Value Memory Used byte integer
short 2 bytes Int 4 bytes long 8 bytes float floating-point double char single character boolean true or false 1 bit

9 Assignment Statements
The most direct way to give a variable a value is to use an assignment statement. For example, it answer is a variable of type int and you want to assign it the value of 42, you can write answer = 42; The equal sign, =, is called the assignment operator. It doesn’t really mean equal in the mathematical sense.

10 Examples of Assignment Statements
amount = 3.99; firstInitial = ‘B’; score = numberOfCards + handicap; eggsPerBasket = eggsPerBasket – 2; The statement means: compute the value of the expression to the right of the equal sign and replace the currently stored value of the variable to the left of the equal sign with this new value.

11 Specialized Assignment Operators
The simple assignment operator (=) can be combined with an arithmetic operator like +, -, or * (times). For example amount += 5; means amount = amount + 5; and amount *= 25; means amount = amount*25

12 Simple Screen Output System.out is an object and println is a method of this object that sends output to the screen. System.out.println(amount + “ is the cost.”); outputs the value of the variable amount (to the screen) followed by the phrase “ is the cost.” Notice that the plus sign here means to append the phrase to the amount. It is not an arithmetic operator.

13 Simple Input In order to do keyboard input we must put the following statement at the beginning of the file: import java.util.*; This line tells the Java compiler where to find the definition of the Scanner class that we use for keyboard input. The following line sets things up so that data can be entered form the keyboard: Scanner keyboard = new Scanner(System.in);

14 Simple Input (cont’d) The statement:
eggsPerBasket = keyboard.nextInt(); reads one whole number from the keyboard. When entering numbers at the keyboard, the user must either place numbers on separate lines or else separate multiple numbers by one or more spaces.

15 Constants Character constants are written by placing a single quote mark before and after the character, e.g., ‘B’, ‘#’, ‘z’, etc. Integer constants are written as integers are normally written. They may be preceded with a + or - sign, but they cannot contain a decimal point. Examples are: 77, -5, or +98, but not 4. or 88.0

16 Constants (cont’d) Numbers with decimal points are called floating point numbers and can be written as a series of digits followed by a decimal point and followed by another series of digits, e.g , .0001, 5., or They can also be written in scientific notation using the letter e to denote the exponent, e.g e2 (which is the same as 865.4

17 Assignment Compatibilities
You cannot store a value of one type into a variable of another type unless it is first converted to the same type as that of the variable. Sometimes this conversion is performed automatically by Java. The conversion is automatic as long as the variable receiving the value is further down the following list than the value is: byteshortintlongfloatdouble

18 Type Casting This is a way of converting a value of one type to another type. The code double distance; distance = int points; points = distance; would result in an error

19 Type Casting (cont’d) However, the code
double distance; distance = int points; points = (int)distance; would be OK. In this case distance is converted to an integer before it is placed in points. However, the value is truncated not rounded.

20 Arithmetic Operators In Java you can form arithmetic expressions involving addition (+), subtraction (-), multiplication (*), and division (/). These operators can be used will all integer and floating point types of variables. The result of an expression involving mixed type is the type of the variable farthest to the right on the list: byteshortintlongfloatdouble

21 The % Operator In Java the result of 9.0/2 is 4.5 but the result of 9/2 is 4 The % operator can be used to capture the remainder of the integer division operation. That is, the result of 9%2 is 1

22 Parentheses and Precedence Rules
Parentheses can be used to group items in arithmetic expressions to indicate the order in which the operations are to be performed. For example, (cost + tax) * discount cost + (tax * discount)

23 Parentheses and Precedence Rules (cont’d)
Without parentheses, precedence rules apply. Highest Precedence First: the unary operators: +, -, ++, --, and ! Second: the binary arithmetic operators: *, /, and % Third: the binary arithmetic operators: + and – Lowest Precedence

24 Increment and Decrement Operators
Increment operator (++) count++; is the same as count = count + 1; Decrement operator (--) count--; is the same as count = count – 1;


Download ppt "Primitive Types and Expressions"

Similar presentations


Ads by Google