You are on page 1of 23

LANGUAGE FUNDAMENTALS

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

if a method doesn’t return anything compulsory that method should


be with void return type

UnUsed Keywords

goto in java usage is considered as harmful.


const alternatively we should use final keyword

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

datatype is int. size = 4 bytes


range = -231 to 231 – 1(-2147483648 to 2147483747)

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:

size = not applicable(virtual machine dependent).


range = not applicable but allowed values are true/false.

Which of the following boolean declarations are valid

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

datatype size range Default value Wrapper class

byte 1 byte -128 to 127 0 Byte


short 2 bytes -32768 to 32767 0 Short
int 4 bytes -231 to 231-1 0 Integer
long 8 bytes -263 to 263-1 0 Long
float 4 bytes -3.4e38 to 3.4e38 0.0 Float
double -1.7e308 to
8 bytes 0.0 Double
1.7e308
boolean NA(But allowed
NA values are true, false Boolean
false)
char 2 bytes 0 to 65535 0(balnk spaces) Character
Literals:
A literal represents a constant value which can be assigned to the variables

int x = 10;

datatype/keyword Name of the Constant value/literal


variable /Identifier
Integral Literal :
We can specify an integral literal in the following ways.
Decimal literals: allowed digits are 0 to 9Ex: int x = 10;
Octal literals: allowed digits are 0 to 7 but here literal value should be prefixed with
0(zero)Ex: int x = 010;
Hexadecimal literals: the allowed digits are 0 to 9, A- F (Both lower, Upper case)
literalsshould be prefixed with 0x or oX
Ex: int x = 0x10;

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.

Floating – point literals:


By default floating-point literals are double type we can specify explicitly as
float type by suffixing with ‘f’ or ‘F’.

we can specify explicitly a floating point literal of double


type by suffixing with d or D. we can also represent float-
point literals by using scientific notation.
Ex:
double d = 10e23;
int i = 10e23; C.E possible loss of precision found : double required : int.

Floating point literals can be specified only in decimal form. i.e we


can’t use octal and hexa decimal representation for floating point
literals.
Ex:
Double d = 0x123.456; C.E: Malformed floating-point literal.
Boolean Literals :
The only allowed values for boolean datatype are true, false.

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:-

1) Length of the code will be decreased


2) We can access the element present in the any location.

3) Readability of the code will be increased

Single Dimensional Array


Declaration:-

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:

dataType [] nameOfArray = new dataType [size]

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

public static void main(String[] args)

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];

Scanner s=new Scanner(System.in);


System.out.println("enter values");
for (int i=0;i<a.length;i++)

System.out.println("enter "+i+" value"); a[i]=s.nextInt();


}

for (int a1:a)

System.out.println(a1);
}

}
}
Ex:-

class Test

public static void main(String[] args)

int[] a=new int[100];


System.out.println(a.length);
System.out.println(a[99]); boolean[] b=new
boolean[100]; System.out.println(b[99]);
char[] ch=new char[50];
System.out.println(ch[44]); byte[] bb=new
byte[100];

System.out.println(bb[100]);//ArrayIndexOutOfBoundsException

}
}

Ex:-class Test

{
public static void main(String[] args)
{

int[] a=new int[4];// allocates memory for 4 elements a[0]=10;

a[1]=100;
a[2]=1000;
a[3]=10000;

System.out.println(a.length); for (int


i=0;i<a.length;i++ )

{
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

public static void main(String[] args)

int[] a={10,20,30};
System.out.println(a.getClass().getName());

}
}

Two dimensional array:-


JAVA NAMING CONVENSIONS:-

Java is a case sensitive language so the way of writing code is important.

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.

Ex: String StringBuffer


FileInputStream

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

4. All java constant variables should be in uppercase letter.

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.

You might also like