JavaIntellectualTricks

Data Type

In java every variable and every expression has some type.
Each and every data type is clearly defined .
Every assignment should be checked by compiler for type compatibility.
Because of above reasons, we can conclude java language is strongly typed programming language.

Example:
int x=10.5; (invalid due to java is strongly typed , here the provided value type and expected value type both should be same for achieving strongly type.)
boolean b=0; (invalid due to java is strongly typed , here the provided value type and expected value type ,both should be same for achieving strongly type.)
But these two are valid in C Language as C Language is a loosely typed programming language.

Question: 
Is java a pure object oriented language?

Answer- Yes/No

Yes point of view:- (if u compare with old object oriented language)
As compare to old languages like c++ java has some more oop features like multiple inheritance.
So in a comparison based java is said to be a pure object oriented language.

No point of view:-(If u consider as alone)(Recommended Answer)
Java is not considered as pure object oriented programming language because several oop features are not satisfied by java (like operator overloading, multiple inheritance etc)
Moreover we are depending on primitive data types which are non objects.

Types of Data Types:

Except boolean and char the remaining data types are considered as signed data types because we can represent both positive and negative numbers.

Integral Data Types:

byte:
  1. size= 1 byte= 8 bits
  2. signed data type
Max Value --> +127 Min Value --> -128 Range --> -128 to +127

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 where as – ve numbers will be represented in 2's complement form. i.e 2's complement of 01111111 is 10000000 so
1^7 + 0^6 + 0^5 + 0^4 + 0^3 + 0^2 + 0^1 + 0^0= 128 (Hence min -ve value= -128)

Example:
  1. byte b=10; (valid)
  2. byte b=127; (valid)
  3. byte b=128; (invalid, gives compile time error, “possible loss of precession”, found int, required byte)
  4. byte b=10.5; (invalid, gives compile time error,” possible loss of precession”, found double, required byte)
  5. byte b= true; (invalid, gives compile time error, “incompatible type”, found boolean, required byte)
  6. byte b= “purna”; (invalid, gives compile time error, “incompatible type”, found java.lang.String, required byte)
Note: byte is the best choice if we want to handle data in terms of streams either from the file or from the network.(file supported form and network supported form is byte)

short:
  • This is the most rarely used data type in java
  • size=2bytes(16 bits)
  • range= -2^15 to 2^15-1
    = -32768 to 32767
Example:
  1. short s=32767 (valid)
  2. short s=32768 (invalid, gives compile time error, “possible loss of precession”, found int, required short)
  3. short s=10.5 (invalid, gives compile time error, “possible loss of precession”, found double, required short)
  4. short s=true (invalid, gives compile time error, “incompatible type”, found boolean, required short)

Note: short data type is best suitable for 16 bits processors like 8085 but these processors are completely outdated and hence corresponding short data type is also outdated data type.

int:

  • The most commonly used data type in java is int.
  • Size= 4bytes(32bits)
  • Range- -2^31 to 2^31-1 i.e -2,147,483,648 to 2,147,483,647
Example:
  1. int x=2,147,483,647; (valid)
  2. int x=2,147,483,648; (invalid,gives compile time error, “integer number too large”)
  3. int x=2,147,483,648 L;(invalid, gives compile time error, “possible loss of precession”, found long, required int)
  4. int x=true; (invalid, gives compile time error, “incompatible type”, found boolean, required int)

long:

Sometimes int may not enough to hold big values, then we should go for long type.

Example 1: The amount of distance traveled by light in 1000 days, to hold this value int may not enough, we should go for long data type.

Long l=1026000*60*60*24*1000 miles

Example 2: The number of characters present in a big file may exceed int range , hence the return type of length() is long but not int.
File f=new File();
Long l=f.length();

size=8 bytes(64 bits)
Range= -2^63 to 2^63-1

Note: 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:


float
double
1
size is 4 bytes.
size is 8 bytes
2
if we want 5 to 6 decimal places of accuracy, then we should go for float.
If we want 14 to 15 decimal places of accuracy, then we should go for double.
3
float follows single precission.
double follows double precession.
4
Range: -3.4e38 to 3.4 e38
Range: -1.7e308 to 1.7e308

boolean Data Type:
  • Size not applicable(virtual machine dependent.).
  • Range not applicable(but allowed values are true or false).
Example:
  1. boolean b=true; (valid)
  2. boolean b=0; (invalid, gives compile time error, “incompatible type”, found int, required boolean)
  3. boolean b=True; (invalid, gives compile time error, “can't find symbol”, class Test)
  4. boolean b=”True”; (invalid, gives compile time error, “incompatible type”, found java.lang.String, required boolean)
Ex.1                                                                             Ex. 2
class Test{                                                                   class Test{
public static void main(String []args){                        public static void main(String []args){
int x=10;                                                                       while(1){
if(x){                                                                             System.out.println(“Hello”);
System.out.println(“Hello”);                                        }

else{                                                                          }
System.out.println(“Hello”);
}                                                                                  
}


These two programs are valid in case of c & c++, but in java it gives error.

Here both the program will give compilation error “Incompatible Types ” found int, required boolean. Because if and while loop condition needs a boolean value only in case of java, and as java is strongly typed programming language this is not supported in java.

char Data Type:


size = 2 bytes (in c & c++ it is 1 byte)
Range= 0 to 65535

Question: 
Why char occupies 2 bytes of memory in case of java?

Old languages (c,c++) are ASCII code based and the number of different allowed ASCII characters are less than or equal to 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 greater than 256 and less than equal to 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.

Summary of java primitive data types:


Data Type
Size (in byte)
Range
Wrapper class
Default Value
byte
1
-128 to 127
Byte
0
short
2
-32768 to 32767
Short
0
int
4
-2,147,483,648 to 2,147,483,647
Integer
0
long
8
-2^63 to 2^63-1
Long
0
float
4
-3.4e38 to 3.4e38
Float
0
double
8
-1.7e308 to 1.7e308
Double
0.0
char
2
0 to 65535
Character
0.0
(represents space char)
boolean
NA
NA, but allowed values are true and false
Boolean
false

Note: null is the default value for object reference and we can't apply for primitives .
If we are trying to use for primitive, then we ll get compile time error.

Example:
char ch=null; // Incompatible type, found: null type, required: char


1 comment: