Download presentation
Presentation is loading. Please wait.
Published byChristina Barnett Modified over 9 years ago
1
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making
2
2 Variables Symbolic representation of data of a specific type Symbolic representation of data of a specific type Variables are named by an identifier. Variables are named by an identifier. Type must be declared before a variable can be used Type must be declared before a variable can be used e.g. int a e.g. int a Values can be assigned to a variable Values can be assigned to a variable Java assignment is = Java assignment is = eg a = b; eg a = b; Variables can be modified during a programs execution (usually by assignment). Variables can be modified during a programs execution (usually by assignment). 3.3
3
3 Major Java Variable Types Numeric Numeric byte - 8 bits signed integer (ie from -128 to 127) byte - 8 bits signed integer (ie from -128 to 127) short - 16 bits signed integer (ie from -32768..32767) short - 16 bits signed integer (ie from -32768..32767) int - 32 bits signed integer int - 32 bits signed integer long - 64 bits signed integer long - 64 bits signed integer float - 32 bit floating point float - 32 bit floating point double - 64 bit floating point (double precision) double - 64 bit floating point (double precision) Character Character char - single character char - single character String - text (any length) bounded by double quotes String - text (any length) bounded by double quotes Boolean Boolean boolean - true or false boolean - true or false User Defined User Defined
4
4 Converting Types Types often need to be converted. Types often need to be converted. Examples: Examples: Integer Real Integer Real Real Integer Real Integer String Real eg "3.14" 3.14 String Real eg "3.14" 3.14 Real String Real String
5
5 Converting Types (2) Library methods Library methods Casting a = (int) b This forces b to be treated as an int. Casting a = (int) b This forces b to be treated as an int. Syntactically any type can be cast into any other type. Syntactically any type can be cast into any other type. Be careful that it makes sense! Be careful that it makes sense! If it does not, then the compiler might generate an error! If it does not, then the compiler might generate an error! Information that does not exist in the target type is lost. Information that does not exist in the target type is lost.
6
6 Decisions Bank Account Bank Account Withdrawal - subtract sum from balance. Withdrawal - subtract sum from balance. Deposit - add sum to balance. Deposit - add sum to balance. A decision is required if deposit then add sum to balance else subtract sum from balance A decision is required if deposit then add sum to balance else subtract sum from balance This is one example of a control structure This is one example of a control structure
7
7 If Statement This statement requires a boolean expression as part of its code. This statement requires a boolean expression as part of its code. For example compare numeric variables a and b. For example compare numeric variables a and b. if (a > b) {......} if (a > b | b == 0) {......} if (a > b) {...... } else {......}
8
8 Relational Operators > greater than > greater than < less than < less than == is equal to == is equal to != is not equal to != is not equal to >= greater or equal to >= greater or equal to <= less or equal to <= less or equal to | or | or & and & and
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.