Professional Documents
Culture Documents
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 specify floating point literal even in exponential form(sciencific notation).
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.
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
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.
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.
If we are trying specify the array size with negative int value then we will get runtime exception saying
NegativeArraySizeException.
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:
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.
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
whenever we are assigning one array to another array both types and dimensions must be matched but
sizes not required to match.