Tuesday 28 February 2017

Java Data Types




Java Data Types


Java Data Type

Primitive data types in Java can be divided into three main categories:
Integral types— represent signed integers (byte, short, int, long) and unsigned character values (char)
Floating-point types (float, double)— represent fractional signed numbers
Boolean type (boolean)— represent logical values
 Primitive data values are not objects. Each primitive data type defines the range of values in the data type, and operations on these values are defined by special operators in the language (see Chapter 3).
Each primitive data type also has a corresponding wrapper class that can be used to represent a primitive value as an object. .

Integer Types:

The integer types are for numbers without fractional parts. Negative values are allowed. Java provides the four integer types shown in bellow Table.

Java integer data type

Under Java, the ranges of the integer types do not depend on the machine on which you will be running the Java code. This alleviates a major pain for the programmer who wants to move software from one platform to another, or even between operating systems on the same platform. In contrast, C and C++ programs use the most efficient integer type for each processor. As a result, a C program that runs well on a 32-bit processor may exhibit integer overflow on a 16-bit system. Because Java programs must run with the same results on all machines, the ranges for the various types are fixed.
For example
byte b=25;
short a=100;
int b=275;
long c=1099;

Character Type:

Characters are represented by the data type char. Their values are unsigned integers that denote all the 65536 (216) characters in the 16-bit Unicode character set. This set includes letters, digits, and special characters.
For example
char gender='m';

Java char data type

The first 128 characters of the Unicode set are the same as the 128 characters of the 7-bit ASCII character set, and the first 256 characters of the Unicode set correspond to the 256 characters of the 8-bit ISO Latin-1 character set.

Floating-Point Types:

The floating-point types denote numbers with fractional parts. The two floating-point types are shown in bellow Table.

Java floating data type

Floating-point numbers are represented by the float and double data types.
The range of values for positive floating-point numbers, but these apply equally to negative floating-point numbers with the '-' sign as prefix. Zero can be either 0.0 or -0.0.
For example
float f=32.3800;
double d=38.360;
The name double refers to the fact that these numbers have twice the precision of the float type. (Some people call these double-precision numbers.) Here, the type to choose in most applications is double. The limited precision of float is simply not sufficient for many situations. The only reasons to use float are in the rare situations in which the slightly faster processing of single-precision numbers is important or when you need to store a large number of them.
Another example
float f=25.312F
double d=325.235D
We can specify a float by putting f or F suffix and similar a double by putting d or D suffix. Note that floating-point numbers without an f or F suffix (such as 3.402) are always considered to be of type double.

Boolean Type:

java boolean data type

The data type boolean represents the two logical values denoted by the literals true and false.
For example
boolean b=true;
Boolean values are produced by all relational, conditional and boolean logical operators and are primarily used to govern the flow of control during program execution.

java primitive data type



 Next Topic Shahbaz

0 comments:

Post a Comment

Powered by Blogger.

Stats