You have seen most of Java building blocks now. They are:
- Project: the top level structure which might have several classes. You can also call a project a package because it typically holds multiple related classes;
- Class: a class is part of a project / package and may have multiple functions;
- Function: we have seen standard functions such as Math.random(); later on, we will learn how to create our own functions. A function may have multiple variables.
- Variable: a variable is part of a function and may have multiple values. A variable is of a certain type such as or . A variable can only store values of that type, for instance is wrong because is notis correct.
int String int x = 1.0;1.0 int. int x = 1 ;
B. Java Naming Conventions:
All identifiers should be named appropriately so the program is easy to understand and maintain. Below are the conventions:
- Names must start with a letter or the underscore '_' , and the remaining characters must be letters, numbers, or underscores().
- Avoid special characters such as '?' or '%'.
- Spaces are not permitted inside names.
(You can use uppercase letters to denote word boundaries, as in milesPerGallon. This naming convention is called camel case because the uppercase letters in the middle of the name look like the humps of a camel.)
- Names are case sensitive, that is, milesPerGallon and milespergallon are different names.
- You cannot use reserved words such as or as names; these words are reserved exclusively for their special Java meanings. Your code won't compile if you use the reserved keywords.
- Class names should be , with the letter of each word capitalized.
- Methods should be , with the and the rest in camel case.
verbsfirst letter in lowercase
- Variable names should be , with the and the rest in camel case. They should indicate to the casual observer the intent of its use.
nounsfirst letter in lowercase
- Variable names should start with underscore '_ ' or dollar sign ‘$’.
- One-character variable names should be avoided except for temporary uses.
C. Which of the following are legal identifiers?
Greeting1 g void 101dalmatians Hello, World <greeting>