This preview shows pages 1–2. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: The more intuitive result of "value is: 7" can be accomplished by forcing the addition of the integers to happen ﬁrst,
via a pair of parenthesis "value is: " + (5 + 2) The + operator is often used to concatenate values to Strings in println statements: int rate : 8;
int hours = 40;
int pay = rate * hours; System.out.println("Pay will be $" + pay);
The println will print the string "Pay will be $320" to the output console. Identifiers There are two issues to consider when choosing a name for a variable -- is it a legal name, and does it have good
style? A legal name is one which the compiler is able to successfully translate. The Java rules for creating an
identiﬁer (e. g., a variable name), are that it: 0 can contain upper and lower case letters (a—z and A-Z), - can contain numeric digits (0-9) 0 can contain the special symbols of underscore ('_') and dollar sign ('$’)
- the ﬁrst character of an identiﬁer cannot be a digit - it cannot be a reserved word Therefore, the following are valid identiﬁers - taXRate
- taX$toPay and these are not valid ° 4best (cannot start with a digit)
- three@front (@ is not an allowed character)
° tax me (the blank between "tax" and "me" is not an allowed character -- an identifier is one continuous word) Style is a separate issue. Just because an identiﬁer will compile successfully does not mean it is one that a Java
programmer would ﬁnd acceptable. Java programmers, generally, prefer that variable names use all lower case
letters (including the ﬁrst character of the identifer) and except when starting an interal word (i.e., the ﬁrst letter of an
internal word is upper case). Using digits is generally acceptable, but do not use the underscore or the dollar sign.
Therefore, the following all good identiﬁers, with respect to style, for variable names: 0 taXRate - taxes4December 0 doors - houseSalesBeforeSummer These are generally not considered to be good style for variable names (or at least I do not consider them to be good
style): - taX_rate
- Doors 0 taX$s - house_sales_before_summer
0 _bread The style for class names is the same as for variable names, except class names should always start with an uppercase
letter. Therefore, these are good style for class names: - Houses - FederalTaxes
- Auto - FirTrees and these are not good style for class names: - houses
- federalTAXES - Fir_Trees
Binary Numbers A computer is just a machine, but it is a machine that keeps track of information, such as letters and numbers.
Unfortunately, the computer does not have anything as fancy as a human brain, so keeping track of letters and
numbers is a bit difﬁcult. The computer records information in its memory using rows of bits. A bit is something like
a tiny light bulb which is either on or 015’. In realitiy, a computer bit is a capacitor and a transistor working together --
the capacitor is either storing a small electric charge (i.e., is on) or is not storing a charge (oﬂ). The transistor helps
the process of turning the capacitor on and off, and also is used to read the current state of the capacitor (i.e., it can
check if the capacitor is on or oﬂ). A set of bits can have different patterns of on and oﬂ to represent to different values. A similar system that we see
everyday is a trafﬁc light. A trafﬁc is a row of three light bulbs, and different on/off patterns have different values: on
off means STOP
on means YIELD
off means G0
on On a computer the different on/off patterns indicate different numeric values. The computer puts the bits into small
groups called bytes (a group of eight bits). Each group is given a different on/off pattern to represent a speciﬁc
number. For example, if we look at a byte that has all of the bits turned off: off off off off off off off off means 0 and if only the last transistor turned on off off off off off off off on means 1 If the last two bits are turned on off off off off off off on on means 3 When referring to the value of bits people typically write a 0 for oﬂ and a l for on. So the values shown above could
be written as follows: 0 O O O O O O 0 means 0 ...
View Full Document
This note was uploaded on 11/17/2010 for the course CIS 1500 taught by Professor Staff during the Summer '08 term at Oakland CC.
- Summer '08