For 2021 - online Python 3 training - see ((here)).
Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.
Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Java - basic rules for arithmetic, variables and conversion
Declare - Initialise - use - the sequence for Java variables
• All Java variables must be declared before they are initialised
• All Java variables must be initialised to a value before their current value is used
1. Declare (i.e. define the type of variable and the name
2. Set an initial value
3. use the value that you have stored
rovers = 3;
total = rovers + city;
Full example [here]
You can declare and set initial values all in a single statement:
int rovers = 1, city = 2;
int total = rovers + city;
Full example [here]
Variable names in Java must start with a letter, followed by as many or as few letters, digits, underscores as you like. Capital letters are different to lower case letters - i.e. you may consider the alphabet to have 52 letters in it as far as Java is concerned. You must not use keywords from the language as java variable names - if and class and public are invalid variable names for example. There are conventions too - whether or not to start a name with a capital, whether to make it all capital, or "camel case" where words in the middle are capitalised ... and some of those conventions are assumed to have been followed by certain tools, meaning that you MUST apply the convenion (as if it were a rule) if you want to use the tool.
Basic rules with numbers in Java
Multiplications and Divisions happen before additions and subtractions, and within each of those groups they work left to right - so
3 * 4 + 5 * 6
= .... 12 + 30
= .... 42
3 + 4 * 5 + 6
= .... 3 + 20 + 6
= .... 29
You can add brackets to change the precedence order - brackets are done first - so
(3 + 4) * (5 + 6)
= .... 7 * 11
= .... 77
If you do an integer (whole number) division, the result will be a whole number too:
3 * 4 / 5 + 6
= .... 12 / 5 + 6
= .... 2 + 6
= .... 8
If you save an int
number into a float
or a double
, that's OK and the conversion is done silently for you (it's known as coercion) since it's always possible and there's no loss of precision. The same thing applies when converting from a float
to a double
, from a byte
to a short
, or to an int
or to a long
, and so on.
If you save something into a variable type that will lead to a loss of precision, or if you are trying to store something in a variable type that not all values from the first type could go into, you must give Java permsission by casting or converting. That applies if you're trying to put a double
into a float
, a float
into an int
, or a long
into a short
, for example.
There's an example [here]
showing a compile time failure when I tried to push a double
into a float
. And there's an example [here]
where I got the wrong results because the precendence order was wrong and I did an integer division, throwing away the remainder when I should not have done so.
I have provided two sample average programs that work - [here]
where I have used specifically a 2.0F (i.e. float) constant rather than just 2.0 (defaults to double) and [here]
, where I let the calculation coerce to double precision then cast it to float. A third approach would simply have been to store the result into a double
variable. (written 2010-11-10)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articlesJ703 - Java - Variables 
Conversion and coercion in Java - (2004-11-22) 
Question on division (Java) - Also Perl, PHP, Python ... - (2007-11-28) 
Variable scope in Java Servlets and other web applications - (2009-05-01) 
Class Loading and Variable Conversion in Java - (2009-05-02) 
Setting up individual variables, and arrays, in Java - some commented examples - (2010-11-09) 
Do I need to initialise variables - programming in C, C++, Perl, PHP, Python, Ruby or Java. - (2011-05-05) 
Turning bright delegates into bright and knowledgable ones - (2011-07-21) 
BODMAS - the order a computer evaluates arithmetic expressions - (2012-11-09) 
Learning to program - variables and constants - (2014-11-22) 
Incrementing a variable in Java - Pre and Post Increment - (2014-12-08)
Some other Articles
After Course Resources - do we publish sample answers. Example from Java Exceptions module.Changing a Servlet - more that just editing and compilingGathering information - logging - with log4j. First steps.Least Common Ancestor - what is it, and a Least Common Ancestor algorithm implemented in PerlJava - basic rules for arithmetic, variables and conversionDoing several things at the same time - Java threadsFresh Paint - Java ArraysLooking back and forward personally - 6 years each waySending out an email containing HTML from within a PHP page
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page
This is a page archived from The Horse's Mouth at
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).