Вы находитесь на странице: 1из 8

Language Fundamentals

1.Identifiers:
A name in a java program is called as an identifier. It can be class
name or method name or variable name or label name.
Ex:-

class Demo{
Public static void main(String[] args){
String companyName=PRODIGY;
}
}

Rules to define Identifiers:-

The only allowed characters in a java identifier are a-z (or) A-Z (or)
0-9 (or) _ (or) $.
Identifier cannot start with digit.
We cannot use reserved words as identifiers.
Java Identifiers are case sensitive.
There is no length limit for a java identifier. But, it is never
recommended to take more than 15 length.
We can use all predefined java class names and interface names as
identifiers.
It is legal but not recommended.
Examples:-->The following are the valid java identifiers.
ProdigySolutions, totalNumber, ca$h, j2se, _$_$_, String,
Runnable, sum555.
--> The following are the invalid java identifiers.
total@Number, 123Prodigy, float, null, cyber*towers.

2.Reserved Words:
In java some identifiers are reserved to represent some functionality
or purpose. Such type of reserved identifiers are called as Reserved
Words.
There are 53 reserved words in java.
Reserved Literals(3):- true, false, null.
Unused Keywords:- goto, const.

Used Keywords: byte, short, int, long, float, double, char, Boolean.
If, else, switch, case, break, default, do, while, for, continue, return.
public, private, protected, abstract, final, static, synchronized,
strictfp(1.2V), native, transient, volatile.
try, catch, finally, throw, throws, assert(1.4V)
class, interface, package, import, extends, implements.
new, instanceof, super, this.
void.
enum(1.5V).
If we want to define a group of named constants then we
should go for enum.
Ex1:enum Month
{

JAN, FEB, MAR,..,DEC;


}
Ex2:-

enum Beer
{

KF, KO, RC, FO;


}
Note:- All reserved words in java contain only lower case alphabet
symbols.

3. Data Types:In java, every variable has a type, every expression has a type and
every data type is strictly defined . Moreover every assignment should be
checked by the compiler for type compatibility. Hence java language is
considered as strictly typed.
int x=10.5; //Invalid in java.
boolean b=0; //Invalid in java.
Java is not considered as pure Object Oriented Programming
Language. Because, several OOP features like multiple inheritance,
operator overloading, etc . are not supported by java. Moreover we are
depending on primitive data types, which are not objects.
The following are the primitive data types in java.
byte, short, int, long, float, double, Boolean, char.

Data
Type

Size

Range

Wrapper
Class

Default
value

byte

1
byte

-128 to 127

Byte

short

2
byte
s

-32768 to 32767

Short

int

4
byte
s

-2147483648 to
2147483647

Integer

long

8
byte
s

9223372036854775
808 to

Long

9223372036854775
808
float

4
byte
s

-3.4e38 to 3.4e38

Float

0.0

double

8
byte

-1.7e308 to 1.7e308

Double

0.0

s
boolean

NA

NA

Boolean

false

char

2byt
es

0 to 65535

Character

space(\u0
000)

4.Literals: Any constant value which can be assigned to the variables is called
literal.
Ex:int x=10; // here value 10 is integer literal.
String s=prodigy; // here value prodigy is string literal.

Integral Literals:- For the integral data types(byte, short, int, long), the
following are the various ways to specify literal values.
1. Decimal Literals(base 10): The allowed digits are 0-9
Ex:int x=10;
2. Octa Literals(base 8): The allowed digits are 0-7 and literal value
should be prefixed with 0.
Ex:- int x=010;
3. Hexadecimal Literals(base 16): The allowed digits are 0-9 and
allowed characters are A-F (or) a-f. Literal value should be prefixed
with 0X (or) 0x.
Ex:- int x=0x10;
These are the only ways to specify integrals literals. There is no
style to represent binary literals(base 2) in java.
Ex:-

