1. Basic syntax
The following points should be noted when writing Java programs:
- case-sensitive : Java is case-sensitive, which means that the identifier Hello is different from hello.
- class name : For all classes, the first letter of the class name should be capitalized. If the class name consists of several words, the first letter of each word should be capitalized, such as Myfirstjavaclass.
- method Name : All method names should start with a lowercase letter. If the method name contains several words, the first letter of each subsequent word is capitalized.
- source file Name: The source file name must be the same as the class name. When saving the file, you should use the class name to save the filename (remember that Java is case-sensitive), and the suffix of the file name is. java. (a compilation error is caused if the file name and the class name are not the same).
- Main Method Entry : All Java programs are executed by the public static void main (String []args) method.
2.Java identifiers
All components of Java require a name. Class names, variable names, and method names are called identifiers.
For the Java identifiers, there are a few things to note:
- All identifiers should start with a letter (A-Z or a-Z), a dollar symbol ($), or an underscore (_).
- The first character can be a letter (A-Z or a-Z), a dollar symbol ($), an underscore (_), or any character combination of a number
- Keyword cannot be used as an identifier
- Identifiers are case-sensitive
- Examples of legal identifiers: Age, $salary, _value, __1_value
- Examples of illegal identifiers: 123abc,-salary
3.Java modifier
Like other languages, Java can use modifiers to decorate methods and properties in a class. There are two main types of modifiers:
- Access control modifiers: default, public, protected, private
- Non-access control modifiers: final, abstract, STRICTFP
4.Java variables
There are several types of variables in Java that are mainly
- Local variables
- class variables (static variables)
- Member variables (non-static variables)
5.Java arrays
An array is an object stored on a heap and can hold multiple variables of the same type.
Java Enumeration
Java 5.0 introduces enumerations, and enumeration limit variables can only be pre-set values. Use enumerations to reduce bugs in your code.
For example, we design a program for the juice shop that will limit the juice to small cups, medium cups, and large cups. This means that it does not allow customers to spot fruit juices other than these three sizes.
Note: enumerations can be declared individually or declared inside a class. Methods, variables, and constructors can also be defined in enumerations.
Java Basic syntax