Java Data Types

In programming, the datatype is the classification of a particular type of information. It is easy for humans to distinguish between different types of data. We can usually tell at a glance whether a number is a percentage, a time, or an amount of money.

We do this through special symbols -- %, and $ -- that indicate the data's type. Similarly, a computer uses special internal codes to keep track of the different types of data it processes. Examples of data types are. Integers, characters, floating point, strings, etc.

Datatype specifies the size, range, and type of values that can be stored in a variable. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory.

Therefore, by assigning different data types to variables, we can store integers, decimals, or characters in these variables. Java language is rich in its data types. Different data types allow us to select the type appropriate to the needs of the application.

Data types Architecture in Java

Basically, there are two datatypes available in java,

  • Primitive Data Type
  • Reference Data Type(Non-Primitive/Derived)
primitive-data-types-java

reference-data-types-java

Primitive Types :

Java defines eight primitive types of data: byte, short, int, long, char, float, double, and boolean. The primitive types are also commonly referred to as simple types, and both terms will be used in this article. These can be put in four groups:

  • Integers This group includes byte, short, int, and long, which are for whole-valued signed numbers.
  • Floating-point numbers This group includes float and double, which represent numbers with fractional precision.
  • Characters This group includes char, which represents symbols in a character set, like letters and numbers.
  • Boolean This group includes boolean, which is a special type for representing true/false values.

The primitive types represent single values but not complex objects. Although Java is otherwise completely object-oriented, the primitive types are not.

The primitive types are defined to have an explicit range and mathematical behavior. Languages such as C and C++ allow the size of an integer to vary based upon the dictates of the execution environment. However, Java is different. Because of Java’s portability requirement, all datatypes have a strictly defined range.

Integers

Java defines four integer types: byte, short, int, and long. All of these are signed, positive and negative values. Java does not support unsigned, positive-only integers.

intergers-table-java

It should be remembered that wide data types require more time for manipulation and therefore it is advisable to use smaller data types, wherever possible.

For example, instead of storing a number like 45 in an int type variable, we must use a byte variable to handle this number. This will improve the speed of execution of the program.

We can make integers long by appending the letter L or l at the end of the number.

Example :123L or  123l

Byte :

The smallest integer type is a byte. This is signed 8-bit type that has value range from –128 to 127. Variables of type byte are especially useful when you’re working with a stream of data from a network or file.

Byte variables are declared by use of the byte keyword. For example, the following declares two-byte variables called n and m:

byte n, m;

short :

short is a signed 16-bit type. It can have values from –32,768 to 32,767. It is probably the least-used Java type. Here are some examples of short variable declarations:

short s;

short t;

int:

The most commonly used integer type is int. It is a signed 32-bit type that has value range from –2,147,483,648 to 2,147,483,647. Using a byte or short would be more efficient than using an int in situations in which the larger range of an int is not needed, this may not be the case.

Long :

Long is a signed 64-bit type and is useful for those occasions where an int type is not large enough to hold the desired value. The range of a long is quite large.

For example, here is a program that computes the number of miles that light will travel in a specified number of days.

// Compute distance light travels using long variables.
class Light { public static void main(String args[]) { int lightspeed; long days; long seconds; long distance; // approximate speed of light in miles per second lightspeed = 186000; days = 1000; // specify number of days here seconds = days * 24 * 60 * 60; // convert to seconds distance = lightspeed * seconds; // compute distance System.out.print("In " + days); System.out.print(" days light will travel about "); System.out.println(distance + " miles."); } }

datatypes-java-integer

Floating-Point Types

Floating point numbers, also known as real numbers, are used when evaluating expressions that require fractional precision. For example, calculations such as square root.

There are two kinds of floating-point types, float and double, which represent single and double precision numbers, respectively.

float-data-type-java

float :

The type float specifies a single precision value that uses 32 bits of storage. Single precision is faster on some processors and takes half as much space as double precision.

Floats are useful when you need a fractional component, but don’t require a large degree of precision. For example, a float can be useful when representing dollars and cents.

Here are some example of float variable declarations:

float hightemp, lowtemp;

double :

Double precision, as denoted by the double keyword, uses 64 bits to store a value. Double precision is actually faster than single precision on some modern processors that have been optimized for high-speed mathematical calculations. All transcendental math functions,such as sin( ), cos( ), and sqrt( ), return double values.

Here is a short program that uses double variables to compute the area of a circle:

// Compute the area of a circle.
class Area { public static void main(String args[]) { double pi, r, a; r = 10.8; // radius of circle pi = 3.1416; // pi, approximately a = pi * r * r; // compute area System.out.println("Area of circle is " + a); } }

Characters in java

In Java, the data type used to store characters is char. Java uses Unicode to represent characters. Unicode defines a fully international character set that can represent all of the characters found in all human languages.

Thus, in Java char is a 16-bit type. The range of a char is 0 to 65,536.

There are no negative chars. The standard set of characters known as ASCII still ranges from 0 to 127 as always, and the extended 8-bit character set, ISO-Latin-1, ranges from 0 to 255.

Here is a program that demonstrates char variables:

// Demonstrate char data type.
class CharDemo { public static void main(String args[]) { char ch1, ch2; ch1 = 88; // code for X ch2 = 'Y'; System.out.print("ch1 and ch2: "); System.out.println(ch1 + " " + ch2); } }

char-datatype-java

Notice that ch1 is assigned the value 88, which is the ASCII (and Unicode) value that corresponds to the letter X.

Booleans

Java has a primitive type, called boolean, for logical values. It can have only one of two possible values, true or false. This is the type returned by all relational operators, as in the case of a < b. boolean is also the type required by the conditional expressions that govern the control statements such as if and for.

Here is a program that demonstrates the boolean type:

// Demonstrate boolean values.
class BoolTest { public static void main(String args[]) { boolean b; b = false; System.out.println("b is " + b); b = true; System.out.println("b is " + b); // a boolean value can control the if statement if (b) System.out.println("This is executed."); b = false; if (b) System.out.println("This is not executed."); // outcome of a relational operator is a boolean value System.out.println("10 > 9 is " + (10 > 9)); } }

boolean-data-type-java

First, as you can see, when a boolean value is output by println( ), "true" or "false" is displayed. Second, the value of a boolean variable is sufficient, by itself, to control the if statement.

Third, the outcome of a relational operator, such as <, is a boolean value. This is why the expression 10>9 displays the value "true."

Reference Data Types aka Objects

Reference types are complex types that are composed of primitive types and possibly other reference types. A reference type can be of arbitrary size, although once specified, all items (objects) of the same type will have the same size.

Reference types can be thought of as pointers that are implicitly de-referenced. The objects being referred to are dynamically allocated on the heap whereas primitive types are allocated automatically on the stack.

There are some points which should be noted.

  • 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, Student 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"); A better understanding of reference data type can be achieved after learning classes, constructors, arrays etc.
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions