Java Language Fundamentals
Identifiers
- A name in a program is called an identifier.
- Identifiers can be used to denote classes, methods, variables, and labels.
- In Java an identifier is composed of a sequence of characters, where each character can be either a letter, a digit, a connecting punctuation (such as underscore _), or any currency symbol (such as $, ¢, ¥, or £). However, the first character in an identifier cannot be a digit.
- Identifiers in Java are case sensitive, for example, price and Price are two different identifiers.
Examples of Legal Identifiers: number, Number, sum_$, bingo, $$_100, mål, grüß
Examples of Illegal Identifiers: 48chevy, all@hands, grand-sum
Keywords
- Keywords are reserved identifiers that are predefined in the language and cannot be used to denote other entities.
- All the keywords are in lowercase, and incorrect usage results in compilation errors.
Literals
- A literal denotes a constant value, that is, the value a literal represents remains unchanged in the program. Identifiers can be used to denote classes, methods, variables, and labels.
- Literals represent numerical (integer or floating-point), character, boolean or string values. In addition, there is the literal null that represents the null reference.
Examples of literals
- Integer 2000, 0, -7
- Floating-point 3.14, -3.14, .5, 0.5
- Character 'a‘, 'A‘, '0‘, ':‘, '-‘, ')'
- Boolean true, false
- String "abba“, "3.14“, "for“, "a piece of the action“
Comments
A program can be documented by inserting comments at relevant places. These comments are for documentation purposes and are ignored by the compiler.
Java provides three types of comments to document a program:
- A single-line comment: // ... to the end of the line
- A multiple-line comment: /* ... */
- A documentation (Javadoc) comment: /** ... */
Primitive data types
Primitive data types in Java can be divided into three main categories:
- Integral types— represent signed integers (byte, short, int, long) and unsigned character values (char)
- Floating-point types (float, double)— represent fractional signed numbers
- Boolean type (boolean)— represent logical values
Primitive data values are not objects. Each primitive data type defines the range of values in the data type, and operations on these values are defined by special operators in the language.
Variable declaration
A variable stores a value of a particular type. A variable has a name, a type, and a value associated with it. In Java, variables can only store values of primitive data types and references to objects. Variables that store references to objects are called reference variables.
Declaring and Initializing Variables
Variable declarations are used to specify the type and the name of variables. This implicitly determines their memory allocation and the values that can be stored in them.
- char a, b, c; // a, b and c are character variables.
- double area; // area is a floating-point variable.
- boolean flag; // flag is a boolean variable.
A declaration can also include initialization code to specify an appropriate initial value for the variable:
- int i = 10; // i is an int variable with initial value 10.
- long big = 2147483648L; // big is a long variable with specified initial value.
Object Reference Variables, An object reference is a value that denotes an object in Java. Such reference values can be stored in variables and used to manipulate the object denoted by the reference value. Before we can use a reference variable to manipulate an object, it must be declared and initialized with the reference value of the object.
- Pizza yummyPizza; // Variable yummyPizza can reference objects of class Pizza.
- Hamburger bigOne; // Variable bigOne can reference objects of class Hamburger
- Pizza yummyPizza = new Pizza("Hot&Spicy"); // Declaration with initializer.