class Test {
public static void main(String[] args) {
int a=10;
int b=010;
int c=0x10;
System.out.println(a+" "+b+" "+c);
}
}
By default every integral literal is of type int. But we can specify
explicitly as long type by suffixing with l or L.
Examples:- int x=10; // valid
long l=10; // valid
long l=10L; // valid
int x=10L; // invalid, results in compile time error.
Note:We can assign smaller data type value to the bigger data type
variable. But we cannot assign
bigger data type value to the
smaller data type variable.
There is no way to specify byte and short literals explicitly. If we are
trying to assign integral literals to the byte (or short) variable and

its value is within the range of byte (or short) then compiler treats
it as byte literal automatically.
Ex:- byte b=10; // valid
byte b=127; // valid
byte b=128; // invalid, results in compile time error.
Floating Point Literals:- By default floating point literal is of double
type. But we can specify explicitly as float type by suffixing with f or F.
Examples:- float f=10.5; // invalid, results in compile time error.
float f=10.5f; // valid
double d=10.5; // valid
double d=10.5d; // valid
double d=10.5D; // valid
We can specify floating point literals only in decimal form and we
cannot specify in Octal and Hexadecimal form.
Examples:- double d=0123.456; //valid, this will be treated as
decimal value, not octal.
double d=0x123.456; // invalid(malformed floating point
literal).
We can assign integral literal directly to the floating point data type
which is specified in Decimal or Octal or Hexadecimal form.
Examples:- double d=0xFace; // valid
double d=0123; // valid
double d=10e2; // valid
float f=10e2; // invalid, results in compile time error.
float f=10e2F; // valid
Boolean Literals:- The only possible values for the Boolean data type are
true or false. If we specify any other we will get compile time error.
Examples:- boolean b=true; // valid
boolean b=TRUE; // invalid
boolean b=0; // invalid
Char Literals:- A char literal can be specified as single character within
single quotes.
Examples:- char ch=a; // valid
char ch=a; // invalid
char ch=ab; // invalid
A char literal can be represented as integral literal which represents
Unicode of that character. Integral literal can be specified in Decimal
or Octal or Hexadecimal forms. But allowed range is 0 65535.
Examples:- char ch=65; // valid
char ch=065; // valid
char ch=0xBeef; // valid
char ch=65535; // invalid
A char literal can be represented in Unicode representation, which is
in the form '\uXXXX.
Ex:- char ch=\u0041; // valid
Every escape character(\n, \t, \r, \b, \f, \, \, \\) in java is a valid char
literal.
Examples:- char ch=\n; // valid
char ch=\t; // valid
char ch=\k; // invalid

String Literals:- Any sequence of characters enclosed in double quotes is


treated as String literal.
Ex:- String s=SRIKANTH; // valid

5.Arrays: An array is an indexed collection of fixed number of


homogeneous(same) data elements.
The main advantage of arrays is, we can represent multiple values
by using a single variable. This approach improves the readability of
the code.
The main disadvantage of arrays is, its size is fixed. i.e., once we
created an array with some size there is no chance of increasing or
decreasing the size of that array based on our runtime requirement.
To use arrays concept compulsory we should know the size in
advance, which may not possible always.
Array Declarations:-

1-D Array
Declarations

2-D Array
Declarations

3-D Array
Declarations

int[] a;

int[][] a;

int[][][] a;

int []a;

int [][]a;

int[] [][]a;

int a[];

int a[][];

int[] []a[];

int[5] a; //CE

int[] []a;

int []a[][];

Note:- If we want to specify the dimension before the variable, it is valid

