You are on page 1of 28

Java Identifiers and Reserved Words

 Except keywords we can use any names as variable names.

Data Types part-1

where byte is useful: the best suitable byte is the data transfer as bytes not like int or char.
Byte is the best choice if you want to handle data in terms of streams either from the file or from the
network (File supported form/network support form) Is byte.

 Short data type is best suitable for 16 bit processors like 8085 but these processors are completely
outdated and hence the corresponding short data type is also out dated data type.
Data Types part-2
 If we want to hold 1000 days of light speed

Sometimes int may not enough to hold big values then we should go for long type.
Example 1: The amount of distance travelled by light in 1000 days, to hold this value int may not enough we
should go for long data type.
Example 2:
The number of characters present in a big file may exceed int range. Hence the return type of length method is
long but not int.

 All the above data types(byte, short, int, long) meant for representing integral values. If we want to
represent floating point values then we should go for floating point data types.
Floating point data types:
Boolean:
Char Data type:
Old languages like C or C++ are ASCII code based and the number of different allowed ASCII code characters
are <= 256. To represent these 256 characters 8 bits are enough hence the size of char in old languages is 1
byte.
But Java is Unicode based and the number of different Unicode characters are >256 and <=65536
To represent these many characters 8 bits may not enough compulsory we should go for 16 bits.
Hence the size of char in java is 2 bytes.
null is the default value for object reference and we can’t apply for primitives. If we are trying to use for
primitives then we will get compile time error.
Example:

Literals Part-1
 Integral Literals:
For integral data types(byte, short, int, long) we can specify literal value in the following ways.

for extra digits (a-f) we can use both the lower case and upper case characters. This is one of very few areas
where java is not case sensitive.
 The literal value should be prefixed with 0x or 0X
 Int x = 0x10;
these are the only possible ways to specify literal values for integral data types.
 There is no direct way for byte and short values.
 If we are assigning integral literal to the byte and the value within the range of byte compiler
automatically treat the value as byte. Same rule applicable for short.

By default the integral data type is int. that’s why compiler always complains found int.
By default every floating point literal is of double type and hence we can’t assign directly to the float
variable.
But we can specify floating point literal as float type by suffixed with f or F.

we can specify explicitly floating point literal as double type by suffixed with d or D of course this convention
is not required.
we can specify floating point literals only in decimal format and we can’t specify in octal and hexa decimal
forms.
we can assign integral literal directly to floating point variables and that integral literal can be specified
either in decimal or octal or hexa decimal forms.

 We can’t assign floating point literals to integral types

we can specify floating point literal even in exponential form(sciencific notation).

 Compile time error.

 The only allowed values in Boolean are true/false.


Literals part-2
We can specify char literal as single character within single quotes

 We can specify char literal as integral literal which represents Unicode value of the character and that
integral literal can be specified in either in decimal or octal or hexa decimal forms but allowed range is
0 to 65535.
 When we print 1970…etc we see ? always
 Reason:
1. there is no character is defined with the number
2. our machine has no such font to print

 We can represent char literal in Unicode representation which is nothing but ‘\uXXXX’. Here XXXX
means 4 digit hexa decimal number.
 Every escape character is a valid char literal.
Different ways to assign literals to char data types.

 Double quotes is treated as string literal

1.7 enhancements with respect to literals


1. Binary Literals:
For integral data types until 1.6 we can specify literal value in the following ways.
1. Decimal form
2. Octal form
3. Hexa decimal form
But from 1.7 version onwards we can specify literal value even in binary form also. Allowed digits are 0 and 1.
Literal value should be prefixed with 0b or 0B
Example:
2. Usage of _ symbol in numeric literals:
We can have _ symbols between digits of numeric literal
Example:

 The main advantage of this approach is the readability of the code will be improved.
 At the time of compilation these _ symbols will be removed automatically. Hence after compilation the
above line will become

 You can use the _ symbols

 We can use _ symbol only between the digits. If we are using anywhere else we will get compile time
error.
Note:
8 byte long value we can assign to 4byte float variable because both are following different memory
representations internally.
 Short and char cannot be assignable for each other since char is unsigned(max 65535) but short is
signed data type(max 32767) one bit is for sign.
Arrays Part-1
The main advantage of arrays is we can represent huge number of values by using single variable. So that
readability of the code will be improved but the main disadvantage of arrays is fixed in size. i.e. once we create
an array there is no chance of increasing or decreasing the size based on our requirement.
To use arrays concept compulsory we should know size which may not possible always.
One Dimensional Array declaration:

At the time of declaration we can’t specify the size otherwise we will get compile time error.

Two dimensional Array:

If we want to specify dimension before the variable the facility is applicable only for first variable in a
declaration. If we are trying to apply for remaining variables we will get compile time error.


Every array in java is an object only hence we can create arrays by using new operator.

 Each Array has corresponding class which is in java.lang and it will not be available at programmer.
 For every array type corresponding classes are available and these classes are part of java language
and not available to the programmer level.
 Compulsory we should specify the size.

 It is legal to have an array with size zero.

 If we are trying specify the array size with negative int value then we will get runtime exception saying
NegativeArraySizeException.

To specify array size allowed data types are byte,short,char,int

If you are trying to specify any other types then we will get compile time error.
Note:
The maximum allowed array size in java is 2147483647 which is the maximum value of int data type.
some times memory error at runtime because of not enough memory in jvm to allocate the space.
Even in the first case we may get runtime exception if sufficient heap memory not available.
Arrays part-2
Two dimensional array creation:
In java 2D array not implemented by using matrix style. Some people followed array of arrays approach for
multi-dimensional array creation.
The advantage of this approach is memory utilization will be improved.

Example 1:

Example 2:
Base size should be there.

Array Initialization:
Once we create an array every array element by default initialized with default values.

Internally toString method will be called which is implemented by default to return the string in the
following form.
Example 3:

when we perform any operation on null then it will return nullpointerexception


Once we creates an array every array element by default initialized with default values.
If we are not satisfied with default values then we can override these values with our customized values.
If we are trying to access array index out of range index either +ve or –ve int value then we will get runtime
exception saying ArrayIndexOutOfBoundsException.
Arrays Part-3
Array Declaration, creation & Initialization in a single Line:

We can extend this shortcut for multi dimensional arrays also.
All Activities in a single line. If we are trying to devide into multiple lines then we will get compile time error.
Length is a final variable applicable for arrays.
length variable represents the size of the array.
Example:
In multi dimensional array length variable represents only base size but not total size.

Total Length Calculate:

Anonymous Array:
An Array without name is called as anonymous array.
Such type of nameless arrays are called anonymous arrays.
The main purpose of Anonymous arrays is just for instant use.(1 time usage)
We can create anonymous array as follows.
while creating anonymous arrays we can’t specify the size otherwise we will get compile time error.
Based on our requirement we can give the name for anonymous array then it is no longer anonymous.

Case 1:
In the case fo primitive type arrays as array elements we can provide any type which can be implicity
promtoed to declared type.
 Its implementation classes are allowed.

Arrays Part-4
Element level promotions are not applicable at array level for example Char element can be promoted to int
type where char array cannot be promoted to int array.
Example:
There no relation between the char array class and int array class.
 In the case of References child class is promoted to parent

Internal elements won’t be copied just reference variables will be reassigned.


Case 3:
Whenever we are assigning one array to another array the dimensions must be matched for example in the
place of one dimensional int Array we should provide one dimensional array only. If we are trying to provide
any other dimension then we will get compile time error.

whenever we are assigning one array to another array both types and dimensions must be matched but
sizes not required to match.

You might also like