Sunday, July 02, 2006

Java - Introduction 2

Lexical Tokens (or Tokens): These are the low level language elements which are the building blocks of more complex constructs. Identifiers, numbers, operators and special characters are all examples of tokens which can be used to build high level constructs .

tokens->expression-> statements-> methods->classes

Identifiers : identifiers are names used to denote classes, methods, variables and labels.

Keywords : These are reserved identifiers predefined by the language and can't be used to denote other entities. All the keywords are in lower case.

Keywords in Java:

abstract

assert

boolean

break

byte

case

catch

char

class

continue

default

do

double

else

extends

final

finally

float

for

if

implements

import

instanceof

int

interface

long

native

new

package

private

protected

public

return

short

static

strictfp

super

switch

synchronized

this

throw

throws

transient

try

void

volatile

while

Reserved keywords not in use:

const

goto

Reserved literals:

null

true

false

Literals: A literal denotes a constant value which doesnot change in the program. These consists of integer, floating point, character, boolean & string literals.

Integer Literals : default data type of a integer literal is int unless specified otherwise by an 'L' or 'l' suffix (eg. 2000L).

Integer literals can also be specified in octal (0 prefix) or hexadecimal (0x, 0X prefix) number systems. Negative numbers can be specified by prefixing a '-' irrespective of the number system.


Decimal

Octal

Hexadecimal

8

16

-90L

010

020

-0132L

0x8

0x10

-0x5aL

Floating point Literals: default data type of a floating point literal is double unless specified as float by suffixing a 'F' of 'f'. These can also be specified in scientif notation, where E (or e ) stands for Exponent. eg. 194.9E-2 = 194.9*10^-2 = 1.949. The decimal & exponent are optional & at least one digit must be specified.

Examples of literals:

double Literals

0

0.17

17.0

1.7E+1

0.0d

0.17D

17.

1.7E+1D

0D

.17D

17D

1.7E1D, 1700e-2D, .17e2

float Literals

0F

0.17F

17.0F

1.7E+1F

0.0f

0.17F

17.F

1.7E+1F

0F

.17F

17F

1.7E1F, 1700e-2F, .17e2F

Boolean Literals : Primitive datatype boolean representing values true or false denoted by reserved literals true and false.

