Recent Posts

Saturday, 23 April 2016

Java Language Fundamentals


Identifiers
     A name in java program is called identifier which can be used for identification purpose. It can be class name or method name or variable name or label name.
Eg:
public class Test {
   public static void main(String[] args) {
     int a = 10;
     System.out.println("Hai This is Ashok");
   }
}
Above example contains 5 identifiers. They are Test, main, String, args and a.

Rules
* The only allowed characters in java identifiers are a to z, A to Z, 0 to 9, $ and _ (Underscore). If we are using any other character then we will get compilation error.

* Identifiers can't start with digit i.e., 123name is not valid.

* Java identifiers are case sensitive of course java language itself considered as case sensitive programming language. i.e., name, Name, NAME both are different variable names.

* There is no length limit for java identifiers but it is not recommended to take too length identifiers.

* We can't use reserved words as identifiers otherwise we will get compilation error. i.e., int if = 20; here we will get compilation error.

* All predefined java class names and interface names we can use as identifiers. i.e., int String = 20; here we won't get any compilation error. But it is not a good programming practice.

Reserved Words
     In java some words are reserved to represent some meaning or some functionality. Such type of words are called as Reserved Words. There are 53 reserved words in java. 50 are keywords and 3 are literals (true, false, null). The 50 keywords are classified in to following.

1. Reserved words for datatypes (8)
    byte, short, int, long, float, double, boolean, char

2. Reserved words for flow control (11)
    if, else, switch, case, default, while, do, for, break, continue, return

3. Reserved words for modifiers (11)
    public, private, protected, final, abstract, static, synchronized, native, strictfp, transient, volatile

4. Reserved words for Exception handling (6)
    try, catch, finally, throw, throws, assert (Used for debugging purpose)

5. Reserved words for class related (6)
    class, interface, package, import, extends, implements 

6. Reserved words for object related (4)
    new, instanceof, super, this

7. Reserved words for return type (1)
    void 
Note : In java return type is mandatory if a method won't return anything. Such type of methods we should declared with void return type. But in C language return type is optional and default return type is int.

8. Unused keywords (2)
    goto, const
    Usage of goto keyword created several problems in old languages like C and C++. Hence SUN people band this keyword in java where as we use final keyword instead of const.
Note: goto and const are unused keywords if we are trying to use then we will get compilation error.

9. enum keyword (1) (From java 1.5V)
    we can use enum to define a group of named constants.
Eg
enum Month {
     JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC;
}
    
Note: 
1. All 53 reserved words in java contain only lower case alphabets
2. In java we have only new keyword but not delete keyword because destruction of useless objects is the responsibility of Garbage Collector.

Data Types
     In java every variable and every expression should has some type and every type is strictly defined. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. There are two data types available in Java:
1. Primitive Data Types
2. Reference/Object Data Types
1. Primitive Data Types 
     Primitive Data Types are again divided in to 2 types
1. Numeric Data types ( Integral and Floating point)
2. Non Numeric Data types (Char and boolean)
    Integral - byte, short, int, long
    Floating point - float, double

i. byte
* Byte data type is an 8-bit signed two's complement integer.

* Minimum value is -128 (-2^7)

* Maximum value is 127 (inclusive)(2^7 -1)

* Default value is 0

* Range : - 128 to + 127

* Byte data type is used to save space in large arrays, mainly in place of integers, since a byte is four times smaller than an int.

byte Representation
      The most significant bit acts as sign bit. 0 means +ve number and 1 means -ve number.
     Positive numbers will be represented directly in the memory where as negative numbers will be represented in 2's compliment form.
E.g 
byte b = 10;
byte b = 127;
byte b = 128; // Compile time error. Possible loss of precision found: int required: byte
byte b = 20.5 // Compile time error. Possible loss of precision found: double required: byte
byte b = true; // Compile time error. In compatible types found: boolean required: byte

Note: byte data type is best suitable to handle data in terms of streams either form the file or form the network.

