school of computer science & information technology g6dicp - lecture 4 variables, data types...
TRANSCRIPT
School of Computer Science & Information Technology
School of Computer Science & Information Technology
G6DICP - Lecture 4G6DICP - Lecture 4
Variables, data types & decision makingVariables, data types & decision making
2
VariablesVariables
Symbolic representation of Symbolic representation of datadata of a specific of a specific typetype Variables are named by an Variables are named by an identifieridentifier..
Type must be declared before a variable can be usedType must be declared before a variable can be used
e.g. int ae.g. int a
Values can be assigned to a variableValues 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 Variables can be modified during a programs execution (usually by assignment).(usually by assignment).
3.3
3
Major Java Variable TypesMajor Java Variable Types
NumericNumeric 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 integerint - 32 bits signed integer long - 64 bits signed integer long - 64 bits signed integer float - 32 bit floating pointfloat - 32 bit floating point double - 64 bit floating point (double precision) double - 64 bit floating point (double precision)
CharacterCharacter char - single characterchar - single character String - text (any length) bounded by double quotesString - text (any length) bounded by double quotes
BooleanBoolean boolean - true or falseboolean - true or false
User DefinedUser Defined
4
Converting TypesConverting Types
Types often need to be converted.Types often need to be converted. Examples:Examples:
IntegerIntegerRealRealRealRealIntegerIntegerStringStringRealReal
eg "3.14" eg "3.14"3.143.14RealRealStringString
5
Converting Types (2)Converting Types (2)
Library methodsLibrary methods CastingCasting
a = (int) b a = (int) b
This forces b to be treated as an int. 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 If it does not, then the compiler mightmight generate an error! generate an error! Information that does not exist in the target type is Information that does not exist in the target type is
lost.lost.
6
DecisionsDecisions
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 A decisiondecision is required is requiredif deposit thenif deposit then add sum to balance add sum to balanceelseelse subtract sum from balance subtract sum from balance
This is one example of a This is one example of a control structurecontrol structure
7
If StatementIf Statement
This statement requires a boolean expression as This statement requires a boolean expression as part of its code.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) {{ ......}}
if (a > b | b == 0) if (a > b | b == 0) {{ ......}}
if (a > b) if (a > b) {{ ......} else} else{{ ......}}
8
Relational OperatorsRelational 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 >= greater or equal
toto<= less or equal to<= less or equal to | or| or& and& and