Skip to main content

Data types


Data types: Every variable has a type, every expression has a type and all types are strictly define moreover every assignment should be checked by the compiler by the type compatibility hence java language is considered as strongly typed 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 inheritances, operator overloading) are not supported by java moreover we are depending on primitive data types which are non-objects.
Diagram:
Except for Boolean and char, all remaining data types are considered assigned data types because we can represent both “+ve” and”-ve” numbers.
Byte:
     Size: 1byte (8bits) Max value: +127 Min value:-128 Range:-128to 127[-27 to 27 -1]
 • The most significant bit acts assign 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
byte b=10.5;//C.E: possible loss of precision
byte b=true;//C.E:incompatible types
byte b="Navneet";//C.E:incompatible types
 a 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
• The 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 the 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 a 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:-263 to 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:
float double
1) If we want to 5 to 6 decimal places of
accuracy then we should go for afloat.
1) If we want to 14 to 15 decimal places
of accuracy then we should go for
double.
2) Size:4 bytes. 2) Size:8 bytes.
3) Range:-3.4e38 to 3.4e38. 3) -1.7e308 to1.7e308.
4) float follows a single precision.
4) double follows double-precision
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:can not find symbol
              boolean b="True";//C.E:incompatible types
              boolean b=0;//C.E:incompatible types
Example 2:
Char data type:
In java, we are allowed to use any worldwide alphabets character and java is Unicode based 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
Example:
char ch1=97;
char ch2=65536;//C.E:possible loss of precision
        
The default value for the object references is “null”.  

Comments

Popular posts from this blog

Core Java

Content 1 - Language fundamental   Identifier Reserved words Data types Literals Arrays Types of variables Var arg method Main method Java coding standards 2 -  Declaration and Access Modifiers Java source file structure Class modifiers Member modifiers Interfaces 3 - Exception Handling Introduction Runtime stack mechanism Default exception handling in java Exception hierarchy Customized exception handling by try-catch  Control flow in try-catch Methods to print exception information Try with multiple catch blocks Finally Difference between final, finally, finalize Control flow in try-catch-finally Control flow in nested try-catch-finally Various possible combinations of try-catch-finally throw keyword throws keyword Exception handling keywords summary Various possible compile-time errors in exception handling Customized exceptions Top-10 exceptions  4 - String Concept of String Immutable String String Comparison String Conca