ii. short
* Short data type is a 16-bit signed two's complement integer.

* Minimum value is -32,768 (-2^15)

* Maximum value is 32,767 (inclusive) (2^15 -1)

* Short data type can also be used to save memory as byte data type. A short is 2 times smaller than an int

* Default value is 0.
E.g 
short s = 10;
short s = 127;
short s = 32768; // Compile time error. Possible loss of precision found: int required: short
short s = 20.5 // Compile time error. Possible loss of precision found: double required: short
short s = true; // Compile time error. Possible loss of precision found: boolean required: short
Note: short data type is best suitable for 16-bit processors like 8086 but these processors are out dated hence short data type also out dated data type

iii. int
* int data type is a 32-bit signed two's complement integer.

* Minimum value is - 2,147,483,648.(-2^31)

* Maximum value is 2,147,483,647(inclusive).(2^31 -1)

* int is generally used as the default data type for integral values unless there is a concern about memory.

* Default value is 0.
E.g
int i = 10;
int i = 2147483648; // C.E :  integer number too large
int i = 2147483648l ;// C.E. Possible loss of precision found: long required: int
int i = true; // C.E. in compatible types found: boolean required: int

Note:
1. By default every number compiler will take as int if the range is beyond the int then we will get compile time error named integer number too large.

2. In C language the size of int is varied from platform to platform. For 16-bit processors it is 2 bytes where as for 32-bit processors it is 4 bytes. The main advantage of this approach is read and write operations we can perform very efficiently and performance will be improved. But the main disadvantage of this approach is the chance of failing C program is very high if we are changing platform. Hence C language is not considered as Robust.

3. But in Java language the size of int is always 4 bytes. The main advantage of this approach is chance of failing java program is very less if we are changing platform. Hence Java language is considered as Robust. But the main disadvantage of this approach is the read and write operations will become costly and performance will be reduced.

iv. long
     Some times int may not enough to hold big values then we should go for long datatype. For example to hold the amount of distance travelled by light in 1000 days int may not enough then we should go for long datatype.
long distance = 126000*60*60*24*1000 miles;
* Long data type is a 64-bit signed two's complement integer.

* Minimum value is -9,223,372,036,854,775,808.(-2^63)

* Maximum value is 9,223,372,036,854,775,807 (inclusive). (2^63 -1)

* This type is used when a wider range than int is needed.

* Default value is 0L.
     All the above four datatypes meant for representing integral values. If we want to represent floating point values then we should go for floating point datatypes.

Floating point datatypes
i. float
* If you want 5 to 6 decimal places of accuracy then we should go for float.

* Float data type is a single-precision 32-bit IEEE 754 floating point.

* Float is mainly used to save memory in large arrays of floating point numbers.

* Default value is 0.0f.

* Float data type is never used for precise values such as currency.

* Range is -3.4 E38 to +3.4 E38
ii. double
* double data type is a double-precision 64-bit IEEE 754 floating point.

* This data type is generally used as the default data type for decimal values, generally the default choice.

* Double data type should never be used for precise values such as currency.

* Default value is 0.0d.

* Range is -1.7 E308 to +1.7 E308
Non Numeric Data types
i. boolean
* boolean data type represents one bit of information.

* There are only two possible values: true and false.

* This data type is used for simple flags that track true/false conditions.

* Default value is false.

* Size : Not applicable (JVM dependent)

* Range : Not applicable (Allowed values are true or false)
Eg:
boolean b = true;
boolean b = 0; C.E: in compatible types found: int required: boolean
boolean b = True; C.E: Cannot find symbol symbol: variable True location: class Test
boolean b = "True"; C.E: in compatible types found: java.lang.String required: boolean

int x = 0;
if(x) { // C.E: in compatible types found: int required: boolean
   System.out.println("Hai");
}

int x = 1;
while(x) { // C.E: in compatible types found: int required: boolean
   System.out.println("Hai");
}

