Coding conventions
This is the current revision of this page, as edited by 81.200.197.161 (discuss) at 11:34, 27 September 2022 (Removed a wrong / "personal opinion" part of the sentence. A language is not "not 100% object oriented", if it exposes some predefined primitve types to the user for good and clearly defined reasons as in this case. If you want to understand the language design details of Java, read the "Java Language Specification". "Personal opinions" shouldn't be part of a document like this.). The present address (URL) is a permanent link to this version.
- 100% developed as of Dec 31, 2012 Statements
- 100% developed as of Mar 10, 2013 Conditional blocks
- 100% developed as of Mar 10, 2013 Loop blocks
- 100% developed as of May 24, 2013 Boolean expressions
- 100% developed as of Feb 16, 2010 Variables
- 100% developed as of Mar 10, 2013 Primitive Types
- 100% developed as of Mar 10, 2013 Arithmetic expressions
- 100% developed as of May 24, 2013 Literals
- 100% developed as of Mar 10, 2013 Methods
- 100% developed as of May 24, 2013 String
- 100% developed as of Mar 10, 2013 Objects
- 100% developed as of Jul 5, 2012 Packages
- 100% developed as of Mar 10, 2013 Arrays
- 75% developed as of Jan 11, 2013 Mathematical functions
- 75% developed as of Jan 11, 2013 Large numbers
- 75% developed as of Jan 11, 2013 Random numbers
- 100% developed as of Apr 8, 2013 Unicode
- 100% developed as of Apr 8, 2013 Comments
- 100% developed as of Sep 27, 2007 Keywords
- 100% developed as of Aug 6, 2013 Coding conventions
- 0% developed as of Mar 26, 2018 Lambda expressions
The Java code conventions are defined by Oracle in the coding conventions document. In short, these conventions ask the user to use camel case when defining classes, methods, or variables. Classes start with a capital letter and should be nouns, like CalendarDialogView
. For methods, the names should be verbs in imperative form, like getBrakeSystemType
, and should start with a lowercase letter.
It is important to get used to and follow coding conventions, so that code written by multiple programmers will appear the same. Projects may re-define the standard code conventions to better fit their needs. Examples include a list of allowed abbreviations, as these can often make the code difficult to understand for other designers. Documentation should always accompany code.
One example from the coding conventions is how to define a constant. Constants should be written with capital letters in Java, where the words are separated by an underscore ('_') character. In the Java coding conventions, a constant is a static final
field in a class.
The reason for this diversion is that Java discerns between "simple" and "complex" types. These will be handled in detail in the following sections. An example for a simple type is the byte
type. An example for a complex type is a class. A subset of the complex types are classes that cannot be modified after creation, like a String
, which is a concatenation of characters.
For instance, consider the following "constants":
publicclass MotorVehicle{ /** Number of motors */ privatestaticfinalintMOTORS=1; /** Name of a motor */ privatestaticfinalStringMOTOR_NAME="Mercedes V8"; /** The motor object */ privatestaticfinalMotorTHE_MOTOR=newMercedesMotor(); /** * Constructor */ publicMotorVehicle(){ MOTORS=2;// Gives a syntax error as MOTORS has already been assigned a value. THE_MOTOR=newToshibaMotor();// Gives a syntax error as THE_MOTOR has already been assigned a value. MOTOR_NAME.toLowercase();// Does not give a syntax error, because it returns a new String rather than editing the MOTOR_NAME variable. THE_MOTOR.fillFuel(20.5);// Does not give a syntax error, as it changes a variable in the motor object, not the variable itself. } }