Data types in java



We know we need variables to store data. A variable represents a memory location which holds data. When we want to use a  variable in a program, we should first declare it.
Ex;  char a;  int x;  float f;
Let us take int x; here x is a variable, which can store int(integer) type data. This means int is representing the nature of data to be stored into x. int is also called a data type. For example x can store an integer number like 100 as:
X=100;
Here x is a variable and = represents that the value 100 is stored into x. this value 100 stored into x is also called literal. There are various data types and literals defined in java.
Java defines eight simple (or elemental) types of data: byte, short, int, long, char, float, double, and boolean. These can be put in 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.
Integers: 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, including C/C++, 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 defined the sign of an int when expressed as a number. Java manages the meaning of the high-order bit differently, by adding a special “unsigned right shift” operator. Thus, the need for an unsigned integer type was eliminated. The width and ranges of these integer types vary widely, as shown in this table:
Name                        Width                                         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                            8                                          –128 to 127
byte : The smallest integer type is 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 c:
byte b, c;
short:  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, since it is defined as having its high byte first (called big-endian format). This type is mostly applicable to 16-bit computers. Here are some examples of short variable declarations:
short s; short t;
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. In addition to other uses, variables of type int are commonly employed to control loops and to index arrays. Any time you have an integer expression involving bytes, shorts, ints, and literal numbers, the entire expression is promoted to int before the calculation is done. The int type is the most versatile and efficient type, and it should be used most of the time when you want to create a number for counting or indexing arrays or doing integer math. Remember, type determines behavior, not size.
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. For example, here is a program that computes the number of miles that light will travel in a specified number of days.
// Compute distance light travels using long variables.
 class Light {
public static void main(String args[]) {
 int lightspeed; long days; long seconds; long distance;
// approximate speed of light in miles per second lightspeed = 186000;
days = 1000; // specify number of days here
seconds = days * 24 * 60 * 60; // convert to seconds
distance = lightspeed * seconds; // compute distance
System.out.print("In " + days); System.out.print(" days light will travel about "); System.out.println(distance + " miles.");
}
}
This program generates the following output:
In 1000 days light will travel about 16070400000000 miles.
Let us try to understand this through an example given here: byte num = 10;
In the preceding statement, we are declaring byte  data type for the variable num and the value 10, which is stored into num.  byte  represents any value between -128 to +12 7.
long  x = 100L;
Here, 100 is stored into x, which is declared as long type. Notice the L at the end of the statement. If this L is not there, then JVM allots only 2 bytes of memory to x as against the usual 8 bytes memory that should be allotted to a long type. The reason for this is that 2 bytes are sufficient to store the value 100. But If we attach 1 or L at the end of the value as shown in the preceding example, then JVM will consider it as a Long value and will allot 8 bytes to It.
Float data 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.There are two kinds of floating-point types, float and double, which represent single- and double-precision numbers, respectively. 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
float:  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, float can be useful when representing dollars and cents. Here are some example float variable declarations:
float  hightemp,  lowtemp;
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. All transcendental math functions, such as sin( ), cos( ), and sqrt( ), return double values. When you need to maintain accuracy over many iterative calculations, or are manipulating large-valued numbers, double is the best choice. Here is a short program that uses double variables to compute the area of a circle:
// Compute the area of a circle.
 class Area {
 public static void main(String args[]) {
 double pi, r, a;
r = 10.8; // radius of circle pi = 3.1416; // pi, approximately a = pi * r * r; // compute area
System.out.println("Area of circle is " + a);
}
}
Q: what is the difference between float and double?
A: float can represent up to 7 digits accurately after decimal point, where as double can represent up to 15 digits accurately after decimal point.