Note: Above two are executed in C++ fine. We didn't get any C.E.

ii. char
     In old languages like C and C++ we can use only ASCII characters and to represent all ASCII characters 8-bits are enough. Hence char size is 1 byte.
     But in Java we can use Unicode characters which covers world wide all alphabets sets. The number of Unicode characters is greater than 256 and hence 1 byte is not enough to represent all characters. Hence we should go for 2 bytes.
* char data type is a single 16-bit Unicode character.

* Minimum value is '\u0000' (or 0).

* Maximum value is '\uffff' (or 65,535 inclusive).

* Char data type is used to store any character.
Reference Data Types
* Reference variables are created using constructors of the classes. They are used to access objects. 

* These variables are declared to be of a specific type that cannot be changed. For example, Employee, Student etc.

* Class objects, and various type of array variables come under reference data type.

* Default value of any reference variable is null.

* A reference variable can be used to refer to any object of the declared type or any compatible type.
E.g
Animal animal = new Animal("Tiger");

null
     null is the default value for object reference and we won't apply for primitive types. If we are trying to assign null value for primitive datatypes then we will get compile time error.
Eg:
char ch = null; C.E: in compatible types found: <null type> required: char


Literals
     A constant value that can be assigned to the variable is called "Literal".
E.g
int x =20;

int - datatype / keyword
x = name of variable / identifier
20 = constant value / literal

Integral Literals:
     For integral datatypes (byte, short, int, long) the following are various ways to specify literal value.
i. Decimal literals
    Allowed digits are : 0 to 9
E.g
int x = 10;

ii. Octal literals
    Allowed digits are : 0 to 7
    Literal values should be prefix with 0.
E.g
int x = 010;

iii. Hexadecimal literals
    Allowed digits are : 0 to 9, a to f or A to F
   For the extra digits we can use both upper case and lower case. This is one of few places where Java is not case sensitive. Literal value should be prefixed with 0x or 0X.
E.g
int x = 0x10 or 0X10;
    These are the only possible integral literals.
E.g
int x = 10; (Fine)
int x = 0777; (Fine)
int x = 0XFace; (Fine)
int x = 0Xbeef; (Fine)
int x = 0786; (C.E: Integer number too large.. Digit is octal so 8 not allowed)
int x = 0Xbeer; (C.E: Integer number too large.. Digit is hexa decimal so r not allowed)

class Test {
   public static void main(String args[]) {
      int x = 10;
      int y = 010;
      int z = 0X10;
      System.out.println(x + " " +y+ " " +z);
   }
}

Output
10 8 16
      By default every integral literal is of int type but we can specify explicitly as long type by specifying with l or L.
E.g:
int x = 10;
long l = 10l;
long l = 10 L;
int x = 10 L; // C.E: Possible loss of precision found: long required: int
     There is no way to specify integral literal is of byte and short type explicitly. If we are assigning integral literal is with in the range of byte then compiler automatically treats as byte literal. Similarly short literal also.
byte b = 10;
byte b = 130; C.E : Possible loss of precision. found : int required: byte

Floating point literals
     Every floating point literal is by default double type and hence we can't assign directly to float variable. But we can specify explicitly floating point literal to the float variable by suffixing with f or F.
E.g
float f = 123.456; //C.E: Possible loss of precision found: double required: float
float f = 123.456f;
double d = 123.456;
double d = 123.456f;
     But we can specify explicitly floating point literal as double type by suffixed with d or D. Of course this conversion is not required.
E.g
double d = 123.456d;
Note: We can specify floating point literals only in decimal form. Octal and hexa decimal terms are not allowed.
double d = 123.456;
double d  = 0123.456; //Fine. It is not treated as octal it is treated as decimal.
double d = 0X123.456; // Compile time error
     We can assign integral literals directly to the floating point variable and that integral literal can be specified either in decimal or octal or hexadecimal form.
