Saturday, May 19, 2007

Primitive Data Types

Java is Strongly typed. meaning is we have to declare the variable before we can use them.

e.g. double speed=30;

Above statement in a code indicates that field named 'speed' exists and holds a numeric value.Fields data type determines what kind of value it will hold (whole number/with fractional part etc.)like 'int' will have only whole number whereas double (in above example) can have fractional part as well.

Apart from int,double there are 6 more data types in Java.Let's go into the details of all one by one.

1) byte
8-bit Signed Integer
Minimum Value = -128 (Inclusive)
Maximum Value = 127 (Inclusive)
Negative numbers are stored in 2's complement form.

2)short
16-bit Signed Integer
Minimum Value = -32,768(Inclusive)
Maximum Value = 32,767 (Inclusive)
Negative numbers are stored in 2's complement form.

We can use a 'byte' and 'short' to save memory in large arrays (if we know that value will not cross maximum & minimum limit), in situations where the memory savings actually matters.

3)int
32-bit Signed Integer
Minimum Value = -2,147,483,648(Inclusive)
Maximum Value = 2,147,483,647(Inclusive)
Negative numbers are stored in 2's complement form.


4)long
64-bit Signed Integer
Minimum Value = -9,223,372,036,854,775,808 (Inclusive)
Maximum Value = -9,223,372,036,854,775,807 (Inclusive)
Negative numbers are stored in 2's complement form.


5)float
single-precision 32-bit IEEE 754 floating point


6)double
single-precision 64-bit IEEE 754 floating point

As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating point numbers.



7)boolean
Represents one bit of information.Only two possible values true and false.
Usage of this data type is as a simple flags to track true/false conditions.
It's size is not defined precisely


8)char
single 16-bit Unicode character
Minimum Value = '\u0000' (Inclusive)
maximum Value = '\uffff' (Inclusive)

Java also provides special support for character strings via the 'String' class.
Although String is not a Primitive Data type.
for example,
String s = "this is a simple string";

String objects are immutable, which means that once created, their values cannot be changed.

Default Values of Primitive Data Types

Fields (class level data members) that are declared but not initialized will have some default values. These values will be assigned by the compiler. These default values are either Zero ('0') or null.

For byte,short, int default value is '0'.
For long it id '0L'
For Float it's 0.0f,whereas default value for double is 0.0
char will have '\u0000' as a default value whereas String (or any other object for that matter) has 'null' as its default value.
boolean has 'false' as it's default value.

Note:
1) Local variables are never initialized by the compiler.
2) As a programmer, we have to initialize a local variable where it is declared.
3) If we try to access an uninitialized local variable , we will get a compile time error.

2 comments:

Dhanashree said...

Hi
Can u plz guide me on the topic 'pass by reference or pass by object'

Shrinivas Mujumdar said...

Surely. Actually i was not feeling well for last few days that's why i could not post anything.

I will post about this very soon.

Thanks
Shriniwas

 
online degree programs
visit us .