Language Fundamentals

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Language Fundamentals as PDF for free.

More details

  • Words: 1,008
  • Pages: 4
Language Fundamentals

Identifiers are names of variables, functions, classes etc. The name used as an identifier must follow the following rules in JavaTM technology. Each character is either a digit, letter, underscore(_) or currency symbol ($,¢, £ or ¥) o First character cannot be a digit. o The identifier name must not be a reserved word. A keyword or reserved word in Java technology has special meaning and cannot be used as a user defined identifier. The list of keywords in Java technology is given below. It is important to completely remember this list as you can expect a question in Java Certification exam related to this. abstract boolean break byte case catch char class const continue default do double else extends final finally float for goto if implements import instanceof int interface long native new null package private protected public return short static strictfp super switch synchronized this throw throws transient try void volatile while It is important to note the following o

II.

III.

const and goto are not currently in use. null, true, and false are reserved literals but can be considered as reserved words for the purpose of exam. C. It is important to understand that Java language is case-sensitive. So even though super is a keyword, Super is not. D. All the Java technology keywords are in lower case. E. strictfp is a new keyword added in Java 2. F. The list of keywords as defined by Sun is present here. A literal in Java technology denotes a constant value. So for example 0 is an integer literal, and 'c' is a character literal. The reserved literals true and false are used to represent boolean literals. "This is a string" is a string literal. Integer literals can also be specified as octal (base 8), or hexadecimal (base 16). Octal and hexadecimal have 0 and 0x prefix respectively. So 03 and 0x3 are representation of integer three in octal and hexa-decimal respectively. Java technology supports three type of comments A. A single line comment starting with // B. A multi-line comment enclosed between /* and */ A. B.

II. III. IV.

A documentation or javadoc comment is enclosed between /** and */. These comments can be used to generate HTML documents using the javadoc utility, which is part of Java language. Java technology supports the following primitive types - boolean (for representing true or false), a character type called char, four integer types (byte, short, int and long) and two floating point types (float and double). The details of these types are given below Data types Width (in bytes) Minimum value Maximum Value byte 1 -27 27 - 1 short 2 -215 215-1 int 4 -231 231 - 1 long 8 -263 263 - 1 char 2 0x0 0xffff -45 float 4 1.401298e 3.402823e+38 double 8 4.940656e-324 1.797693e+308 Corresponding to all the primitive type there is a wrapper class defined. These classes provide useful methods for manipulating primitive data values and objects. Data types Wrapper class int Integer short Short long Long byte Byte char Character float Float double Double Instance variables (data members of a class) and static variables are initialized to default values. Local variables (i.e. variables defined in blocks or inside member functions) are not initialized to default values. Local variables must be explicitly initialized before they are used. If local variables are used before initialization, compilation error gets generated. The defaults for static and instance variables are given in the table below. Data types Default Values boolean false char '\u0000' Integer types (byte, short, int, long) 0 Floating types (float, double) 0.0F or 0.0 D Object References null C.

V.

VI.

VII.

VIII. IX.

public static void main(String args[]) {

X. XI. XII. XIII.

XIV.

XV.

int i; System.out.println(i); }

In this example printing of i generates a compilation error because local variable i is used before being initialized. The initialization of instance and static variables is an important concept both for understanding of Java language, and for Java Certification exam. A Java source file has the following elements in this specific order. o

An optional package statement. All classes and interfaces defined in the file belong to this package. If the package statement is not specified, the classes defined in the file belong to a default package. An example of a package statement is package testpackage;

o

Zero or more import statements. The import statement makes any classes defined in the specified package directly available. For example if a Java source file has a statement importing the class "java.class.Button", then a class in the file may use Button class directly without providing the names of the package which defines the Button class. Some examples of import statement are import java.awt.*; // All classes in the awt package are imported. import java.applet.Applet;

o

Any number of class and interface definitions may follow the optional package and import statements.

If a file has all three of the above constructs, they must come in the specific order of package statement, one or more import statements, followed by any number of class or interface definitions. Also all the above three constructs are optional. So an empty file is a legal Java file. II.

The Java interpreter executes a method called main, defined in the class specified in command line arguments. The main method is the entry point for execution of a class. In Java technology the main method must have the following signature public static void main(String args[])

The java interpreter is invoked with the name of the class as an argument. The class name is followed by possible set of arguments for the main function of the class. When a Java program is invoked then the Java interpreter name "java" and the class name are not passed to the main() method of the class. The rest of the command line arguments are passed as an array of String. For example invoking java Sky blue gray

would invoke main method of Sky class with an array of two elements - blue and gray.

Related Documents