Character Literals : Primitive data type char. A character literal is quoted in single quote ('). Character in Java are represented in Unicode which encompasses the 8-bit ISO-Latin-1 & 7-bit ASCII charater sets. Any Unicode character can be specified as a 4-digit hexadecimal number (i.e. 2 bytes x 8 bits) with the prefix \u.

Character Literal

Unicode Value

Character

' '

'0'

'1'

'9'

'A'

'B'

'Z'

'a'

'b'

'z'

'\u0020'

'\u0030'

'\u0031'

'\u0039'

'\u0041'

'\u0042'

'\u005a'

'\u0061'

'\u0062'

'\u007a'

space

0

1

9

A

B

Z

a

b

z

Escape Sequences :

Escape Sequence

Unicode Value

Character

\b

\t

\n

\f

\r

\'

\"

\\

\u0008

\u0009

\u000a

\u000c

\u000d

\u0027

\u0022

\u005c

Backspace

TAB (HTab)

Newline(NL,LF)

Formfeed(FF)

Carriage return (CR)

Apostrophe

Quotation mark

Backslash


Special Unicode characters : '\u000a' (newline) & '\u000d' (carriage return). These are interpreted by the compiler as line terminator characters so use sequence '\n' & '\r' respectively for correct interpretation in the source code.

A charater literal can also be specified by an Octal value using escape sequence \ddd. where each d repesents an octal digit (0-7).

Escape sequence \ddd

Character literal

'141'

'\46'

'\60'

'a'

'&'

'0'

String Literals: It is a string of characters enclosed within quotation mark. These are objects of the String class. Escape sequence as well as Unicode values can appear in string literals :

Eg. "A fresh brew!"

"a Tab using \\t:[\t] . another using unicode : [\u0009]" ;

"\"A fresh brew!\" " ; // quoted string.

"A new line \n restart..."

White spaces : consists of spaces, tabs, formfeeds & line terminator characters(can be CR, NL or NL+CR) in Java source file. These help in formating the program for easy reading as well as aids in separating tokens.

A java program is a free format sequence of characters that is tokenized by the compiler. Separators & operators help identify the tokens.

Comments :

Single line comments

//.......

rest of the line is ignored

Multi line comment

/*.... */

can't be nested.

Javadoc comment

/** .... */

Groups of special tags can be used inside documentation comment to provide specific information.

/**

* @author SK

* @version 1.9.1

*/

Primitive Data Types

Data Type

Width(bits)

MIN_VAL

MAX_VAL

Wrapper Class

Integer types:

byte

8

-2^7 (-128)

2^7-1 (+127)

Byte

short

16

-2^15(-32768)

2^15-1(+32767)

Short

int

32

-2^31(-2147483648)

2^31-1(+2147483647)

Integer

long

64

-2^63(-9223372036854775808L)

2^63-1(+9223372036854775807L)

Long

Character type:

char

16

0x0 (\u0000)

0xffff (\uffff)

Character

Floating Point types:

float

32

1.401298464324817E-45f

3.402823476638528860e+38f

Float

double

64

4.94065645841246544e-324

1.79769313486231570e+308

Double

Boolean Types:


width

True value literal

False value literal


boolean

n.a.

true

false

Boolean


Variable declaration : A variable has a name, a type & a value.

eg.: char a, b, c;

int i=100, j=250;

double i=j=200;

long k=2088376L;

Object reference variable : A variable declaration that specifies a reference type (i.e. A class, interface or an array) declares a reference variable. A variable must be declared & initialised before it is used. A declaration only creates the variables(eg. String str1;).

Otherwise, an initializer can also be included (eg. String s6= new String("A fresh brew!")) to create an object whose reference is than assigned to the variable. The key word new together with constructor call String("A fresh brew! ") creates an object of class String.

eg.: String str1; String s1, s2;

String s5="A fresh brew!";

String s6= new String("A fresh brew!");

Life time of variables:

Instance variables

Members of a class, created for each object of the class, these exists as long as the object they belong to exist.

Static variables

Members of a class, but not created of each object. Thus only belong to the class. Created when class is loaded at runtime, they exist as long as the class exists.

Local variables

Method automatic variables declared in methods and in blocks. Created for each execution of the method or block, these local variables (non-final) become inaccessible after the execution completes.

Initial default values of variables:

boolean

false

Char

'\u0000'

byte,short,int, long

0, 0L for long

float/double

0.0F, 0.0D

reference type

null

Initialization status if no explicit initialisation is provided:

Static variables

Initialized to default value when the class is loaded.

Instance variables

Initialized to default value when the class is instantiated.

Reference variables

Always initialized to null.

Local variables

Not initialized when created at method invocation. Must be explicitly initialized before use.

Java Source file structure:

//Test1.Java

//Optional Package declaration

package sk.faPackage;

// import declarations

import java.utils.*;

import java.io.*;

// top level class & interface declarations

public class Test1{}

interface mnk{ }

class Test2 { }

interface jk2 { }

//done

Declarations sequence:

  1. Package declaration

  2. Import declarations

  3. Type declarations i.e. class/interface declarations in any order

  4. At most one public class per source file and the name of the source file must be the same as the public class.

  5. Except for package & import statements all code is encapsulated in classes & interfaces. White spaces & comments can be put anywhere.

  6. main() method is the public static void method of the class so no instance of the class is required for execution. It has a array of String objects as argument which carries the command line parameters passed to the program. It is the starting point of the program.

    Signature of main method: public static void main (String[] args){}

    The main() method can be overloaded, and addional modifiers & throws clause can be included.



Comments: Post a Comment

Links to this post:

Create a Link



<< Home
Advertisements

eBay.in - The World's Online Marketplace

ARCHIVES
  • March 2006
  • April 2006
  • May 2006
  • June 2006
  • July 2006
  • August 2006
  • September 2006

This page is powered by Blogger. Isn't yours?

Recent Postings

Google