Monday 1 October 2012

Data Types available in JAVA

There are two data types available in Java:
  1. Primitive Data Types
  2. Reference/Object Data Types

1. Primitive Data Types:

There are eight primitive data types supported by Java. Primitive data types are predefined by the language and named by a key word. Let us now look into detail about the eight primitive data types.
Java comes with eight primitive data types to handle simple data values. They can be split into four categories by the kind of value they hold:
  • Integers: these are positive and negative whole numbers.
  • Floating Point Numbers: any number that has a fractional part.
  • Characters: a single character.
  • Truth Values: either true or false.

Integers

Integers hold number values that cannot have a fractional part. There are four different types:
  • byte: uses one byte to store values from -128 to -127
  • short: uses two bytes to store values from -32,768 to 32,767
  • int: uses four bytes to store values from -2,147,483,648 to 2,147,483,647
  • long: uses eight bytes to store values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Floating Point Numbers

Unlike integers, floating point numbers like fractional parts. There are two different types:
  • float: uses four bytes to store values from -3.4028235E+38 to 3.4028235E+38
  • double: uses eight bytes to store values from -1.7976931348623157E+308 to 1.7976931348623157E+308

Characters

There is only one primitive data type that deals with individual characters – the char. The char can hold the value of one character and is based on 16-bit Unicode encoding. The character might be a letter, digit, punctuation, a symbol or a control character (e.g., a character value that represents a newline or a tab).

Truth Values

As Java programs deal in logic there needs to be a way to determine when a condition is true and when it is false. The boolean data type can hold those two values; it can only be true or false.


2. Reference Data Types:

  • Reference variables are created using defined constructors of the classes. They are used to access objects. These variables are declared to be of a specific type that cannot be changed. For example, Employee, Puppy etc.
  • Class objects, and various type of array variables come under reference data type.
  • Default value of any reference variable is null.
  • A reference variable can be used to refer to any object of the declared type or any compatible type.
  • Example : Animal animal = new Animal("giraffe");

No comments:

Post a Comment