Language Fundamentals

Basic Language Elements

Like any other programming language, the Java programming language is defined by grammar rules that specify   how    syntactically       legal constructs can be formed the language elements, and by a semantic definition that specifies the meaning of syntactically legal constructs.

  Lexical Tokens

The low-level  language elements   are    called lexical tokens     (or just tokens for short) and are the
building blocks for more complex constructs. Identifiers,   numbers, operators, and special characters are all examples of tokens that can be used to build high-level constructs like expressions, statements, methods, and classes.

  Identifiers

A name in a program is      called    an identifier. Identifiers can be used to denote classes, methods, variables, and labels.

  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. Literals represent numerical (integer or floating-point), character, boolean or string values. In addition, there is the literal null that represents the null reference.

  Integer Literals

Integer data types are comprised of the following primitive data types: int, long, byte, and short.

  Floating-point Literals

Floating-point data types come in two flavors: float or double. The default data type of a floatingpoint
literal is double,      but it can be explicitly designated by appending the suffix D (or d) to the value. A floating-point literal can also be specified to be a float by appending the suffix F (or f). Floating-point literals can also be specified in scientific notation, where E (or e) stands for
Exponent.

 For example, the double literal 194.9E-2 in scientific notation is -2interpreted as 194.9*10 (i.e.,

1.949).

 

  Boolean Literals--

The primitive data type boolean represents the truth-values true or false that are denoted by the
reserved literals true or false, respectively.

Character Literals

     A   character literal is quoted in single-quotes ('). All character literals have the primitive data type char. Characters in Java are represented by the 16-bit Unicode character set, which subsumes the 8-bit
ISO-Latin-1 and the 7-bit ASCII characters.

  String Literals

A string literal is a sequence of characters, which must be quoted in quotation marks and which
must occur on a single line. All string literal are objects of the class String .


  White Spaces

    A white space is a sequence of spaces, tabs,form feeds, and line terminator characters in a Java
source file. Line terminators can be newline, carriage return, or carriage return-newline sequence.
A Java program is a free-format sequence    of characters that is tokenized by the compiler, that is,
broken into a stream of tokens for further     analysis. Separators and operators help to distinguish
tokens, but sometimes white  space  has     to be inserted explicitly as separators. For example, the
identifier classRoom will  be  interpreted  as  a single   token,  unless white       space is inserted to
distinguish the keyword class from the identifier Room.
   White space aids not only in separating tokens, but also in formatting the program so that it is
easy for humans to read. The compiler ignores the white spaces once the tokens are identified.


  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 Declarations

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. We show some
examples of declaring variables that can store primitive values:

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.
The first declaration above is equivalent to the following three declarations:
char a;
char b;
char c;
A declaration can also include initialization code to specify an appropriate initial alue for the
variable:
int i = 10, // i is an int variable with initial value 10.
j = 101; // j is an int variable with initial value 101.
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.
     A variable declaration that specifies a reference type (i.e., a class, an array, or an interface name)
declares an object reference variable. Analogous to the declaration  of  variables  of  primitive  data
types, the simplest    form   of reference    variable declaration    20 only specifies the name and the reference type. The declaration determines what objects a reference variable can denote. 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, smallOne; // and so can variable smallOne.

  Lifetime of Variables

Lifetime of a variable, that is, the time a variable is accessible during execution, is determined by
the context in which it is declared. We distinguish between lifetime of variables in three contexts:

   • Instance variables members of a class and created for each object of the class. In other words,
every object of the class will have its own copies of these variables, which are local to the object.
The values of these variables at any given time constitute the state of the object. Instance
variables exist as long as the object they belong to exists.

    • Static variables— also members of a class, but not created for any object of the class and,
therefore, belong only to the class. They are created when the class is loaded at runtime, and exist
as long as the class exists.

   • Local variables (also called method automatic variables) declared in methods and in blocks and created for each execution of the method or block. After the execution of the method or block completes, local (non-final) variables are no longer accessible.

  The main() Method

The Java interpreter executes a method called main in the class specified on the command line. Any class can have  a  main()           method, but only the main() method of the class specified to the Java interpreter is executed to start   a Java application. The main() method must have public accessibility
so that the interpreter can call it       . It is a static method belonging to the class, so that no object of
the class is required to start the     execution. It does not return a value, that is, it is declared void . It always has an array of String objects as its only formal parameter. This array contains any arguments passed to the program on the command line. All this adds up to the following definition of the main() method:
public static void main(String[] args) 
{
// ...
}
     The above requirements do not exclude specification of additional modifiers or any throws clause.
The main() method can also be overloaded like any other     method. The Java interpreter ensures that
the main()       method, that complies       with the above definition is the starting point of the program
execution.

No comments:

Post a Comment