Character data type:
In Java, the data type used to store characters is 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. Instead, 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. It is a unification of dozens of character sets, such as Latin, Greek, Arabic, Cyrillic, Hebrew, Katakana, Hangul, and many more. For this purpose, it requires 16 bits. Thus, in Java char is a 16-bit type. 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 as always, and 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. Of course, the use of Unicode is somewhat inefficient for languages such as English, German, Spanish, or French, whose characters can easily be contained within 8 bits. But such is the price that must be paid for global portability.
Ex:
// Demonstrate char data type.
class Demo {
 public static void main(String args[]) {
 char ch1, ch2;
ch1 = 88;  // code for X ch2 = 'Y';
System.out.print("ch1 and ch2: "); System.out.println(ch1 + " " + ch2);
}
}
This program displays the following output:
ch1 and ch2: X Y
Notice that ch1 is assigned the value 88, which is the ASCII (and Unicode) value that corresponds to the letter X. As mentioned, the ASCII character set occupies the first 127 values in the Unicode character set. For this reason, all the “old tricks” that you have used with characters in the past will work in Java, too. Even though chars are not integers, in many cases you can operate on them as if they were integers. This allows you to add two characters together, or to increment the value of a character variable. For example, consider the following program:
// char variables behave like integers.
class CharDemo2 {
 public static void main(String args[]) {
 char ch1;
ch1 = 'X'; System.out.println("ch1 contains " + ch1);
ch1++; // increment ch1 System.out.println("ch1 is now " + ch1);
}
}
The output generated by this program is shown here:
ch1 contains X ch1 is now Y
In the program, ch1 is first given the value X. Next, ch1 is incremented. This results in ch1 containing Y, the next character in the ASCII (and Unicode) sequence.
Q: what is Unicode system?
A: Unicode system is an encoding standard that provides a unique number for every character, no matter what the platform, program or language is. Unicode uses 2 bytes to represent a single character.

Boolean data types
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.  boolean is also the type required by the conditional expressions that govern the control statements such as if and for. JVM uses 1 bit to represent a Boolean value.
Ex:
// Demonstrate boolean values.
class BoolTest {
 public static void main(String args[]) {
boolean b;
b = false;
 System.out.println("b is " + b);
 b = true;
 System.out.println("b is " + b);
// a boolean value can control the if statement
if(b) System.out.println("This is executed.");
b = false;
 if(b) System.out.println("This is not executed.");
// outcome of a relational operator is a boolean value
System.out.println("10 > 9 is " + (10 > 9));
}
}
The output generated by this program is shown here:
b is false b is true This is executed. 10 > 9 is true
There are three interesting things to notice about this program. First, as you can see, when a boolean value is output by println( ), “true” or “false” is displayed. Second, the value of a boolean variable is sufficient, by itself, to control the if statement. There is no need to write an if statement like this:
if(b == true) ...
Third, the outcome of a relational operator, such as <, is a boolean value. This is why the expression 10 > 9 displays the value “true.” Further, the extra set of parentheses around 10 > 9 is necessary because the + operator has a higher precedence than the >.

Reference Data Types:

 Reference variables are created using defined constructors of the classes. They are used to access objects. These variables are declared to be of a specific type that cannot be changed.
For example:Employee, Puppy etc.
 Class objects, and various type of array variables come under reference data type.    Default value of any reference variable is null.    A reference variable can be used to refer to any object of the declared type or any compatible type.
Example : Animal animal = new Animal("giraffe");

Literals
A literal represents a value that is stored into a variable directly in the program.
Ex: char gender=’M’; short st = 1000; int i=100; Boolean output = true;
We have different types of literals. They are
1.       Integer literals
2.       Float literals
3.       Character literals
4.       String literals
5.       Boolean literals

