Presentation is loading. Please wait.

Presentation is loading. Please wait.

Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.

Similar presentations


Presentation on theme: "Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants."— Presentation transcript:

1 Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants operators and expressions data conversion syllabus basic programming concepts object oriented programming topics in computer science

2 Data representation question: how to represent information in the computer, using the java language? answer: java lets us represent information in 8 different ways these representation formats are called data types

3 Primitive Data Types A data type is defined by a set of values and the operators you can perform on them Each value stored in memory is associated with a particular data type; Java has several predefined types, called primitive data types The following reserved words represent eight different primitive types: byte, short, int, long float, double boolean char

4 Integers There are four separate integer primitive data types; they differ by the amount of memory used to store them Type byte short int long Storage 8 bits 16 bits 32 bits 64 bits Min Value -128 -32,768 -2,147,483,648 < -9 x 1018 Max Value 127 32,767 2,147,483,647 > 9 x 1018

5 reading from memory תא 1: 1 תא 4:

6 Floating Point There are two floating point types: Approximate
Min Value -3.4 x 1038 -1.7 x 10308 Approximate Max Value 3.4 x 1038 1.7 x 10308 Type float double Storage 32 bits 64 bits

7 Characters A char value stores a single character from the Unicode character set A character set is an ordered list of characters The Unicode character set uses 16 bits per character, allowing for 65,536 unique characters It is an international character set, containing symbols and characters from many world languages

8 Characters The ASCII character set is still the basis for many other programming languages ASCII is a subset of Unicode, including: uppercase letters lowercase letters punctuation digits special symbols control characters A B C … a b c … . , ; … & | \ … carriage return, tab, ...

9 Boolean A boolean value represents a true/false condition.
It can also be used to represent any two states, such as a light bulb being on or off The reserved words true and false are the only valid values for a boolean type how many bytes does this datatype use?

10 Variables A variable is an identifier (שם) that represents a storage in memory that holds a particular type of data Variables must be declared before being used; the syntax of a variable declaration is: data-type variable-name; data type variable name int total;

11 variables in memory תא 1: } 1 total תא 4:

12 Details of variable declaration
Multiple variables can be declared on the same line: int total, count, sum; Variables can be initialized (given an initial value) in the declaration: int total = 0, count = 20; double unitPrice = 57.25;

13 Variables use public static void main (String[] args) {
short weeks = 14; int numberOfStudents = 120; double averageFinalGrade = 78.6; System.out.println(weeks); System.out.println(numberOfStudents); System.out.println(averageFinalGrade); }

14 Constants A constant is similar to a variable except that it keeps the same value throughout its existence; Constants are specified using the reserved word final It is better to use constants than literals because: They make the code more readable by giving meaning to a value They facilitate change because the value is only specified in one place Q: assembler languages do not have constants; what do we do?

15 Example - Constants use
// Reads the radius of a circle and prints // its circumference and area class ConstantsExample { static final double PI = ; public static void main(String[] args) { double r, circumference, area; System.out.println(“Enter radius: “); r = EasyInput.readDouble (); circumference = 2*PI*r; area = PI*r*r; System.out.println(“Circumference: “ +circumference); System.out.println(“Area: “ + area); }

16 Assignment Statements
An assignment statement takes the following form: variable-name = expression; The expression is evaluated and the result is stored in the variable, overwriting the value currently stored in the variable

17 Assignment Statements: example
// Uses assignment to change a variable's value public static void main (String[] args) { int NumberOfStudents = 140; System.out.println(“Students in 2001:”); System.out.println(NumberOfStudents); NumberOfStudents = 170; System.out.println(" Students in 2000:”); }

18 Operators An operator is a mapping that maps one or more values to a single value examples: +, -, *, / Java operators can be either: Unary operators - takes a single value (e.g., -) Binary operators - takes two values (e.g., +) All Java binary operators are written in the infix notation: operand1 operator operand2

19 Operators arithmetic increment and decrement logical assignment
conditional

20 Arithmetic operators Java defines 5 arithmetic operators that are valid between every two numerical types a + b add a and b a - b subtract b from a a * b multiply a and b a / b divide a by b a % b the remainder of dividing a by b

21 Operators Operators can act differently on different data types
5.0 / 2.0 5 / 2.0 5.0 / 2 5 / 2 Essentially these are different operators 5.0 / 2.0 5 / 5.0 / 2 5 / 2 5.0 / 5 / 2.0 5.0 / 2 5 / 2 5.0 / 2.0 5 / 2.0 5.0 / 5 / 2 5.0 / 2.0 5 / 2.0 5.0 / 2 5 /

