Professional Documents
Culture Documents
1) Identifiers
2) Keywords
3) Datatypes
4) Literals
5) Arrays
6) Types of variables
7) var – arg methods
8) command line arguments & main method
9) java coding standard
Identifiers :
Identifiers A name in the program is an identifier it may be class name or method
name,variable name or label name
Rules for defining Identifiers :
1) A java identifier is a sequence of characters, where each character may be a
letter from a-z or A-Z or a digit form 0-9 or currency symbol $ or connecting
punctuation – , if we are using any other symbol we will get Compile time error
“Illegal Character”.
2) Identifier should not be starts with digit.
3) There is no length limit for java identifiers but it is not recommended to take
more than 15 length.
4) Java Identifiers are case sensitive.
5).we cannot take Reserved words as not identifiers.
EX:
Int x=10;//accepted
Int if =20;//reserved word not Accepted
6).All predefined java class name and interface names we can use as identifiers.
Ex:
Which of the following are valid Identifiers
1) total#
2) all@hands
3) 123total
4) break
5) String
6) total_number
7) $_$
8) $ca$h
Reserved Words:
Some identifiers are reserved to associate some functionality
or to represent values, such type of reserved identifiers are
called “ReservedWords”.
Keywords for ExceptionHandling
void return type Keywords
UnUsed Keywords
Note: All reserved words in java contain only lower case alphabet symbols.
Datatypes
In java every variable has a type, every expression has a type and all types
are strictly defined.
All the assignments should be checked by the compiler for the
type compatibility. Hence java language considers as strongly
typed language.
Java is not considered as pure object oriented programming language
because several OOP features(like multiple inheritance, operator overloading)
are not supported by java. Even java contains non-object primitive datatypes
Except boolean and char all the remaining datatypes are signed datatypes i.e we
can represent both +ve and–ve numbers.
Byte:
Size: 1Byte(8bits)
Range -128 t0 127
-ve numbers can represented in 2’s compliment form.
Ex:
byte b = 10;
byte b = 127;
byte b = 130; C.E: possible loss of precision
byte b = true; C.E: Incompatible types found: boolean
required: byte
byte datatype is best suitable if we are handling data either from file or form network
Short:
size = 2 bytes
range = -215 to 215 -1 (-32768 to 32767)
Ex:
short s = 10;
short s = 32767;
short s = 65535; C.E: possible loss of precision
short s = true; C.E: Incompatible types
short is best suitable datatype for 16-bit process. But currently these are completely out
dated and hence the corresponding datatypes also no one is using.
Int:
The most commonly used
The size of int is always fixed irrespective of platform hence the chance of failing java
program is very less if u r changing the platform hence the java is considered as Robust.
Long :
if int is not enough to hold big values then we should go for long-
datatype
size = 8 bytes
range = -263to263 – 1
Ex:
The amount of distance traveled by light in 1000days can be represented by long
datatype only and int is not enough.
floating-point :
for representing real numbers(numbers with decimal points)
Boolean data type:
boolean b1 = true;
boolean b2 = 0; × Incompatible types found:int required : Boolean
boolean b3 = TRUE; × capital TRUE is not valid.
Char :
java provides support for Unicode i.e it supports all world wide
alphabets. Hence the size of char in java is 2 – bytes. And range is 0
to 65535
Comparison table for java primitive datatypes
int x = 10;
class Test
{
public static void main(String arg[])
{int x = 10;
int y = 010;
int z = 0x10;
System.out.println(x + "..." + y + "..." + z);
}
}
Except decimal, octal, hexadecimal there is no other way to represents
constant values for the integral datatype.
By default every integral lateral is of int datatype we can specify explicitly.
An integral literal is of long type by suffixing with l or L.
Ex: 10 int value.
10l long value.
long l = 10l;
int i = 10l;
C.E: possible loss of precision found : long
Required:int
There is no way to specify explicitly an integral literal is of type byte and short.
If the integral literal is with in the range of byte then the JVM by default treats
it as byte literal.
Similarly short literal also.
character literal :
A char literal can be represented as a single character with in single quotes.
String literal :
A sequence of character with in double quotes is String literal.
String s ="Durga";
String s ='software';
String s = 'a';
ARRAYS:
1) Array is a final class inheritance is not possible.
2) Arrays are used to store the multiple numbers of elements of single type.
3) The length of the array is established at the time of array creation. After creation the length is
fixed.
4) The items presented in the array are classed elements. Those elements can be accessed by index
values. The index is begins from (0).
Advantages of array:-
Syntax:
dataType [ ] nameOfArray;
int[] a;
int []a;
int a[];
Declaration &
instantiation &
initialization :-
Approach 1:-
Syntax:
Data type arrayname[]={values};
Ex:
int a[]={10,20,30,40};
Approach 2:-
Syntax:
Ex:
int[] a=new int[100];
a[0]=10;
a[1]=20;
a[2]=30;
a[4]=40;
Ex:-printing the array elements
class Test
int[] a={10,20,30,40};
System.out.println(a[0]);
System.out.println(a[1]);
System.out.println(a[2]);
System.out.println(a[3]);
}
}
Ex:-printing the array elements by using
Ex:-printing the array elements by using for for-each loop(1.5 version)
loop
class Test
class Test {
{ public static void main(String[] args)
public static void main(String[] args) {
{
int[] a={10,20,30,40};
int[] a={10,20,30,40};
for (int a1:a)
for (int i=0;i<a.length;i++)
{
{
System.out.println(a1);
System.out.println(a[i]);
}
}
}
}}
} }
Ex:-
import java.util.*;
class Test
{public static void main(String[] args)
{
int[] a=new int[5];
System.out.println(a1);
}
}
}
Ex:-
class Test
System.out.println(bb[100]);//ArrayIndexOutOfBoundsException
}
}
Ex:-class Test
{
public static void main(String[] args)
{
a[1]=100;
a[2]=1000;
a[3]=10000;
{
System.out.println(a[i]);
}
}}
To get the class name of the array:-getClass() method is
used to get the class. getName() method is used to
print the name of the class.
class Test
int[] a={10,20,30};
System.out.println(a.getClass().getName());
}
}
1. All Java classes,Abstract classes and Interface names should start with
uppercase letter ,if any class contain more than one word every
innerword also start with capital letters.
2. All java methods should start with lower case letters and if the method
contains more than one word every innerword should start with capital
letters.
Ex :- post() toString()
toUpperCase()
3. All java variables should start with lowercase letter and inner words
start with uppercase letter.
Ex:- pageContent
bodyContent
Ex: MIN_PRIORITY
MAX_PRIORITY
NORM_PRIORITY
5. All java packages should start with lower case letters only.
Ex: java.awt
Java.io
NOTE:-
The coding standards are applicable for predefined library not for user
defined library .But it is recommended to fallow the coding standards for
user defined library also.