This is Lesson 1.5 of the Java programming course.
Java syntax and naming conventions
In common with most languages, Java programs must adhere to a certain syntax in order for them to be successfully compiled. There are also certain conventions which ought to be complied with to aid understanding. This course will use the terms must and should cover the most important aspects of syntax and conventions by stating whether you must do something a certain way (because otherwise your program won't compile), or whether you should do it a certain way (in order to adhere to conventions).
An important point to note is that capitalisation is significant in Java; that is; Animal, animal and ANIMAL all refer to different things as far as Java is concerned since they each have different letters capitalised.
Therefore, you must remember to be consistent in your use of capitalisation, and the built-in Java commands and keywords must be used using the correct capitalisation or else they will not be recognised.
There are several rules regarding syntax and conventions, but here are the important ones to get you started:
- The names of your classes should be in CamelCase[1] and should always start with a capital letter (e.g.,,
Animal,Person,TelephoneNumber,HourlyPaidEmployee). - The names of your objects, variables and methods should be in camel-case and should always start with a lower case letter; examples:
- --> object names:
fred,firstAccount,blueBoy,counter. - --> variable names:
address,currentBalance,dateOfBirth. - --> method names:
getName,setCurrentBalance,run. - The names of methods that retrieve and return information (such as the value of a variable) often begin with the word "get" (e.g.,
getName()would return the value of thenameattribute), although there are exceptions to this. Note that the 'n' for name becomes capitalised when appended to the "get". - The names of methods that modify information (such as the value of a variable) often begin with the word "set" (e.g.,
setName), although again there are exceptions to this. - Spaces are not allowed for class, variable or method names.
The next lesson will describe how Java programs are entered.
Comments