Breaking

Friday, March 1, 2019

data types in java with example

data types in java with example

Firstly short brief DATA TYPES-

: DATA Types specify the different sizes and values that can be stored in the various.

There area unit 2 kinds of information sorts in JAVA:-

PRIMITIVE DATA TYPES

NON-PRIMITIVE DATA TYPES


➤PRIMITIVE DATA TYPES:-

A primitive information kind specifies the scale and kind of variable values, and it's no extra strategies.

THE PRIMITIVE DATA TYPES INCLUDE-

BOOLEAN

CHAR

BYTE

SHORT

INT

LONG

FLOAT

DOUBLE

The Java programing language supports eight primitive knowledge varieties. All variables should initial be declared before they'll be employed in Java. A variable’s data type determines the values it may hold and the operations that may be performed upon it. A primitive sort is predefined by the language and is known as by a reserved keyword.
byte
SIZE: 8-bit integer
MIN_VALUE : -128
MAX_VALUE: 127
short
SIZE: 16-bit integer
MIN_VALUE : -32768
MAX_VALUE: 32767
int
SIZE: 32-bit integer
MIN_VALUE : -2147483648
MAX_VALUE: 2147483647
long
SIZE: 64-bit integer
MIN_VALUE : -9223372036854775808L
MAX_VALUE : 9223372036854775807L

➤NON-PRIMITIVE DATA TYPES:-

The non-primitive data types include

CLASSES

INTERFACES

ARRAYS

JAVA PRIMITIVE DATA TYPES:-

In Java language, primitive knowledge varieties square measure the building blocks of information manipulation. These area units the foremost basic information sorts accessible in JAVA language.

These are 8 types of PRIMITIVE DATA TYPES-

➤ BOOLEAN DATA TYPE

➤ BYTE DATA TYPE

➤ CHAR DATA TYPE

➤SHORT DATA TYPE

➤INT DATA TYPE

➤LONG DATA TYPE

➤FLOAT DATA TYPE

➤DOUBLE DATA TYPE

BOOLEAN DATA TYPE:-

➭The Boolean data types are used to share only two possible values:

TRUE and FALSE

➭This information kind is employed for straightforward flags that track true/false

conditions

BOOLEAN - 1 bit

➭ It can have only one of two possible values true or false

TRUE - 1 (Stored)

FALSE- 0 (Stored)

Example: Boolean one=false.


 BYTE DATA TYPE

The computer memory unit information sort is Associate in Nursing 8-bit signed Java primitive whole number data type.

Its vary is -128 to 127 (-27 to twenty seven - 1).

byte sort is that the smallest whole number information type on the market in Java.

byte variables are used once a program uses an oversized range of variables whose values fall within the very -128 to 127 or when handling binary information during a file or over the network.

There are not any computer memory unit literals. we will assign any int literal that falls within the vary of computer memory unit to a byte variable.

For example,

byte b1 = 125;
byte b2 = -11;

CHAR DATA TYPE

The char information kind may be a 16-bit unsigned Java primitive data type. It represents a Unicode character.

Note that char is associate degree unsigned information kind. Therefore, a char variable cannot have a negative worth.

The vary of the char information kind is zero to 65535, that is that the same because of the vary of the Unicode set.

Character Literals
A character literal represents a price of the char information kind. a personality literal in Java is expressed within the following formats:

A character encircled in single quotes
As a personality escape sequence
As a Unicode escape sequence
As associate degree positional representation system escape sequence
A character is expressed by enclosure it in single quotes.

The following piece of code assigns values to char variables victimisation this form:

char   c1  = 'A'; 
char   c2  = 'L'; 
char   c3  = '5'; 
char   c4  = '/';

SHORT DATA TYPE

The short knowledge kind may be a 16-bit signed Java primitive whole number knowledge kind. Its vary is -32768 to 32767 (or -215 to 215 - 1).

There is no short literal. However, we will assign an int literal that falls within the vary of short (-32768 to 32767) to a brief variable.

For example,

short s1  = 11111;      // ok 
short s2  = -11111;   // ok
The value of a computer memory unit variable is often assigned to a brief variable as a result of the vary of the computer memory unit knowledge kind falls at intervals the vary of the short knowledge kind.

INT DATA TYPE

The int information sort could be a 32-bit signed Java primitive information sort.
A variable of the int information sort takes thirty-two bits of memory.
Its valid vary is -2,147,483,648 to 2,147,483,647 (-231 to 231 - 1).
All whole numbers during this very area unit called whole number literals.
For example, 10, -200, 0, 30, 19, etc. are integer literals of int.
A whole number literal may be appointed to Associate in a Nursing int variable, say num1, like so:
int num1 = 21;

LONG DATA TYPE

The long information sort may be a 64-bit signed Java primitive information sort.
It is used once the results of calculations on whole numbers might exceed the vary of the int information sort.
Its vary is -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (-263 to 263 - 1).
All whole numbers within the vary of long square measure referred to as whole number literals of the long sort. A whole number literal of the sort long invariably ends with L or minuscule l.
The following square measure samples of employing a whole number literal of long type:
long num1 = 0L;
long num2 = 4L;
long mum3 = -3;
long num4 = 8;
long num5 = -1L;

FLOAT DATA TYPE

Floating-point numbers, additionally called real numbers, area unit used once evaluating expressions that need halfway exactitude.
A range that contains a halfway half is thought as a true number, as an example, 3.2, 0.1149, -1.19, etc.
Java supports the floating-point number format. Java uses the IEEE 754 Floating-Point customary to store real numbers.
There area unit 2 types of floating-point sorts, float and double, that represent single- and double-precision numbers, severally.
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

DOUBLE DATA TYPE

The double information kind uses sixty-four bits to store a number. double price is additionally called a double-precision number.

It will represent variety as little as four.9 x one0-324 and as massive as 1.7 x 10308 in magnitude.

It may be positive or negative.

All real numbers area unit referred to as double literals. A double literal could optionally finish with d or D, for instance, 1.27d.

The suffix d or D is nonobligatory in double literals.

Both 19.7 and 19.7d represent an equivalent double literal.

Double Literals
A double literal are often expressed within the following 2 formats:

Decimal range format
Scientific notation
Examples of double literals in decimal range format area unit as follows:

double   d1  = 8D ;
double   d2  = eight.; 
double   d3  = eight.0; 
double   d4  = eight.D; 
double   d5  = seventy eight.9867; 
double   d6  = forty five.0;
8 is associate int literal whereas eightD, 8., and 8.0 area unit double literals.

We can additionally use scientific notation to specific double literals.

double   d1  = thirty two.5E-1; 
double   d2  = zero.325E+1; 
double   d3  = zero.325E1; 
double   d4  = zero.0325E2; 
double   d5  = zero.0325e2; 
double   d6  = thirty two.5E-1D; 
double   d7  = zero.325E+1d; 
double   d8  = zero.325E1d; 
double   d9  = zero.0325E2d;

No comments:

Post a Comment