Published 15 October 2018

Java language syntax

Getting to know the language syntax, important considerations

Getting to know the language syntax, important considerations

The syntax of the Java programming language is a set of rules that define how the program is written and interpreted.  When writing Java programs, you need to take into account the following considerations:

  • The file name is identical to the class name. It is very important to keep a record of the names in the register in which it was made, otherwise an error will occur during compilation;
  • each opening bracket must have a closing bracket;
  • the output is done using System.out.print, System.out.println;
  • Each command (expression) ends with a semicolon;
  • Classes are grouped into packages;
  • The main() method must exist in one of the classes

Identifiers in Java

Identifiers in Java

Identifier is the name given to the program object by the variable, class, object, method, interface.

  • an identifier can consist of letters, numbers, an underscore _, and a $ sign;
  • Java keywords cannot be used as identifiers;
  • an identifier cannot begin with a number;

Operators and separators

Operators and separators

Operators are special characters that perform operations on operands. Operators are divided into the following groups:

  • arithmetic operators are used in mathematical expressions;
  • comparison operators check whether the operand is greater than, less than, equals to, not equal to the second one. The result is true or false.
  • boolean operators are intended for operations on Boolean expressions;
  • bitwise operators allow to check and modify the state of individual bits of integer data types;
  • assignment operators are used to assign a value to variables;
  • a ternary operator or conditional statement consists of three operands and is used to evaluate Boolean expressions.
  • an instanceof operator checks whether an object is a class or an interface that is used only for reference type variables.

Delimiters are special characters that affect the appearance and functionality of code.  Example of delimiters: ( ),{ }, [ ], . , ;

Enumeration and what are they used for

Enumeration and what are they used for

Enumerations are a set of logically related named constants, similar to any other Java class. The enumeration is created using the enum keyword, followed by the name of the enumerator, and the identifiers in the curly brackets are referred to as enumeration constants.

Used to assign variable values from a small set of possible values. Can be used instead of a class if the class must have a fixed number of enumerated instances.

Variables and literals

Variables and literals

Variable is the memory area used to store the specified value. To declare a variable, you must set the data type, variable identifier, and initialize the variable as needed.

  • name of a variable starts with a lowercase letter and consists of letters (Unicode), numbers, and an underscore "_";
  • name of a variable must not be a key or a reserved word;
  • variable name is case sensitive;
  • after declaring a variable it is necessary to put a semicolon ";"

Literals are explicitly defined values of one of the valid data types that are used in different expressions.

Modifiers

Modifiers

Modifiers are key words that give a specific property to a class, class field, method.  

There are the following groups of modifiers:

  • accessibility modifiers, private, package-private, protected, public.
  • class modifiers, abstract, final, static, strictfp.
  • method modifiers, abstract, final, static, strictfp, native, synchronized.
  • field modifiers, static, final, transient, volatile.
  • modifiers associated with interfaces are the same as class modifiers except final.

Array

Array

An array is a data structure storing a fixed number of values of the same type. An array consists of elements of an array; the position of an element in an array is specified by an index.  An array is specified by specifying the data type of the elements contained therein, followed by the brackets [] and the name of the array that should conform to the variable naming rule. Arrays can be single-dimensional and multidimensional.

Spaces and comments in Java

Spaces and comments in Java

In Java, you can format the program text arbitrarily, so there is no need to align the text in a special way. Any code entry is interpreted as if there is a single space between the individual tokens.

Comments are used to describe the program code. Three types of comments are used in Java:

  • single line, cover one line starts with two slashes //
  • multi-line, spans multiple lines starts with /* and ends with */
  • Javadoc comments, special multiline comment. Starts with /** and ends with */  

Keywords in Java (what they are and what are they used for)

Keywords in Java (what they are and what are they used for)

Keywords are reserved words in the Java language and cannot be used as identifiers.

The following words are keywords:

abstract, default, if, private, throw, boolean, do, implements, protected, throws, break, double, import, public, transient, byte, else, instanceof, return, try, case, extends, int, short, void, catch, final, interface, static, volatile, char, finally, long, super, while, class, float, native, switch, const, for, new, synchronized, continue, goto, package, this.

Method names in Java

Method names in Java

Method names should be given in the form of a combination of verbs and nouns. The first letter must be lowercase. Example: getNumberPage.

Empty string

Empty string

An empty string "" is a string that has no characters. All standard operations for strings are correctly defined for an empty string.

Inheritance

Inheritance

Inheritance is a mechanism by which you can extend the functionality of existing classes by adding new functionality or changing the old one. To declare a class as an Inheriting class, the extends keyword is used after the name of the inheritor class, before the parent class name.

Interface

Interface

An interface is a collection of methods and rules of interaction between elements of the system. Interface defines how the elements will interact with each other. Is used to create fully abstract classes, declared using the interface keyword.

Votes 0, average rating 0

Contents