E.g
double d = 0786; // C.E : integer number is too large
double d = 0XFACE; // Fine
double d = 0777; // Fine
double d = 0XFACE.0; // Compile Error
double d = 0786.0; // Fine
     But we can't assign floating point literals directly to the integer types.
int i = 21.50; // C.E Possible loss of precision found : double required : int
    We can specify floating point literal even in exponential form. (Also known as scientific notation)
E.g
double d = 1.2e3; // 1.2 * 1000 = 1200;
float f = 1.2e3; // C.E Possible loss of precision found : double required : float
float f = 1.2e3f; // 1200 

Boolean literals
     The only allowed values for boolean data type are true or false, where case should be in lower case.
E.g
boolean b = true; //Fine
boolean b = 0; // C.E Possible loss of precision found : int required : boolean
boolean b = True; // Cannot find symbol
boolean b = "true"; // C.E Possible loss of precision found : java.lang.String required : boolean

int x = 0;
if(x) {  // C.E Incompatible types found : int required : boolean
   System.out.println("Hai");
} else {
   System.out.println("Hello");
}

while(1) { // C.E Incompatible types found : int required : boolean
   System.out.println("Hai");
}

int x = 10;
if(x=20) { // C.E Incompatible types found : int required : boolean
   System.out.println("Hai");
} else {
   System.out.println("Hello");
}

int x = 10;
if(x==20) { // Hello
   System.out.println("Hai");
} else {
   System.out.println("Hello");
}

Character literals
     A character literal can be represented as single character within the single quotes.
E.g
char ch = 'a'; // Fine
char ch = a ; // C.E: Can't find symbol Symbol : variable a location : class XXXX
char ch = 'ab'; // C.E: Unclosed character literal 'ab' (at a)
                // C.E: Unclosed character literal  'ab' (at b)
                // C.E: not a statement
     We can specify character literal as integral literal which represents UNICODE value of that character. The integral literal can be specified either in decimal or octal or hexadecimal forms. The allowed range is 0 to 65535.
E.g
char ch = 97; // a
char ch = 65535; // ?
char ch = 65536; // Possible loss of precision found : int required : char
     A character literal can be represented as in Unicode representation which is nothing but \uxxxx. 4 digit hexa decimal number.
E.g
char ch = '\u0061'; // a
char ch = '\uface'; // ?
char ch = '\iface'; // C.E: invalid escape sequence
char ch = '\Uface'; // C.E: invalid escape sequence
     Every escape character in java is a char literal.
E.g
char ch = '\n';
char ch = '\t';
char ch = '\m'; \\ C.E: Illegal escape character
Escape character       Description
    \n              New line character
    \t              Horizontal tab
    \r              Carriage return
    \b              Backspace character
    \f              Form feed
    \'              Single quote
    \"              Double quote
    \\              Back slash

String literals
     A sequence of character within " " (double quotes) is considered as string literals.
E.g
String s = "Ashok";

Enhancements with respect to literals
Binary Literals
     For integral datatypes (byte, short, int, long) until 1.6 version we can specify literal value in the following ways
1. decimal literal
2. octal literal
3. hexadecimal literal
     But from 1.7 version onwards we can specify literal value even in binary form also. The allowed digits are 0 or 1. Literal value should be prefixed with 0b or 0B;
E.g
int i = 0B1111; //15
     In Java SE 7 and later, any number of underscore characters (_) can appear anywhere between digits in a numerical literal. This feature enables you, for example, to separate groups of digits in numeric literals, which can improve the readability of your code.
E.g
int num = 111_222;
System.out.println(num); //Prints 111222

double d = 1_23_4.5_67;
System.out.println(d); //Prints 1234.567
     We can use more than one _ symbols also between the digits
E.g
double d = 1__2__3.4__5;
     We can use _ symbol only between the digits otherwise we will get compile time error.
E.g
double d = _1_23_456.7_8; // C.E
double d = 1_23_456_.7_8; // C.E

Next Tutorial   Arrays in Java

Previous Tutorial  Java Language Basics

2 comments: