Comments

Friday, November 18, 2011

Java Tutorial : Identifiers, Keywords and Types

Posted by on Friday, November 18, 2011 Read our previous post
Identifiers :

In Java programming language, an identifier is a name given to a variable, class or method. Identifiers start with a letter, underscore(_) or dollar sign ($). The following characters can be digits. Identifiers are case sensitive and have no maximum length.



The following are valid Identifiers :

  • identifier
  • userName
  • user_name
  • _sys_var1
  • $change


Note :

Identifiers containing a dollar sign ($) are generally unusual, although languages like BASIC make extensive use of them. Its probably best to avoid using dollar sign ($).


Keywords :

Keywords have special meaning to the Java compiler. They help in indentifying a data type name or program construct name.

Java Programming Language Keywords :

abstract continue for new switch
assert default goto package synchronized
boolean do if private this
break double implements protected throw
byte else import public throws
case enum instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp volatile
const float native super while


To download the full description of each keyword : CLICK HERE

Types :

The Java programming language has many built in data types. These are classified into two broad categories 

  1. Class Types
  2. Primitive Types

Primitive types are simple values and not objects. Class types are used for more complex types, includeing all types that we declare ourselves.

Primitive Types :


The java programming languagw has eight primitive data types :
  • Logical : boolean
  • Textual : char and String
  • Integral : byte, short, int and long
  • Floating point : double and float

Logical - boolean :

Logical values are represented using the boolean type, which takes one of two values : true or false. The following code is an example of the declaration and initialization of a boolean type varialble :
    // declares the variable truth as boolean and

    // assigns it the value true

    boolean truth=true;
    
Textual - char :

Single character are represented using a char type. A char represents a 16-bit, unsigned Unicode character.
    '\a' The letter a

    '\t' A tab

    '\u????' A specific Unicode character, ????, is replaced with exactly four hexadecimal digits.
    
Textual - String :

String type, its not a primitive type its a class type, used to represent a sequences of characters. The characters themselves are Unicode. Unlike C and C++, strings do not end with \0. A String literal is enclosed in double quotes marks :


    "Code2Learn team welcomes you to Javabegin."

    
Some example of declaration and initialization of char and String types are :

    // declares and intializes a char variable

    char ch='A';

    //declares and initializes a String variable

    String str="hello! world";

    

Note :

The String data type always starts from captical S and not s, i.e. String str=null; (is right) and string str=null; (is wrong).


Integral - byte, short, int and long

There are four integral tyoes in java programming language as you can see above. Each type is declared using one of the keywords byte, short, int or long.

    2  This is the decimal form of integer 2

    077       The leading 0 indicates an octal value

    0xBAAC The leading 0x indicates a hexadecimal value

    
Integral values are of type int unless specified by the letter L that indicates long value. In java programming you can use either an uppercase or lowercase L. Long version of literals are shown as

2L The L indicates that the decimal value 2 is represented as long value

077L  The leading 0 indicates an octal value

0xBAACL The leading 0x indicates a hexadecimal value

Integral Data Type - Size and Range

Integer Length Name or Type Range
8 bits byte From- 27 to 27-1
16 bits short From- 215 to 215-1
32 bits int From- 231 to 231-1
64 bits long From- 263 to 263-1

Floating Point - float and double

Floating point variable can be declared using the keywords float and double.

Examples of float and double variable

    3.14  A simple floating-point value (a double)

    6.02E23  A large floating-point value

    2.718F  A simple float size value

    123.4E+306D  A large double value with redundant D

    
Floating point literals are double by default. You can declare a literal of types float by appending F or f to value.

© 2010 Code 2 Learn