what are the primitive data types in java

 Data types: Every variable has a type, every expression has a type and all types are strictly defined over every assignment should be checked by the compiler by the type compatibility hence java language is considered as strongly typed language.

what are the primitive data types in java


Except Boolean and char all remaining data types are considered as signed data types because We can represent both '+ve' and '-ve' numbers.


Data Types:

  • byte:

The most rarely used data type in java is byte

Size: 1byte (8bits).

Max Value: +127

Min Value: -128

Range:-128 to 127


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="Reshma";//C.E:incompatible types

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


Example:

short s=130;

short s=32768;//C.E:possible loss of precision

short s=true;//C.E:incompatible types


  • int:

This is most commonly used data type in java.

Size: 4 bytes

Range:-2147483648 to 2147483647

 

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 I=file.length();

Size: 8 bytes

Range:-2^63 to 2^63-1


  • boolean:

The only allowed values for the boolean type are true (or) false where case is important.

Size: Not applicable (virtual machine dependent)

Range: Not applicable but allowed values are true or false.


Example 1:

boolean b=true;

boolean b=True;//C.E:cannot find symbol

boolean b="True";//C.E:incompatible types

 

Example 2:



  • char:

A char literal can be represented as single character within single quotes. 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


             Read more Most popular Programming Languages


Floating  Point  Literals

Floating point literal is by default double type but we can specify explicitly as float type by suffixing with f or F.

 

  • float:

we can specify explicitly as float type by suffixing with f or F.

Size: 4 byte

Range: 1.175494351 E - 38 to 3.402823466 E + 38

 

Example:

float f=123.456;//C.E:possible loss of precision(invalid)

float f=123.456f;(valid)

 

 

  • double:

We can specify explicitly floating point literal as double type by suffixing with d or D.

Size: 8 byte

Range: 2.2250738585072014 E - 308 to 1.7976931348623158 E + 308

 

Example:

double d=123.456;(valid)


FAQ:

Which of the following floating point declarations are valid?

1)float f—123.456;//C.E:possible loss of precision(invalid)

2)float f-123.456D;//C.E:possible loss of precision(invalid)

3)double d=0x123.456;//C.E:malformed floating point literal(invalid)

4)double d=0xFace;(valid)

5)double d=0xBeef;(valid)

Post a Comment

3 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.