Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

Similar presentations

Presentation on theme: "1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,"— Presentation transcript:

1 1 Variables and Data Types

2 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed, and retrieved Using variables establish its data type and initial value (declaration) set/change its value (assignment, input) use/display its value (expressions, output)

3 3 Example: Using Variables int dimecount;// declaration double dimevalue = 0.10;// declaration with initial value double totalvalue; dimecount = Input.readInt(); // input (assignment) totalvalue = dimecount*dimevalue; // assignment System.out.println(totalvalue); // output

4 4 0.10 5.30 53 dimevalue dimecount totalvalue dimecount = Input.readInt(); totalvalue = dimecount*dimevalue; INPUT OUTPUT System.out.println(totalvalue);

5 5 Back to Java Program Structure A Java program (application or applet) is a class that consists of methods main(), init(), paint(), action(), setup(), onButtonPressed(), … Each method has a body delimited by { } consists of a sequence of statements (including declarations)

6 6 Statements Declarations int dimecount; double dimevalue = 0.10; Assignment statements dimecount = Input.readInt(); totalvalue = dimecount*dimevalue; Output statements System.out.println(totalvalue);

7 7 Identifier A name in a Java program used for variables, classes, methods,... Rules in forming an identifier: consists of letters, digits, and underscores (_) should start with letter or underscore Examples: ateneo score5 total_credit BigBlue _one4three x if Some identifiers are reserved words

8 8 Data Type Describes a domain or pool of values Helps a compiler impose rules Programming language needs rules for constructing literals for a given data type e.g., 234 is an integer literal, ‘A’ is a character literal, 2.1e-3 is a double floating point literal Some primitive data types in Java: int, char, float, double, boolean

9 9 Understanding Data Types Important components of a data type: Range of values Literals Possible operations

10 10 The int Data Type Range: -2,147,483,648 to 2,147,483,647 applies to all system platforms Literals sequence of digits Examples: 22, 16, 1, 426, 0, 12900 Operations: usual arithmetic operations +, -, *, /, % negative numbers obtained using - as prefix

11 11 The double Data Type Values: decimal numbers Range: 4.94e-324 to 1.80e+308 precision: n.nnnnn... X 10 (+/-)mmm Literals (examples) 100.5, 0.33333, 200000.0 -8E10 (-80000000000), 2.1e-3 (0.0021) Operations: arithmetic ops (division?) float: lower precision

12 12 Constants Literal values in a program appear often enough and may be associated with an appropriate name declare as a “variable with a fixed value” Examples public static final int MAX = 100; public static final double PI = 3.1415926; public static final double DIMEVALUE = 0.10;

13 13 Input and Output

14 14 I/O in Java Text output in Java System.out.print & System.out.println Input in pure Java is not straightforward need to handle exception cases uses notions of streams and files Trend in current applications perform I/O through visual components GUIs

15 15 A “home made” class designed to make console input simpler For use in Java applications make sure that is in your working directory use for text input of ints/doubles Input.readInt(), Input.readDouble()

16 16 Input Statements are Assignment Statements Examples: double interestRate;... int count = Input.readInt();

17 17 Applets To create applets need to process GUI events need an init() method to set up visual objects, an action() method to specify associated actions use InputOutputApplet extend InputOutputApplet instead of Applet define setup() and onButtonPressed()

18 18 Using InputOutputApplet Make sure InputOutputApplet.class is present in your directory In the setup() method addInput(“name”) to add input objects addButton(“label”) to add a button addOutput() to add an output area In the onButtonPressed method getInt(), getDouble() for retrieving data print(), println() for printing on output area

19 19 Operators and Expressions

20 20 Operators in Java Arithmetic operators +, -, *, /, % Special operators (, ) performs grouping = (assignment) Other operators

21 21 Understanding Operators Operands count (binary/unary) type Calculation performed value (and type) returned Other effects

22 22 Example: % Binary operation Both operands are ints Returns the (int) remainder when left operand is divided by the right operand No additional effects

23 23 Another Example: = Binary operation Left operand must be a variable Returns the value of the right operand Effect: value of right operand is assigned to left operand * Note that a = b = c = 0; is valid

24 24 Other Operators Increment and decrement operators ++, -- post- or pre- Assignment operators +=, -=, *=, /=, … “Built-in” Functions not really operators (but similar) Math.abs(), Math.sqrt(), Math.pow(),...

25 25 Post-increment Operator: ++ Example: number++ Unary operator Operand must be a variable Returns the (original) value of the operand Effect: variable is incremented

26 26 Pre-increment Operator: ++ Example: ++number Unary operator Operand must be a variable Returns the incremented value of the operand Effect: variable is incremented

27 27 About ++ Notice that a++; and ++a; are virtually equivalent return value is ignored in both cases could be used as shorthands for a = a+1; Distinction apparent when the return value is useda = 5; b = a++;b = ++a;// values of a & b?

28 28 Decrement Operator: -- Analogous definitions for Post-decrementnumber-- Pre-decrement--number

29 29 Assignment Operators There is a shorthand for constructs such as sum = sum + number; sum += number; += is an operator: such an operator exists for virtually every arithmetic operator +=, -=, *=. /=, %=,... effect: variable is updated returned value: the updated value

30 30 Built-in Functions Provided in Java to provide for more complex operations Example: Math.pow() double result = Math.pow(5.5,3.0); can be viewed as a binary operation that calculates some power of a number javap java.lang.Math prints a list of available math functions

31 31 Operand Types vs Result Type There are cases where the type of the result differs from the types of the operands Examples division between an int and a double returns a double Math.round() has an operand (argument) that is a float but returns an int

32 32 Expressions Expression sequence of variables, literals, operators, and function calls Uses right operand of an assignment argument for System.out.println() Expression-statement an expression terminated by a semicolon

33 33 Strings

34 34 Variables Revisited A variable holds a value A variable may instead contain a reference 5 “Hello” int num = 5; num String s = “Hello”; s

35 35 String A special kind of data type called a class allows for string objects About Strings sequences of characters (letters, digits, etc) literals: formed by delimiting the sequence of characters with " " operations?

36 36 Operations on Strings Concatenation “Hello” + “ there” equals “Hello there” Obtain length of string String s = “Hello”; int len = s.length(); // len = 5 Obtain a substring String s = “Hello”; String t = s.substring(0,3); // t = “Hel”

Download ppt "1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,"

Similar presentations

Ads by Google