Thursday, 17 August 2017

Data Type In Java

Java Data Type

thejavaxpert blog,, Piyush Dabhi blog, Dabhi Piyush blog, TheJavaXpert blogger owner name,  Java all info, java master, java manual, java Expert, Java Best information, Java New blog, JAVA, Piyush, Dabhi, piyush blog, Pkdabhi,  Gurukul blog, BCA, BCA best knowledge, IT, IT knowledge,  language, OOP concept, Full OOP languages name, All java program, Core java, Core Java Interview, Java Knowledge, JAVA language, Full Object oriented languages, Java nice info, Java best knowledge, Java Master, Java  java,, My java, javazone, Java game programming, java programs, java GUI, Java Blog, Top 10 java blog, javaxpert is the best blog, Java new blog list,  java top 10 blog, java top website, java top web, java, Java Datatype,Datatype in java, java DT,

The Primitive Types

Java defines eight primitive types of data: byte, short, int, long, char, float, double, and boolean.

These can be put into four groups:

Integers: This group includes byte, short, int, and long, which are for whole-valued signed numbers.

Floating-point numbers: This group includes float and double, which represent numbers with fractional precision.

Characters: This group includes char, which represents symbols in a character set, like letters and numbers.

Boolean: This group includes boolean, which is a special type for representing
true/false values.


Java defines four integer types: byte, short, int, and long. All of these are signed positive and negative values. Java does not support unsigned, positive-only integers. Many other computer languages support both signed and unsigned integers. However, Java’s designers felt that unsigned integers were unnecessary. Specifically, they felt that the concept of unsigned was used mostly to specify the behavior of the high-order bit, which defines the sign of an integer

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:


The smallest integer type is the byte. This is a signed 8-bit type that has a range from –128 to 127. Variables of type byte are especially useful when you’re working with a stream of data from a network or file. They are also useful when you’re working with raw binary data that may not be directly compatible with Java’s other built-in types. Byte variables are declared by use of the byte keyword. For example, the following declares two-byte variables called b and cbyte b, c;


short is a signed 16-bit type. It has a range from –32,768 to 32,767. It is probably the least-used Java type. Here are some examples of short variable declarations: short s; short t;


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. In addition to other uses, variables of type int are commonly employed to control loops and to index arrays. 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
evaluatedTherefore, int is often the best choice when an integer is needed.


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. For example, here is a program that computes the
number of miles that light will travel in a specified number of days.

Floating-Point Types

Floating-point numbers, also known as real numbers, are used when evaluating expressions
that require fractional precision.Floating point data types are used to represent numbers with a fractional part. Single precision floating point numbers occupy 4 bytes and Double precision floating point numbers occupy 8 bytes. There are two subtypes:


The type 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. Variables of type float are useful when you need a fractional component, but don’t require a large degree of precision. For example, a float can be useful when representing dollars and cents.
Here are some example float variable declarations: float hightemp, lowtemp;


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. All transcendental math functions, such as sin( ), cos( ), and sqrt( ), return double values. When you need to maintain accuracy over.


It stores character constants in the memory. It assumes a size of 2 bytes, but basically, it can hold only a single character because char stores Unicode character sets. It has a minimum value of ‘u0000’ (or 0) and a maximum value of ‘uffff’ (or 65,535, inclusive).


Java has a primitive 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, as in the case of a < b. boolean is also the type required by the conditional expressions that govern the control statements such as if and forHere is a program that demonstrates the boolean type:

Post a Comment

Click Here