DATA TYPES:-
Every variable has a type, every expression has a type and all types are strictly define
more over every assignment should be checked by the compiler by the type
compatibility hence java language is considered as strongly typed programming
language.
Java is pure object oriented programming or not?
Java is not considered as pure object oriented programming language because several
oops features (like multiple inheritance, operator overloading) are not supported by
java moreover we are depending on primitive data types which are non objects.
Diagram:
Except Boolean and char all remaining data types are considered as signed data types
because we can represent both "+ve" and"-ve" numbers.
Integral data types :
Byte:
Size: 1byte (8bits)
Maxvalue: +127
Minvalue: -128
Range: -128to 127[27 to 27-1]
- The most significant bit acts as sign bit. "0" means "+ve" number and "1" means "–ve" number.
- "+ve" numbers will be represented directly in the memory whereas "–ve" numbers will be represented in 2's complement form.
Example:
byte b=10;
byte b2=130;//C.E:possible loss of precision
found : int ,required : byte
C.E=COMPILE TIME ERROR.
byte b=10.5;//C.E:possible loss of precision
byte b=true;//C.E:incompatible types
byte b="rehan";//C.E:incompatible types
found : java.lang.String
required : byte
byte data type is best suitable if we are handling data in terms of streams either from
the file or from the network.
Short:
The most rarely used data type in java is short.
Size: 2 bytes
Range: -32768 to 32767(215 to 215-1)
Example:
short s=130;
short s=32768;//C.E:possible loss of precision
short s=true;//C.E:incompatible types
Short data type is best suitable for 16 bit processors like 8086 but these processors are
completely outdated and hence the corresponding short data type is also out data type.
Int:
This is most commonly used data type in java.
Size: 4 bytes
Range:-2147483648 to 2147483647 (-231 to 231-1)
Example:
int i=130;
int i=10.5;//C.E:possible loss of precision
int i=true;//C.E:incompatible types
long:
Whenever int is not enough to hold big values then we should go for long data type.
Example:
To hold the no. Of characters present in a big file int may not enough hence the return
type of length() method is long.
long l=f.length();//f is a file
Size: 8 bytes
Range:-263to 263-1
Note: All the above data types (byte, short, int and long) can be used to represent whole
numbers. If we want to represent real numbers then we should go for floating point
data types.
Floating Point Data types:
boolean data type:
Size: Not applicable (virtual machine dependent)
Range: Not applicable but allowed values are true or false.
Which of the following boolean declarations are valid?
Example 1:
boolean b=true;
boolean b=True;//C.E:cannot find symbol
boolean b="True";//C.E:incompatible types
boolean b=0;//C.E:incompatible types
Example 2:
Char data type:
- In old languages like C & C++ are ASCII code based the no.Of ASCII code characters are < 256 to represent these 256 characters 8 - bits enough hence char size in old languages 1 byte.
- In java we are allowed to use any worldwide alphabets character and java is Unicode based and no.Of unicode characters are > 256 and <= 65536 to represent all these characters one byte is not enough compulsory we should go for 2 bytes
Size: 2 bytes
Range: 0 to 65535
Example:
char ch1=97;
char ch2=65536;//C.E:possible loss of precision
Comments
Post a Comment