In Java, variables must be defined before they have an initial value assigned to them, and they must have an initial value assigned to them before the value thay contain is used; there's no automatic assumption that a new name is a variable, nor is there an assumption made of what type of data a variable can contain.
[Here]'s an example of a "first day program" that I wrote on yesterday's
Starting Java Course showing this setup of variables, calculations using them, and output. Keyboard input in Java isn't straightforward, so I used a class of my own to provide a method which hides the complicated bits of that stage within in - and that's
[here].
Following of from that example for newcomers to coding, I provided an example with a loop -
[here] - showing the same principle in use, but now with a repeating block of code too. Note - in both of theses examples - how vital it is to have to code well commented to make it followable!
Arrays similarly need to be defined (telling Java what type of data they'll contain) and their length needs to be set up / memory allocated too via the
new keyword. Arrays in Java start at position number 0, so an array with 7 members has elements numbered 0 through to 6 inclusive (it's very easy for this to confuse you!)
You'll want to set up an array, you'll want to fill it with values. You'll want to echo those values out, find the lowest and highest values, and do a lot more. I've just written an (over)commented example
[here] so you can see how many of those things are done ... no longer on the first day of the course and just for newcomers to Java ... we're now into the
Java Bootcamp Course!.
(written 2010-11-09)
Associated topics are indexed under
J704 - Java - Loops and Conditional Statements [3397] Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18)
[3200] How a for loop works Java, Perl and other languages - (2011-03-12)
[2734] for and foreach in Java - (2010-04-22)
[1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
[1582] Ruby, C, Java and more - getting out of loops - (2008-03-19)
[1220] for loop - how it works (Perl, PHP, Java, C, etc) - (2007-06-06)
[353] Wimbledon Neck - (2005-06-20)
J703 - Java - Variables [3917] BODMAS - the order a computer evaluates arithmetic expressions - (2012-11-09)
[3365] Turning bright delegates into bright and knowledgable ones - (2011-07-21)
[3278] Do I need to initialise variables - programming in C, C++, Perl, PHP, Python, Ruby or Java. - (2011-05-05)
[3041] Java - basic rules for arithmetic, variables and conversion - (2010-11-10)
[2153] Class Loading and Variable Conversion in Java - (2009-05-02)
[2148] Variable scope in Java Servlets and other web applications - (2009-05-01)
[1448] Question on division (Java) - Also Perl, PHP, Python ... - (2007-11-28)
[127] Conversion and coercion in Java - (2004-11-22)
J705 - Java - Arrays [3118] Arrays of arrays - or 2D arrays. How to program tables. - (2011-01-02)
[3039] Fresh Paint - Java Arrays - (2010-11-09)
[2648] Java arrays - are they true arrays or not? - (2010-02-23)
[1614] When an array is not an array - (2008-04-17)
[1498] Java is a dynamic language .... (and comparison) - (2008-01-08)
[1497] Training Season Starts again! - (2008-01-07)
55d2
Some other Articles
Least Common Ancestor - what is it, and a Least Common Ancestor algorithm implemented in PerlDoing several things at the same time - Java threadsSetting up individual variables, and arrays, in Java - some commented examplesLooking back and forward personally - 6 years each waySending out an email containing HTML from within a PHP pageHow to display information from a database within a web pageBirth Notice - Aeryn Cassandra EllisRail services back to Radstock?