Data types in java:
For storing different type of data
we have data types. Java defines eight types of data: byte, short, int,
long, float, double, char and boolean.
Integers:This group includes byte, short, int,
and long, which are for signed and unsigned number including zero.
Floating-point
numbers:This group includes float and double, which can
store numbers with fractional precision.
Characters:This group includes char, which
represents symbols in a characterset, like letters and numbers.
Boolean:This group includes boolean, which is a
special type for representing true/false values.
NOTE: The
data types mentioned above are primitive data types. Java also
support reference data types like Array, Class and Interface.
1. Integers:
Java defines four
integer types: byte, short,
int, and long.
All of these can have signed unsigned or zero value. Java does not support
unsigned, positive-only integers.
Name
Width (in bits)
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
08
–128 to 127
Note: Divide width
with 8 to get size in Bytes; the table mentioned above is providing width and
range of the integer data type. Width is the size the data type will occupy in
the program if you write int a,b; than a and b both will occupy 32 bits (4
bytes) in memory.
1.1 byte
The
smallest integer type is byte. This is a signed
8-bit type that has a range from –128 to 127
Ex.
byte b1;
Variables
of type byte are especially
useful when we’re working with a stream of data from a network or file.
1.2 short
Short
is a signed 16-bit type. It has a range from –32,768 to 32,767 so if you are
sure that your value will not more than the range of Short data type than and
only we recommended to use this.
Ex
short s1,s2;
1.3 int
The
most commonly used integer type is int. It
is a signed 32-bit type that has a range from –2,147,483,648 to 2,147,483,647.
Ex.
int a, b;
1.4 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. This makes it useful when
big, whole numbers are needed.
2. 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, or transcendental such as
sine and cosine, result in a value whose precision requires a floating-point
type.
Name Width
in Bits Approximate
Range
double
64
4.9e–324 to 1.8e+308
float
32
1.4e-045 to 3.4e+038
2.1 float
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, but will
become imprecise when the values are either very large or very small. When you
require to store floating point value use float and double.
Ex.
float r, pi;
2.2 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.
3. Characters:
Data type used to store
characters is char.
NOTE: However,
C/C++ programmers beware: char in Java is not the same as char in C or C++. In
C/C++, char is an integer type that is 8 bits wide. This is not the case in
Java. 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. In Java char require 16-bit. 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. The extended 8- bit character set,
ISO-Latin-1, ranges from 0 to 255. Since Java is designed to allow applets to
be written for worldwide use, it makes sense that it would use Unicode to represent
characters.
4. Boolean:
Java has a simple 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,
such as a < b.
Ex.
boolean b; So here the value of b is either true or false.
Operator: It is a symbol that tells the computer to perform
mathematical and logical calculation.
Java supports basically 6 types of operators.
1. Arithmatic operator
2. Bitwise operator
3. Relational operator
4. Boolean Logical operator
5. Assignment operator
6. Condetional operator
Selection
Statements
If
selection statements
Switch
selection statement
Looping
statements
Jump
Statements
Comments
Post a Comment