only for the first variable. If we try to apply for the second variable of the
same declaration we will get compile time error.
Ex:- int[] []a,b; // valid
int[] []a,b[]; // valid
int[] []a,[]b; // invalid
Array Construction:- Every array in java is an object. Hence, we can
create an array by using new operator. At the time of construction
compulsory we should specify the size, otherwise we will get compile time
error.
Ex:- int[] a=new int[5]; // valid
int[] a=new int[]; // invalid
int[] a=new int[0]; // valid
If we try to specify the array size with negative int value then we will
get Runtime Exception saying NegativeArraySizeException.
To specify array size the only allowed data types are byte, short, int,
char. If we are using any other type we will get compile time error.
Ex:- int[] a=new int[A]; // valid
int[] a=new int[10L]; // invalid
int[] a=new int[10.5]; // invalid

The maximum allowed array size in java is 2147483647(maximum


value of int data type).
Ex:- int[] a=new int[2147483647]; // valid
For every array type corresponding classes are available. But these
classes are part of java language and not available to the
programmer level.

Array
Type

Corresponding
Class Name

int[]

[I

int[][]

[ II

float[]

[F

double[]
[]

[[ D

char[]

-----

In java, multidimensional arrays are not implemented by using


matrix form(rows & columns). These are implemented by using array
of arrays concept. The main advantage of this approach is to
improve memory utilization.
Ex:- int[][] a=new int[5][]; // valid
int[][] a=new int[][5]; // invalid, base size is mandatory.
int[][][] a=new int[2][3][]; // valid
int[][][] a=new int[2][][3]; // invalid
Array Initialization:- Once we created an array every element by default
initialized with default values.
Ex:- int[] a=new int[3];
System.out.println(a); // O/P: [ I@3e25a5
System.out.println(a[0]); // O/P: 0
Note:- Whenever we are trying to print only object reference, internally
Object class toString() method will be executed, which is implemented to
return the following string form.
className@Hexadecimal string representation of
hashCode
public String toString() {
return getClass().getName() + "@" +
Integer.toHexString(hashCode());
}
Ex1:-

int[][] a=new int[3][2];


System.out.println(a); //O/P: [[ I@3e25a5
System.out.println(a[0]); //O/P: [ I@19821f

System.out.println(a[0][0]); //0
Ex2:- int[][] a=new int[3][];
System.out.println(a); //O/P: [[ I@3e25a5
System.out.println(a[0]); //O/P: null
System.out.println(a[0][0]); // NullPointerException
Note:- If we are trying to access array element with out of range index
(either +ve or -ve) we will get Runtime Exception saying
ArrayIndexOutOfBoundsException.
Ex:- int[] a=new int[3];
A[3]=50; // AIOOBE
A[-3]=50; // AIOOBE

Alternate way to declare, construct, initialize an array:- We can

perform these three operations in a single line.


Ex:- int[] a={10,20,30,40,50};
char[] ch={a,e,i,o,u};
String[] s={CHIRU,BALAIAH,NAAG,VENKY};
int[][] a={{10,20,30},{40,50}};
int[][][] a={{{10,20},{30,40,50}},{{60,70},{80},
{90,100}}};
length Vs length(): length is a final variable applicable for arrays. It represents the size
of the array.
Ex:- int[] a=new int[3];
System.out.println(a.length); // O/P: 3
length() is a final method available in String class(final class) and
applicable only for String objects. This method returns the number
of characters present in the string.
Ex:- String s=PRODIGY;
System.out.println(s.length()); // O/P: 7
Note:- In multidimensional arrays length variable represents only base
array size.
Ex:- int[][] a=new int[4][5];
System.out.println(a.length); // O/P: 4
System.out.println(a[0].length); // O/P: 5
Anonymous Arrays:- We can declare arrays without name, such type of
nameless arrays are called anonymous arrays. The main objective of
anonymous arrays is just for instant use(only for one time usage). We can
create an anonymous array as follows..
new int[]{10,20,30,40,50};
At the time of anonymous array creation we cannot specify the size.
Ex:- new int[5]{10,20,30,40,50}; // invalid, results in compile time
error.
Based on our requirement we can provide the name for an
anonymous array, then it is no longer anonymous.
Ex:- int[] a=new int[]{10,20,30,40,50};
System.out.println(a[2]); // O/P: 30