Wednesday 8 August 2018

DATA TYPES



A Data type is the type of data that can be stored into the variable. A description on a variable
that determines what kind of information you can enter in the variable.
There are seven primitive data types, which are supported by Java language programming. A
primitive data type is a data type, which is predefined in Java. Following are the eight
The term “ Data Type” refers to the type of data that can be stored in a variable.
Java is sometimes called a “ strongly typed ” language.
Java has two types of data types.
1. Primitive data types.
2. Reference data types.

Primitive data types:
Primitive data types are the data types that are defined by the language Itself.
The memory location associated with a primitive types variables contains the actual value of
the variable.It is also called as Value types.

Java’s Primitive Types
Type Explanation
int A 32-bit (4-byte) integer value
short A 16-bit (2-byte) integer value
long A 64-bit (8-byte) integer value
byte An 8-bit (1-byte) integer value
float A 32-bit (4-byte) floating-point value
double A 64-bit (8-byte) floating-point value
char A 16-bit character using the Unicode encoding scheme
boolean A true or false value

primitive data types:
int
It is a 32-bit signed two's complement integer data type. It ranges from -2,147,483,648 to
2,147,483,647. This data type is used for integer values. However for wider range of values
use long.

byte
The byte data type is an 8-bit signed two's complement integer. It ranges from -128 to127
(inclusive). We can save memory in large arrays using byte. We can also use byte instead of
int to increase the limit of the code.

short
The short data type is a 16-bit signed two's complement integer. It ranges from -32,768 to
32,767. short is used to save memory in large arrays.

long
The long data type is a 64-bit signed two's complement integer. It ranges from -
9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Use this data type with larger
range of values.

float
The float data type is a single-precision 32-bit IEEE 754 floating point. It ranges from
1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative). Use a float
(instead of double) to save memory in large arrays. We do not use this data type for the exact
values such as currency. For that we have to use java.math.BigDecimal class.

double
This data type is a double-precision 64-bit IEEE 754 floating point. It ranges from
4.94065645841246544e-324d to 1.79769313486231570e+308d (positive or negative). This
data type is generally the default choice for decimal values.

boolean
The boolean data type is 1-bit and has only two values: true and false. We use this data type
for conditional statements. true and false are not the same as True and False. They are defined
constants of the language.
char
The char data type is a single 16-bit, unsigned Unicode character. It ranges from 0 to 65,535.
They are not same as ints, shorts etc.
/* Program on Datatypes */
class datatypes
{
public static void main(String []args)
{
int a=457647657;
byte b=12;
short c=435;
long d=345;
float g=3.5f;
double f=345.6;
char k='a';
boolean h=false;
System.out.println("Integer="+a);
System.out.println("Byte="+b);
System.out.println("Short="+c);
System.out.println("Long="+d);
System.out.println("Float="+g);
System.out.println("Double="+f);
System.out.println("Character="+k);
if(h==true)
System.out.println("Boolean value is true");
else
System.out.println("Boolean value is false");
}
}
Output:
E:\sysvol>javac datatypes.java
E:\sysvol>java datatypes
Integer=457647657
Byte=12
Short=435
Long=345
Float=3.5
Double=345.6
Character=a
Boolean value is false
Reference data types :
Reference types are types that are defined by the language java API rather than by the
language itself.
• The memory location associated with a reference tpes variable contains an address
(called pointer) that indicates the memory location of the actual object.

No comments: