Manage Data

Data Types
Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.
Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or characters in these variables.
There are two data types available in Java:
1. Primitive Data Types
2. Reference/Object Data Types
Primitive Data Types
Java supports eight basic data types known as primitive types. In addition, it supports classes and arrays as composite data types, or reference types. The primitive types are: a boolean type, a character type, four integer types, and two floating-point types. The four integer types and the two floating-point types differ in the number of bits that represent them, and therefore in the range of numbers they can represent.
Type Contains Default Size Range
boolean true or false false 1 bit NA
char Unicode character \u0000 16 bits \u0000 to \uFFFF
byte Signed integer 0 8 bits -128 to 127
short Signed integer 0 16 bits -32768 to 32767
int Signed integer 0 32 bits -2147483648 to 2147483647
long Signed integer 0 64 bits

-9223372036854775808 to 9223372036854775807

float

IEEE 754 floating point

0.0 32 bits

±1.4E-45 to ±3.4028235E+38

double

IEEE 754 floating point

0.0 64 bits

±4.9E-324 to ±1.7976931348623157E+308


. User-Defined Data Types
A java programmer creates software objects by taking help of either user-definedapplication specific classes or pre-defined library classes. Therefore, java program involvesclass definitions and creating objects out of them for computational interactions. A class can beregarded as auser defined data type template to produce as many desired objects as required.
A class can make use of both instance and function variables of simple data types likeint, float, double, boolean, char, etc. Data values to variables of simple data types are always passed as call-by-value method.Class type objects, being a composite (a chunk of memoryspace having slots to accept different types of data values) data type, data values are passed todifferent instance variable-slots and function argument slots as call-by-reference.
At times, a programmer may need to represent a simple data type as an object of its javadefined wrapper class. Such wrapping of a simple data type helps in utilizing the methodsavailable with the wrapper classes. So java provides wrapper class for each simple datatype. Such type wrapper classes remain defined in the java.lang package, which automaticallygets included with every java program you write. Subsequent examples will make this wrapper conception more clear.
Java programs can make use of both user defined classes as encapsulated composite datatypes as well as wrapper classes for primitive data types.
Java programs can make use of both user defined classes as encapsulated composite datatypes as well as wrapper classes for primitive data types.
Unicode Character Set
Java programs are written using the Unicode character set. Unlike the 7-bit ASCII encoding, which is useful only for English, and the 8-bit ISO Latin-1 encoding, which is useful only for major Western European languages, the 16-bit Unicode encoding can represent virtually every written language in common use on the planet. Very few text editors support Unicode, however, and in practice, most Java programs are written in plain ASCII. 16-bit Unicode characters are typically written to files using an encoding known as UTF-8, which converts the 16-bit characters into a stream of bytes. The format is designed so that plain ASCII and Latin-1 text are valid UTF-8 byte streams. Thus, you can simply write plain ASCII programs, and they will work as valid Unicode.
If you want to embed a Unicode character within a Java program that is written in plain ASCII, use the special Unicode escape sequence \uxxxx. That is, a backslash and a lowercase u, followed by four hexadecimal characters. For example, \u0020 is the space character, and \u3c00 is the character p. You can use Unicode characters anywhere in a Java program, including comments and variable names.
Arrays of Primitive Data Types in Java
int[] a = new int[5];
int[] b = a;
a[0] = 1;
b[0] = 2;
a[1] = 1;
b[1] = 3;
System.out.println(a[0]);
System.out.println(b[0]);
System.out.println(a[1]);
System.out.println(b[1]);
Comments
Java supports three types of comments. The first type is a single-line comment, which begins with the characters // and continues until the end of the current line. For example:
int i = 0; // initialize the loop variable
The second kind of comment is a multiline comment. It begins with the characters /* and continues, over any number of lines, until the characters */. Any text between the /* and the */ is ignored by the Java compiler. Although this style of comment is typically used for multiline comments, it can also be used for single-line comments. For example:
/*
* Step 4: Print static methods, both public and protected,
* but don't list deprecated ones.
*/
The third type of comment is a special case of the second. If a comment begins with /**, it is regarded as a special doc comment. Like regular multiline comments, doc comments end with */ and cannot be nested. A program named javadoc extracts these comments and processes them to create online documentation for your class. A doc comment can contain HTML tags and can use additional syntax understood by javadoc. For example:
/**
* Display a list of classes, many to a line.
*
* @param classes The classes to display
* @return true on success,
* false on failure.
* @author David Flanagan
*/
Identifiers and Reserved Words
An identifier is any symbolic name that refers to something in a Java program. Class, method, parameter, and variable names are all identifiers. An identifier must begin with a letter, an underscore ( _ ), or a Unicode currency symbol (e.g., $, £, ¥). This initial letter can be followed by any number of letters, digits, underscores, or currency symbols. Remember that Java uses the Unicode character set, which contains quite a few letters and digits other than those in the ASCII character set. The following are legal identifiers:
theCurrentTime
the_current_time
Identifiers can include numbers, but cannot begin with a number. In addition, they cannot contain any punctuation characters other than underscores and currency characters. By convention, dollar signs and other currency characters are reserved for identifiers automatically generated by a compiler or some kind of code preprocessor. It is best to avoid these characters in your own identifiers.
Java Reserved Words
abstract do if package synchronized
boolean double implements private this
break else import protected throw
byte extends instanceof public throws
case false int return transient
catch final interface short true
char finally long static try
class float native strictfp void
const for new super volatile
continue goto null switch while
default

