Language Fundamentals
___________________________________________________
___________________________________________________
Identifiers - A name in Java program is called identifier, it can be class name, variable name, label name or method name
Java identifiers are case sensitive, there is no limit of identifiers name but for good programming it is not recommended to take very large name, identifier can't start with number. in special character only & and _ is acceptable.
Reserved Words - can't be use as identifiers
byte - size 8 bits / 1 byte, Min_Value -128, Max_Value 127, Range -128 to 127
The most significant bit is called sign bit, 0 means +ve, 1 means -ve value, +ve numbers represented directly in the memory where as -ve numbers represented in 2's compliment. byte data type - is best suitable, if we want to handle data in terms of streams either from file or the network.
short - size 2 bytes, Range -2^15 to 2^15 - 1 (-32768 to 32767)
Most rarely used data type in java is short, if we use 16 bit processor like 8086 then short data type is best choice.
int - size 4 bytes, Range -2^31 to 2^31 - 1 (-2147483648 to 2147483647)
The most commonly used data type, In C language, the size of int is varied from platform to platform for 16 bit processor 2 bytes, 32 bit processor 4 bytes, the main advantage of this approach is read write operation 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 very high if we are changing platform, it is not robust.
In Java, the size of int is always 4 bytes irrespective of any platform the main advantage of this approach is the chance of failing java program is very less if we change platform hence java is considered as robust and the main disadvantage in this approach is read write operation will be costly and performance will reduce.
long - size 8 bytes, Range -2^63 to 2^63 - 1
When int is not enough to hold big values then we have to go for long data type. for eg- To represent the amount of distance traveled by light in 1000 days int is not enough compulsory we have to go for long type. long l = 123000*60*60*24*1000 miles;
float - size 4 bytes, Range -3.4e38 to 3.4e38
If we want 5 to 6 decimal places of accuracy then we should go for float float follows the single precision.
double - size 8 bytes, Range -1.7e308 to 1.7e308
if we want 14 to 15 places of decimal accuracy the we should go for double. double follows the double precision.
In other programming language, 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 set. the number of Unicode characters is >256 & 1 byte is not enough to represent all character so -
size 2 bytes, Range 0 to 65535
int i = 10;
int i = 010; // octal - should be prefix with 0
int i = 0x10 // Hexa - should be prefix with 0x
long l = 10;
long l = 10l;
float f = 123.456f;
double d = 123.456;
char c = 'a';
char c = 97;
String s = "ilk";
Array is an indexed collection of fixed number of homogeneous data elements, the main advantage of array is we can represent multiple values under the same name. so, that readability of code improved. but the main limitation of array is once we create an array there is no chance of increasing or decreasing the size based on our requirements. Hence, for memory utilization array is not suitable but performance wise array concept is awesome, by array we can fetch any element with the help of index. we can resolve memory utilization issue by using collections.
int[ ] a; // Single Dimensional Array
int [ ][ ] a; // Two Dimensional Array
int[ ][ ][ ] a; // Three Dimensional Array
Every array in java is an object, so we can create by using 'new' keyword. for every array type corresponding classes are available but these classes are not applicable for programmer level, at the time of construction compulsory we have to specify the size otherwise we will get compile time error.
int[ ] a = new int[ 3 ]; // Here, a is a reference variable
int[ ] a = new int[ 0 ]; // In java it is possible to have 0 size array
In java multi dimensional array does not implement in the matrix form, here we use another concept called 'Array of Array'. the main advantage of this approach is memory utilization will be improved. for example.
int[ ][ ] a = new int[ 3 ][ ];
a[ 0 ] = new int[ 2 ];
a[ 1 ] = new int[ 1 ];
a[ 2 ] = new int[ 3 ];
Whenever we create an array automatically every element gets initialized with default values. if we try to print any object reference internally toString( ) will be call.
int[ ] a = new int[ 3 ];
System.out.print(a); // output - [I@3e25a5 hashcod
System.out.print(a[ 0 ]); // output - 0
we can declare, construct and initialize the array in single line also. for eg
int[ ] a = {10, 20, 30, 40}; // Single Dimensional Array
int[ ][ ] a = {{10, 20, 30,}, {40, 50}}; // Two Dimensional Array
char[ ] c = {'a', 'e', 'i', 'o', 'u'}; // char type Array
String[ ] s = {"arjava", "tutorial"}; // String type Array
length - It is a final variable applicable only for arrays, use to represent the size of array.
int[ ] a = new int[11];
System.out.print(a.length); // output - 11
In multi dimensional arrays length variable represent only base size, not total size.
int[ ][ ] a = new int[6][5];
System.out.print(a.length); // output - 6
System.out.print(a[0].length); // output - 5
length( ) - It is a final method applicable only for String objects, use to represent the number of characters present in String.
String s = "arjava";
System.out.print(s.length()); // output - 6
If we create array without name then it is called as Anonymous Array, the main objective of this concept is just for instant use, at the time of creation we can't specify the size otherwise we will get compile time error.
new int[ ] {10, 20, 30, 40};