22 Expressions An expression can consist of a combination of operators and operands Operands can be literal values, variables, or expressions by themselves Examples of expressions: 4 + 5 x * 2.73 a - (7 - b) x

23 Expression example public static void main (String[] args) {
int numberOfBooks = 30; double bookPrice = 45.90; double totalPrice; totalPrice = numberOfBooks * bookPrice; System.out.println( “The total price is:”); System.out.println(totalPrice); }

24 Operator Precedence The order in which operands are evaluated in an expression is determined by a well-defined precedence hierarchy Operators at the same level of precedence are evaluated according to their associativity (right to left or left to right) Parentheses can be used to force precedence

25 Operator Precedence Multiplication, division, and remainder have a higher precedence than addition and subtraction Both groups associate left to right Expression: Order of evaluation: Result: / % 3 3 1 4 2 6

26 Operator Precedence What is the order of evaluation in the following expressions? a + b + c + d + e a + b * c - d / e 1 2 3 4 3 1 4 2 a / (b + c) - d % e 2 1 4 3 a / (b * (c + (d - e))) 4 3 2 1

27 Assignment Revisited The assignment operator has a lower precedence than the arithmetic operators First the expression on the right hand side of the = operator is evaluated answer = sum / 4 + MAX * lowest; 4 1 3 2 Then the result is stored in the variable on the left hand side

28 Assignment Revisited The right and left hand sides of an assignment statement can contain the same variable First, one is added to the original value of count count = count + 1; Then the result is stored back into count (overwriting the original value)

29 String Concatenation The ‘+’ operator between strings has the meaning of String concatenation When we apply ‘+’ upon a String and a value of another type, that value is first converted into a String and the result is the concatenation of the two Strings

30 String Concatenation Output: public static void main(String[] args) {
System.out.print(“The international “ + “dialing code”); System.out.println(“for Israel is “ + 972); } Output: The international dialog code for Israel is 972

31 Data Conversions Sometimes it is convenient to convert data from one type to another; e.g., we may want to treat an integer as a floating point value during a computation Conversions must be handled carefully to avoid losing information: Widening conversions are safest because they tend to go from a specific data type to a general one (such as int to float) Narrowing conversions can lose information because they tend to go from a general data type to a more specific one (such as float to int)

32 Data Conversions In Java, data conversions can occur in three ways:
Assignment conversion occurs when a value of one type is assigned to a variable of another; only widening conversions can happen via assignment Arithmetic promotion happens automatically when operators in expressions convert their operands Casting

33 Data Conversions: casting
Casting is the most powerful, and dangerous, technique for conversion Both widening and narrowing conversions can be accomplished by explicitly casting a value To cast, the type is put in parentheses in front of the value being converted int total, count; result = (float) total / count;

34 The Increment and Decrement Operators
The increment operator (++) adds one to its integer or floating point operand The decrement operator (--) subtracts one The statement count++; is essentially equivalent to count = count + 1;

35 The Increment and Decrement Operators
The increment and decrement operators can be applied in prefix (before the variable) or postfix (after the variable) form When used alone in a statement, the prefix and postfix forms are basically equivalent. That is, count++; is equivalent to ++count;

36 The Increment and Decrement Operators
When used in a larger expression, the prefix and postfix forms have a different effect In both cases the variable is incremented (decremented) But the value used in the larger expression depends on the form Expression count++ ++count count-- --count Operation add 1 subtract 1 Value of Expression old value new value

37 The Increment and Decrement Operators
If count currently contains 45, then total = count++; assigns 45 to total and 46 to count total = ++count; assigns the value 46 to both total and count

38 The Increment and Decrement Operators
If sum contains 25, then the statement System.out.println (sum++ + " " + ++sum + " " + sum + " " + sum--); prints the following result: and sum contains after the line is complete 25 27 27 27 26

39 Assignment Operators Often we perform an operation on a variable, then store the result back into that variable Java provides assignment operators that simplify that process For example, the statement sum += value; is equivalent to sum = sum + value;

40 Assignment Operators There are many such assignment operators, always written as op= , such as: Operator += -= *= /= %= Example x += y x -= y x *= y x /= y x %= y Equivalent To x = x + y x = x - y x = x * y x = x / y x = x % y

41 Assignment Operators The right hand side of an assignment operator can be a complete expression The entire right-hand expression is evaluated first, then combined with the additional operation Therefore result /= (total-MIN) % n; is equivalent to result = result / ((total-MIN) % n);

42 How many different ways to do...
int x=0,y=0;  x=1; y=2; int x=1;y=1;  x=2; y=2;

43 What you should be able to do...
write programs which have variables perform simple arithmetic operations and store the output value for further use


Download ppt "Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants."

Similar presentations


Ads by Google