Fundamentals of Java

Fundamentals of Java

Learn about keywords, identifiers, literals, data types, operators, separators and comments in Java

The smallest building block of a program that can be identified separately from the compiler is called a token. Java programs are a collection of statements, and these statements are created by putting together these tokens. Java supports these types of tokens: Keywords, Identifiers, Literals, Operators, Separators and Comments.

Keywords

Keywords are the reserved words in Java that have a predefined meaning in the language. Whenever a compiler encounters a keyword token, it identifies its special purpose in the code and generates instructions for the same. For the same reason, we cannot use the names of the keywords as variables, methods or class names. Keywords in Java are always written in lowercase.

Java has the following 52 keywords: java keywords

Note:

  1. The keywords const and goto are reserved, even though they are outdated and not currently used.
  2. true and false might seem like keywords, but they are reserved literals; you cannot use them as identifiers in your programs.
  3. _ (underscore) was added as a reserved keyword in Java 9.

Identifiers

Identifiers are the names given to memory locations used to store data in a program. Identifiers represent the names given to any variable, constant, function, class, array or user-defined type.

Rules for defining identifiers in Java:

  1. The only allowed characters for identifiers are all alphanumeric characters ([A-Z], [a-z], [0-9]), '$' (dollar sign) and '_' (underscore).
  2. Identifiers cannot start with digits ([0-9]).
  3. Java identifiers are case-sensitive.
  4. There is no limit on the length of the identifier.
  5. Reserved words can’t be used as an identifier.

Literals

Literals are the constant values i.e. they do not change their values in a program. Java provides the following literals: Integer, Floating Point, Character, String, Boolean and null. We will learn about each in the next section.

Data Types in Java

data types in java Data types are the various pre-defined specifications of values, sizes and ranges of literals provided by a programming language to work on different types of data.

Java provides two types of Data Types:

Primitive Data Types

These are the basic built-in data types provided by Java, and cannot be created by the programmer. They are always used in lowercase as they are keywords. They occupy a predefined amount of memory during runtime. They can be used to create Non-Primitive Data Types. They are also known as Basic, Fundamental or Intrinsic Data Types. There are 8 different primitive data types: primitive types in java

Note:

  • By default, all non-fractional numbers/literals are considered to be int data type and all fractional literals are considered to be double data type.
  • To specify float, double and long data types, special suffixes like f,d and l (or their uppercase forms) respectively can be used along with their literals.
    Example: 1.2f, 2.5d, 10L
  • Floating-point data types include the use of storing numbers as their Mantissa Exponent Form.
    Example: 3.2E1, 1.2E-3
  • Character literals are single Unicode symbols that are enclosed within single quotes. Hence '' and 'ab' are not valid characters.
  • There are specific non-graphic characters denoted by a \ before the character, which cannot be typed directly from the keyboard and they have a special meaning in the language.
    Example: '\n' (new line), '\t' (tab space), '\b' (backspace key), '\a' (audital bell), '\'' (apostrophe), '\"' (double quote) and '\\' (backslash)
  • String is not a primitive data type, as it is a class in Java. String literals are zero or more characters enclosed within double-quotes. Example: "", "ABC"

Non-Primitive Data Types

They can be either provided by the Java language or created by the programmer. They may not be written in lowercase as they are not keywords. The amount of memory occupied by this data type depends on the set of primitive data types used to make them. They are also known as Derived, Reference and Extrinsic Data Types.

Separators

Separators are symbols that help us define the structure of a program. Separators have special meanings attached to them by the compiler and are used to identify a block of code and differentiate them from other parts. The most commonly used separator in java is a semicolon ;. separators

Operators

Operators are special symbols provided by any language that act on operands to give back a result. Java provides various types of operators:

Increment/Decrement Operators

It is a unary operator. This operator increases or decreases the value of the operand by 1. The operator may be placed before the operand (prefix) or after the operand (postfix) and this determines whether, in an expression, the value should be evaluated first or changed first. Example: If initially x=10, then x++ will increase the value of x by 1, making it 11. Similarly, x-- will decrease the value to 9.

Arithmetic Operators

Unary arithmetic operators + and - are used to indicate the positive or negative value of the number.
Binary arithmetic operators in Java are shown as follows: arithmetic operators Note:

  • The division operator (/) always results in an integer quotient, not fractional.
    Example: 10/3 results in 3 and not 3.33
  • The modulus operator can be used with both integer and fractional values.

Relational Operators

They are binary operators which always return a boolean value. They are used to compare the two operands they are used with. relational operators

Logical Operators

These operators are used along with relational operators to form complex conditions. Java provides three relational operators: Logical Short-circuit AND &&, Logical Short-circuit OR || and Logical NOT !. logical operators

Conditional/Ternary Operator

This operator takes three operands for its evaluation. It can be used a simple alternative to the if-else statement. The syntax of the same is:
<test condition> ? <true expression> : <false expression>.
Example: 5<10 ? 0 : 1 return 0

Bitwise and Shift Operators

These are operators that perform operations on the binary representations of integers (only byte, short, int, and long data types). bitwise and shift operators

Bitwise Operators take corresponding bits of the numbers to return another 0 or 1 accordingly. They work according to the truth table as shown below: truth table

Shift Operators manipulate the left-hand side binary numbers by moving the bits to the left or right as many times as mentioned by the right-hand side operand. shift operator explanation

Assignment Operator

This operator is used to store the result of the right-hand side expression in the left-hand-side variable. It has the lowest priority among all operators i.e. it is always evaluated after all other operators are evaluated in the expression.
Example: a = 10 + 5

Shorthand Operators

These operators are a way by which an expression can be written in a shorter form. shorthand operators

Comments

These are non-executable statements of a program and can be written anywhere. Comments are code-description or any additional information about the program or any of its parts, written to enhance the readability of a program.
Java provides these types of comments:

  1. Single-line comments:
    These are comments that span only till the end of the current line of the program. They are written using the // symbol.
  2. Multi-line comments:
    To describe a full method in a code or a complex code snippet single line comments can be tedious to write. To overcome this multi-line comments can be used. They are written between /* and */
  3. Documentation Comments:
    This type of comment is used generally when writing code for a project/software package since it helps to generate an auto-generated documentation page for reference that is generated by using documentation comments and a Javadoc tool for processing the comments.
    Syntax: documentation comments

Conclusion

There are a few more nuances in each of these concepts we covered in this article, which will be introduced along with the advanced concepts at the right time. Until now we have successfully learned the very basics of programming in Java so that when we do start coding from the next article in this series, we will have a better understanding of the code.