Literals
A literal is a source code representation of a fixed value. They are represented directly in the code without any computation. Literals can be assigned to any primitive type variable. For example:
byte a = 68;
char a = 'A'
byte, int, long, and short can be expressed in decimal(base 10), hexadecimal(base 16) or octal(base 8) number systems as well.
Prefix 0 is used to indicate octal and prefix 0x indicates hexadecimal when using these number systems for literals. For example:
int decimal = 100;
int octal = 0144;
int hexa = 0x64;
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\""
String and char types of literals can contain any Unicode characters. For example:
char a = '\u0001';
String a = "\u0001";
Java language supports few special escape sequences for String and char literals as well. They are:
Notation Character represented
\n Newline (0x0a)
\r Carriage return (0x0d)
\f Formfeed (0x0c)
\b Backspace (0x08)
\s Space (0x20)
\t tab
\" Double quote
\' Single quote
\\ backslash
\ddd Octal character (ddd)
\uxxxx Hexadecimal UNICODE character (xxxx)

Type Conversions
Java allows conversions between integer values and floating-point values. In addition, because every character corresponds to a number in the Unicode encoding, char types can be converted to and from the integer and floating-point types. In fact, boolean is the only primitive type that cannot be converted to or from another primitive type in Java.
There are two basic types of conversions. A widening conversion occurs when a value of one type is converted to a wider type--one that is represented with more bits and therefore has a wider range of legal values. A narrowing conversion occurs when a value is converted to a type that is represented with fewer bits. Java performs widening conversions automatically when, for example, you assign an int literal to a double variable or a char literal to an int variable.
Narrowing conversions are another matter, however, and are not always safe. It is reasonable to convert the integer value 13 to a byte, for example, but it is not reasonable to convert 13000 to a byte, since byte can only hold numbers between -128 and 127. Because you can lose data in a narrowing conversion, the Java compiler complains when you attempt any narrowing conversion, even if the value being converted would in fact fit in the narrower range of the specified type:
If you need to perform a narrowing conversion and are confident you can do so without losing data or precision, you can force Java to perform the conversion using a language construct known as a cast. Perform a cast by placing the name of the desired type in parentheses before the value to be converted. For example:
int i = 13;
byte b = (byte) i; // Force the int to be converted to a byte
i = (int) 13.456; // Force this double literal to the int 13
Convert Convert To:
From: boolean byte short char int long float double
boolean - N N N N N N N
byte N - Y C Y Y Y Y
short N C - C Y Y Y Y
char N C C - Y Y Y Y
int N C C C - Y Y* Y
long N C C C C - Y* Y*
float N C C C C C - Y
double N C C C C C C -

* N: - The letter N in the table means that the conversion cannot be performed.
* Y: - The letter Y means that the conversion is a widening conversion and is therefore performed automatically and implicitly by Java.
* C: - The letter C means that the conversion is a narrowing conversion and requires an explicit cast.
* Y*: - The notation Y* means that the conversion is an automatic widening conversion, but that some of the least significant digits of the value may be lost by the conversion. This can happen when converting an int or long to a float or double.
Data Type Casting (Type Conversion)
Java supports two types of castings – primitive data type casting and reference type casting. Reference type casting is nothing but assigning one Java object to another object. It comes with very strict rules and is explained clearly in Object Casting. Now let us go for data type casting.
Java data type casting comes with 3 flavors.
1. Implicit casting
2. Explicit casting
3. Boolean casting.
1. Implicit casting (widening conversion): - A data type of lower size (occupying less memory) is assigned to a data type of higher size. This is done implicitly by the JVM. The lower size is widened to higher size. This is also named as automatic type conversion.
byte -> short -> int -> long -> float -> double ->
Examples:
int x = 10; // occupies 4 bytes
double y = x; // occupies 8 bytes
System.out.println(y); // prints 10.0
2. Explicit casting (narrowing conversion): - A data type of higher size (occupying more memory) cannot be assigned to a data type of lower size. This is not done implicitly by the JVM and requires explicit casting; a casting operation to be performed by the programmer. The higher size is narrowed to lower size. double -> float -> long -> int -> short -> byte ->
Examples:
double x = 10.5; // 8 bytes
int y = x; // 4 bytes ; raises compilation error
In the above code, 8 bytes double value is narrowed to 4 bytes int value. It raises error. Let us explicitly type cast it.
double x = 10.5;
int y = (int) x;
The double x is explicitly converted to int y. The thumb rule is, on both sides, the same data type should exist.
3. Boolean casting: - A boolean value cannot be assigned to any other data type. Except boolean, all the remaining 7 data types can be assigned to one another either implicitly or explicitly; but boolean cannot. We say, boolean is incompatible for conversion. Maximum we can assign a boolean value to another boolean.
Following raises error.
boolean x = true;
int y = x; // error
boolean x = true;
int y = (int) x; // error
byte –> short –> int –> long –> float –> double
In the above statement, left to right can be assigned implicitly and right to left requires explicit casting. That is, byte can be assigned to short implicitly but short to byte requires explicit casting.
Following char operations are possible.
public class Demo
{
public static void main(String args[])
{
char ch1 = 'A';
double d1 = ch1;
System.out.println(d1); // prints 65.0
System.out.println(ch1 * ch1); // prints 4225 , 65 * 65
double d2 = 66.0;
char ch2 = (char) d2;
System.out.println(ch2); // prints B
}
}


Free Web Hosting