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 as below, or enter http://melksh.am/nnnn for individual articles
J705 - Java - Arrays [1497] Training Season Starts again! - (2008-01-07)
[1498] Java is a dynamic language .... (and comparison) - (2008-01-08)
[1614] When an array is not an array - (2008-04-17)
[2648] Java arrays - are they true arrays or not? - (2010-02-23)
[3039] Fresh Paint - Java Arrays - (2010-11-09)
[3118] Arrays of arrays - or 2D arrays. How to program tables. - (2011-01-02)
[4347] Arrays in Java - an introduction for newcomers - (2014-12-10)
[4413] Binomial Coefficient (Pascal Triangle) objects in Java - (2015-02-03)
[4428] Using the lead - passing arrays and other collections in Java - (2015-02-16)
J704 - Java - Loops and Conditional Statements [353] Wimbledon Neck - (2005-06-20)
[1220] for loop - how it works (Perl, PHP, Java, C, etc) - (2007-06-06)
[1582] Ruby, C, Java and more - getting out of loops - (2008-03-19)
[1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
[2734] for and foreach in Java - (2010-04-22)
[3200] How a for loop works Java, Perl and other languages - (2011-03-12)
[3397] Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18)
[4322] Learning to Program - the conditional statement (if) - (2014-11-21)
[4323] Learning to program - Loop statements such as while - (2014-11-22)
[4346] A behaviour driven example of writing a Java program - (2014-12-09)
[4427] Java example - for loop and conditionals from course exercise - (2015-02-16)
J703 - Java - Variables [127] Conversion and coercion in Java - (2004-11-22)
[1448] Question on division (Java) - Also Perl, PHP, Python ... - (2007-11-28)
[2148] Variable scope in Java Servlets and other web applications - (2009-05-01)
[2153] Class Loading and Variable Conversion in Java - (2009-05-02)
[3041] Java - basic rules for arithmetic, variables and conversion - (2010-11-10)
[3278] Do I need to initialise variables - programming in C, C++, Perl, PHP, Python, Ruby or Java. - (2011-05-05)
[3365] Turning bright delegates into bright and knowledgable ones - (2011-07-21)
[3917] BODMAS - the order a computer evaluates arithmetic expressions - (2012-11-09)
[4324] Learning to program - variables and constants - (2014-11-22)
[4345] Incrementing a variable in Java - Pre and Post Increment - (2014-12-08)
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?