Integer Literals Integers are probably the most commonly used type in the typical program. Any whole number value is an integer literal. Examples are 1, 2, 3, and 42. These are all decimal values, meaning they are describing a base 10 number. There are two other bases which can be used in integer literals, octal (base eight) and hexadecimal (base 16). Octal values are denoted in Java by a leading zero. Normal decimal numbers cannot have a leading zero. Thus, the seemingly valid value 09 will produce an error from the compiler, since 9 is outside of octal’s 0 to 7 range. A more common base for numbers used by programmers is hexadecimal, which matches cleanly with modulo 8 word sizes, such as 8, 16, 32, and 64 bits. You signify a hexadecimal constant with a leading zero-x, (0x or 0X). The range of a hexadecimal digit is 0 to 15, so A through F (or a through f ) are substituted for 10 through 15. Integer literals create an int value, which in Java is a 32-bit integer value. Since Java is strongly typed, you might be wondering how it is possible to assign an integer literal to one of Java’s other integer types, such as byte or long, without causing a type mismatch error. Fortunately, such situations are easily handled. When a literal value is assigned to a byte or short variable, no error is generated if the literal value is within the range of the target type. Also, an integer literal can always be assigned to a long variable. However, to specify a long literal, you will need to explicitly tell the compiler that the literal value is of type long. You do this by appending an upper- or lowercase L to the literal.
 For example, 0x7ffffffffffffffL or 9223372036854775807L is the largest long.
Floating-Point Literals: Floating-point numbers represent decimal values with a fractional component. They can be expressed in either standard or scientific notation. Standard notation consists of a whole number component followed by a decimal point followed by a fractional component. For example, 2.0, 3.14159, and 0.6667 represent valid standard-notation floating-point numbers. Scientific notation uses a standard-notation, floating-point number plus a suffix that specifies a power of 10 by which the number is to be multiplied. The exponent is indicated by an E or e followed by a decimal number, which can be positive or negative. Examples include 6.022E23, 314159E–05, and 2e+100. Floating-point literals in Java default to double precision. To specify a float literal, you must append an F or f to the constant. You can also explicitly specify a double literal by appending a D or d. Doing so is, of course, redundant. The default double type consumes 64 bits of storage, while the less-accurate float type requires only 32 bits.
Boolean Literals: Boolean literals are simple. There are only two logical values that a boolean value can have, true and false. The values of true and false do not convert into any numerical representation. The true literal in Java does not equal 1, nor does the false literal equal 0. In Java, they can only be assigned to variables declared as boolean, or used in expressions with Boolean operators.
Character Literals: Characters in Java are indices into the Unicode character set. They are 16-bit values that can be converted into integers and manipulated with the integer operators, such as the addition and subtraction operators. A literal character is represented inside a pair of single quotes. All of the visible ASCII characters can be directly entered inside the quotes, such as ‘a’, ‘z’, and ‘@’. For characters that are impossible to enter directly, there are several escape sequences, which allow you to enter the character you need, such as ‘\’’ for the single-quote character itself, and ‘\n’ for the newline character. There is also a mechanism for directly entering the value of a character in octal or hexadecimal. For octal notation use the backslash followed by the three-digit number. For example, ‘\141’ is the letter ‘a’. For hexadecimal, you enter a backslash-u (\u), then exactly four hexadecimal digits. For example, ‘\u0061’ is the ISO-Latin-1 ‘a’ because the top byte is zero. ‘\ua432’ is a Japanese Katakana character. Table 3-1 shows the character escape sequences.

Escape Sequence                                         Description
\ddd                                                                 Octal character (ddd)
\uxxxx                                                              Hexadecimal UNICODE character (xxxx)
\’                                                                       Single quote
\”                                                                      Double quote
\\                                                                      Backslash
\r                                                                      Carriage return
\n                                                                     New line (also known as line feed)
\f                                                                      Form feed
\t                                                                       Tab
\b                                                                      Backspace

String Literals: String literals in Java are specified like they are in most other languages—by enclosing a sequence of characters between a pair of double quotes. Examples of string literals are
“Hello World” “two\nlines” “\”This is in quotes\””
The escape sequences and octal/hexadecimal notations that were defined for character literals work the same way inside of string literals. One important thing to note about Java strings is that they must begin and end on the same line. There is no line-continuation escape sequence as there is in other languages.

No comments:

Post a Comment

Genuine websites to earn money.

If you are interested in PTC sites then this article is for you. I have personally tried many of the sites and found that the best thing ...