Tutorialslot.com

youtube facebook twitter gmail pinterest

Data Types in Java

The 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.These can be put in four groups:
• Integers - includes byte, short, int, and long, which represents the whole-valued signed numbers.
• Floating-point numbers - includes float and double, which represent numbers with fractional precision.
• Characters -includes char, which represents symbols in a character set,like letters and numbers.
• Boolean - includes boolean, which is a special type for representing true/false values.

Integers
They are signed, positive and negative values. Java does not support unsigned, positive-only integers. Many other computer languages support both signed and unsigned integers.The width of an integer type should not be thought of as the amount of storage it consumes,but rather as the behavior it defines for variables and expressions of that type. The Java run-time environment is free to use whatever size it wants, as long as the types behave as you declared them. The width and ranges of these integer types vary widely, as shown in this table:

NAME WIDTH RANGE
long 64 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
int 32 2,147,483,648 to 2,147,483,647
short 16 32,768 to 32,767
byte 8 128 to 127

Byte:
It is the smallest integer type(8-bits) that range from-128 to 127.They are more useful when working with a stream of data or raw binary data that cannot be directly compatible.They are declared by using the keyword "byte".
For example:
byte x, y;

Short:
It is the least used data type of signed 16-bits. It range from –32,768 to 32,767.They are declared by using the keyword "short".
For example:
short a;
short b;

Int:
It is the most commonly used data type.It is a signed 32-bit type that range from –2,147,483,648 to 2,147,483,647.They are mainly used in control loops and index arrays concept.It is declared by the keyword "int".Although you might think that 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. The reason is that when byte and short values are used in an expression they are promoted to int when the expression is evaluated.Therefore, int is often the best choice when an integer is needed.

Long:
It is a signed 64-bit type and it is used only in some cases where an integer type is not large enough to hold the desired values.It's range is very big.

Floating-Point Types:
Floating-point numbers, also known as real numbers.They are used for evaluating expressions that require fractional values.For example, calculations such as square root, or transcendentals such as sine and cosine, result in a value whose precision requires a floating-point type.There are two kinds of floating-point types, float and double, which represent single- and double-precision numbers, respectively. Their width and ranges are shown here:

Name Width in Bits Approximate Range
double 64 4.9e–324 to 1.8e+308
float 32 1.4e–045 to 3.4e+038

Float:
The float type specifies a 32-bits of storage.It is uesd only when a fractional component is required but not a large degree of precisions.For instance it can be used for representing dollars and cents.
For example:
float temp;

Double:
It specifies a 64-bits of storage.All mathematical functions like sin(),cos(),sqrt() can be found and stored with the help of double values.In order to maintain accuracy double will be the best one.

Characters:
It is a 16-bit type used for storing all characters.It is represented by the keyword "char".It range from 0 to 65,536.The negative characters are always not allowed.

Boolean:
It is used only for logical values. It can have only one of two possible values, true or false.It is mainly used for all relational operators, as in the case of a

Advertisement Powered By Google Adsense






IF YOU LIKE THIS TUTORIALS PLEASE SHARE

Share on whatsapp

POPULAR TAGS

Java java tutorials learn java java programming tutorial learn java online learning java professionals tutorials Multithreading Generics Object Oriented Language Methods Polymorphism Inheritance