Objective
This information is useful to the person who has prior knowledge on JAVA and also who
is preparing for scjp exam.
The purpose of this document is to provide you with important concepts with
examples. I have tried my best to cover as many points as I could in this document. So I
recommend you to go through this document at least once.
Chapter 1: Language fundamentals
1. Identifiers
2. Keywords
3. Data types
4. Literals
5. Arrays
6. Un initialized variables
1.1. Identifiers
Rules of identifiers
Combination a-z ,0-9 and Symbols only _ , $.
First character should never be a digit.
There is no length limit but up to 15 digits is recommended.
Identifiers are case sensitive (Number=Number=number).
Some identifiers are reserved in java those are called Keywords.
No Keyword is allowed as identifier in java.
All predefined JAVA class names are allowed to use as identifiers.
1.2. Keywords
Some identifiers are reserved in java which is called reserved words.
1) 53 Reserved words
1.1) 50 Keywords
1.1.1) 48 Key words
1.1.2) 2 Unused key words (Constant, go to)
1.2) 3 Reserved literals (True/false/null)
Keywords for primitive data type (8)
Key words for flow control (11)
1.3. Data types
Data types
Numeric types Char Boolean(True/False)
Integral Floating Point
byte float
short double
int
long
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
int:
Long:
float:
double:
boolean:
char:
1.4. Literals
1.5. Arrays
1) Declare an array
1.6. UN initialized variables
This information is useful to the person who has prior knowledge on JAVA and also who
is preparing for scjp exam.
The purpose of this document is to provide you with important concepts with
examples. I have tried my best to cover as many points as I could in this document. So I
recommend you to go through this document at least once.
Chapter 1: Language fundamentals
1. Identifiers
2. Keywords
3. Data types
4. Literals
5. Arrays
6. Un initialized variables
1.1. Identifiers
Rules of identifiers
Combination a-z ,0-9 and Symbols only _ , $.
First character should never be a digit.
There is no length limit but up to 15 digits is recommended.
Identifiers are case sensitive (Number=Number=number).
Some identifiers are reserved in java those are called Keywords.
No Keyword is allowed as identifier in java.
All predefined JAVA class names are allowed to use as identifiers.
1.2. Keywords
Some identifiers are reserved in java which is called reserved words.
1) 53 Reserved words
1.1) 50 Keywords
1.1.1) 48 Key words
1.1.2) 2 Unused key words (Constant, go to)
1.2) 3 Reserved literals (True/false/null)
Keywords for primitive data type (8)
byte float
short double
int char
long boolean
Key words for flow control (11)
if do
else while
switch return
case break
default continue
Keywords for Exception handling (6)
try throw
catch throws
finally assert
Keywords for modifiers (11)
public synchronized (not synchronize)
protected volatile
private transient
static native
abstract strictfp (strict floating point)
final
Keywords related to class (6)
class implements (not implement or implemented)
import import (not imports)
package extends (not extend)
Keywords related to objects (4)
new
instance of (not instance Of)
super
this
Other keywords
void: indicate no return type for a method
Unused key words: goto, const
The new keyword introduced in j2se1.5 is enum.
Enum: in order to define a list of name constants
Ex: enum month
{JAN, FEB…}
All the java reserved words contain only alphabet symbols and all the symbols are in
lowercase.
Data types
Numeric types Char Boolean(True/False)
Integral Floating Point
byte float
short double
int
long
Because of this non objects (primitive data types) java is not considered as a purely
object oriented programming language.
Primitive type Wrapper classbyte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
except boolean and char all the remaining data types are called signed data types. We
can represent both +ve and –ve numbers
byte:
Smallest integral data types
Size = 8 bits = 1 byte
If MSB is 0 = +ve
If MSB is 1 = -ve
Range is -128 to +127
Default value = 0
short:
Size = 2 bytes = 16 bits
Range -2pow (16-1) to 2pow (16-1)-1 formula [-2pow (n-1) to 2pow (n-1)-1]
This data type is very rarely used data type in java. It is best suitable for 16-bit
processor like 8086.
Default value = 0
int:
Size = 4 bytes = 32 bits
Range -2pow (32-1) to 2pow (32-1)-1
The size of the int is irrespective of the platform.
This is the most commonly used data type in java.
Default value = 0
This date type is preferable if int is not enough to hold big values like the amount of
distance travelled by light in 1000 days.
Size = 8 bytes = 64 bits
Range = -2pow (64-1) to 2pow (64-1)-1
Default value = 0
float:
Single precision = 6-7 decimal places
Size = 4 bytes = 32 bits
Range = -1.7e38 to 1.7e38
Default value = 0.0
Double precision = 14-15 decimal places
Size = 8 bytes = 64 bits
Range = -3.4e308 to 3.4e308
Default value = 0.0
boolean:
Size: not applicable (virtual machine dependent)
Range: not applicable
Values true/false (not True/False)
Default value = false (not False)
char:
Size = 2 bytes = 16 bits
Range 0 to 65535
Default value = 0 (blank space)
In Unicode 0 = blank space character
12
The constant value which can be assigned for a variable is called Literal.
Integral----- [byte, short, int, long]
Integral literals------ 3 types
decimal literals---- int x = 3
octal literals 066
hexadecimal literals 0x66 [alphabets are either capital or small ]
All integral literals are by default int type.
There is no direct way to specify byte literals and short literals externally.
Floating point literals
All floating point literals are by default double values.
Byte---short—int---long----float---double
Char------------int---long----float---double
Floating point literals can not be assigning for the integral data types.
Integral literal can be assigned to the floating point literal.
Boolean literals
For the Boolean variables only the possible literals are true/false. [Not True/False].
Char literals
A char literal can be specified as a single character with in simple code.
A char literal can also be specified as an integer from 0 to 65535.
Char literal can also specify as Unicode representation. It is \u followed by 4 digit hexa
decimal number with in single codes.
Escape sequences
Escape sequences Unicode values character
\b \u0008 Back space
\t \u0009 Horizontal tab
\n \u000a new line
\r \u000d carriage return
\f \u000c form feed
\’ \u0027 single code
\” \u0022 double codes
\\ \u00jc back slash
Easy to remember big forms need red tractors.
int x = 5
Data type/keyword
Name/identifier
Value/literal
13
Instead of \n and \r we never allow using the corresponding uni codes \u000a and
\u000d even in comments also.
There is a separate meaning for that in compiler so violation leads to compile time
exception.
1) Declare an array [ create a reference variable ]
2) How to construct an array [ creation of array object ]
3) Initialization of array [ populating array with elements ]
4) Declaration, construction and initialization in a single line
5) length Vs length()
6) Anonymous arrays
7) Array element assignments
8) Array reference assignments
An array is a data structure that defines an indexed collection of fixed number of
homogeneous date elements.
The main limitation of array is the size is fixed. I.e. once we are creating an array with
some size there is no chance of increasing or decreasing the size.
The performance of arrays is very high when compared with collections.
Ways of declaring array
int [ ] a;
int a [ ];
2-dimentional array:
int [ ] [ ] a [ ];
int [ ] a[ ],b; ----- a= two dimension, b= one dimension.
We can declare size after the type or after the name but not before the name.
int [ ] a[ ], [ ]b; ------------ wrong, this gives compile time exception.
int [ ] [ ]a, b[ ], [ ]c; ---------------- wrong
Int [ ] a;
type
dimension
Name of array
14
It is illegal to specify the size of the array at the time of declaration.
Ex: int [ ] a; is correct.
int [16] a; is wrong.
2) Constructing an array:
If we are not specifying the size of the array at the time of construction it is a
compile time exception.
Some examples:
1. int[ ] a;
a = new int[6]; ------ correct
2. int[ ] a = new int[ ];
This is wrong; it gives compile time exception because size is not specified.
3. int[ ] a = new int[-6];
Runtime exception like negative array size exception.
4. It is legal to have an array with size zero in java.
long l = 10;
int[ ] a = new int[l];
it gives compile time exception like
Found: long
Required: int
If we want to specify the size of the array with some variables
byte, short, char and int are allowed
long, float, double and Boolean are not allowed.
Assignment of arrays or populating an array with elements
Ex: int[ ][ ] a = new int[3][ ];
a[0] = new int[2];
Structure is
O/P a[0][0] = 0.
a[1] = null.
a[1][0] = null pointer exception.
null 0 null null
0 0
15
4) Declaration construction and initialization in a single line
If we are accessing an element with out of range index or negative index we will
get a runtime exception saying array index out of bounds exception.
Declaration, construction and initialization we should perform in a single line. If
we split it will give runtime exception.
int[ ] a;
a = {10, 20, 30}; it will give compile time exception.
length Vs length()
length is a final variable for an array object and length() is final method for string
objects.
length returns the size of an array and length() returns the number of characters in the
string.
Examples:
1. Every array object has final field length
int[ ] a = new int[6];
System.out.println(a.length);
O/P = 6
2. Every object has a final method called length()
String s = “Surya”
System.out.println(s.length());
O/P = 5
3. int[ ] a = new int[10]
System.out.println(a.length());
O/P = Compile time exception like unresolved symbol exception
4. int[ ] a = new int[6];
a.length; O/P = 6
a.length(); O/P = Compile time exception like unresolved symbol.
5. int[ ][ ] a = new int[3][2];
a.length; O/P = 3;
a[0].length; O/P = 2;
6) Anonymous arrays
Anonymous arrays are called name less arrays.
The purpose of these arrays is, if just in time use.
While creating anonymous arrays we are not allowed to specify the size, violation leads
to compile time exception.
Ex: new int[3] = {0, 3, 5}; O/P is Compile time exception.
16
7) Array Element assignment:
An array can access any element which can be implicitly promoted declared type.
In object arrays we can give any object as array element which is an instance of
declared class type (or) its change class instances.
8) Array reference assignments:
1. int [ ] a = new int[6];
char [ ] ch = {‘a’ , ‘c’ };
a=ch;----------------------------------Compile time exception like incompatible types.
2. int [ ]a = new int[6];
Char ch = new char[6];
int []b = a;………………correct
int [ ]b = ch;………….compile time exception.
Object array assignments:
In the case of object arrays the child array we can assign to the parent array variable.
1.6. UN initialized variables
1) Instance variables (Attributes/Member variables)
2) Static variables (Fields/class variable )
3) Local variables (Temporary/automatic/stack variable)
1) Instance variables:
The value of these variables varies from instance to instance. For every instance a
separate copy of these variables are created. These variables present as long as the
object present on the heap. These variables have to be declaring with in a class but
outside of nay method or constructor.
2) Static variables:
There is only one global copy of this variable present and shared by all instances
of that class. Hence these variables are considered as class level variables. (When ever
the class loaded into the memory static variables are created. As long as the class file is
in the JVMs memory the static variables will exist we can access a static variable by
using either object reference or class name).
3) Local variables:
The variables which are declared inside the method or inside a block are called as local
variables. The scope of this local variable is with in the method or block where the
variable declared.
Note:
In the case of instance variable and static variable if we are not performing any
initialization by default, default values will get.
The local variables never get a default values. We should perform initialization before
using a local variable.
No comments:
Post a Comment