Wednesday, 23 March 2011

java Data Types

Data Types

THE Java programming language is a strongly typed (not keyboard typing it’s a Data Types)) language, which means that every variable and every expression has a type (data type) that is known at compile time.

Types limit the values that a variable can hold or that an expression can produce, limit the operations supported on those values, and determine the meaning of the operations. Strong typing helps detect errors at compile time.

The types of the Java programming language are divided into two categories:

The primitive types are

the boolean type and

the numeric types.

The numeric types are

the integral type’s

byte, short, int, long, and char, and

the floating-point types

`float and double.

There is also a special null type

4.1 The Kinds of Types and Values

There are two kinds of data types in the Java programming language:

primitive types and

reference types . correspondingly,

There are, two kinds of data values that can be stored in variables, passed as arguments,returned by methods, and operated on:

primitive values and

Reference values

There is also a special null type

the type of the expression null, which has no name. Because the null type has no name, it is impossible to declare a variable of the null type or to cast to the null type. The null reference is the only possible value of an expression of null type. The null reference can always be cast to any reference type.

In practice, the programmer can ignore the null type and just pretend that null is merely a special literal that can be of any reference type

4.2 Primitive Types and Values

A primitive type is predefined by the Java programming language and named by its reserved keyword

Primitive Type:

1) Numeric Type

2) boolean type

The boolean type has exactly two values: true and false

Numeric Type:

1) Integral Type

2) Floating Point Type

Integral Type: one of byte ,short, int ,long ,char

Floating Point Type: one of float ,double

The numeric types are the

integral types and the

floating-point types.

The integral types are byte, short, int, and long, whose values are 8-bit,

16-bit, 32-bit and 64-bit signed two’s-complement integers, respectively, and

char, whose values are 16-bit unsigned integers representing UTF-16 code units

The floating-point types are float, whose values include the 32-bit IEEE 754

floating-point numbers, and double, whose values include the 64-bit IEEE 754

floating-point numbers.

The boolean type has exactly two values: true and false

4.2.1)Integral Types and Values

The values of the integral types are integers in the following ranges:

• For byte, from –128 to 127, inclusive

• For short, from –32768 to 32767, inclusive

• For int, from –2147483648 to 2147483647, inclusive

• For long, from –9223372036854775808 to 9223372036854775807

• For char, from '\u0000' to '\uffff' inclusive, that is, from 0 to 65535

4.2.2 Integer Operations

4.2.3 Floating-Point Types, Formats, and Values

The floating-point types are

float, whose values include the 32-bit IEEE 754 floating-point numbers, and

double, whose values include the 64-bit IEEE 754 floating-point numbers.

4.2.4 Floating-Point Operations

4.2.5 The boolean Type and boolean Values

The boolean type represents a logical quantity with two possible values, indicated by the literals true and false (§3.10.3).

4.3 Reference Types and Values

There are three kinds of reference types:

class types (§8),

interface types (§9), and

array types (§10).

Reference types may be parameterized (§4.5) with type arguments (§4.4).

The reference types are

class types,

interface types, and

array types. There is also a special

null type.

java operaters

3.12 Operators

Operators are special symbols that perform specific operations on one, two, or three operands, and then return a result.

The following 37 tokens are the operators, formed from ASCII characters:

Operator: one of

= > < ! ~ ?: == <= >= != && || ++ -- + - * / & |^ % <<>>>>>

+= -= *= /= &= |= ^= %= <<= >>= >>>=

Operators in java fall into 8different categories

Assignment

Arithmetic

Relational operators

Logical

Bitwise

Compound assignment

Conditional

Type

OPERATERS PRESEDENCE

The order which operator evaluated first is called as precedence

The operators in the following table are listed according to precedence order. The closer to the top of the table an operator appears, the higher its precedence. Operators with higher precedence are evaluated before operators with relatively lower precedence. Operators on the same line have equal precedence. When operators of equal precedence appear in the same expression, a rule must govern which is evaluated first. All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left.

Operator Precedence

Operators

Precedence

parenthesis

[];(),

Postfix

expr++ expr--

Unary

++expr --expr +expr -expr ~ !

Creation/cast

new,(type)expr

Multiplicative

* / %

Additive

+ -

Shift

<< >> >>>

Relational

< > <= >= instanceof

Equality

== !=

bitwise AND

&

bitwise exclusive OR

^

bitwise inclusive OR

|

logical AND

&&

logical OR

||

Ternary

? :

Assignment

= += -= *= /= %= &= ^= |= <<= >>= >>>=

The Simple Assignment Operator

Arithmetic operators are used to perform calculations using number types, these operators can only be applied between numeric data types including character.

If we apply assignment operator between boolean types, it leads to compile time error.

+ additive operator(also used for String concatenation)

- subtraction operator

* multiplication operator

/ division operator

% remainder operator

The “+” operator can also be applied between String and any other operand.

It is only the overloaded operator to add two numbers and also used as concatenate two strings and string and any data type.

java literals

3.10 Literals
A literal is the source code representation of a value of a primitive type (§4.2), the String type (§4.3.3), or the null type (§4.1):
Literal:
Integer Literal
Floating Point Literal
Boolean Literal
Character Literal
String Literal
Null Literal
3.10.1 Integer Literals LEXICAL STRUCTURE
An integer literal may be expressed in decimal (base 10), hexadecimal (base 16), or octal (base 8)
IntegerLiteral:
DecimalIntegerLiteral
HexIntegerLiteral
OctalIntegerLiteral
DecimalIntegerLiteral:
DecimalNumeral IntegerTypeSuffixopt
HexIntegerLiteral:
HexNumeral IntegerTypeSuffixopt
OctalIntegerLiteral:
OctalNumeral IntegerTypeSuffixopt
IntegerTypeSuffix: one of l L.
An integer literal is of type long if it is suffixed with an ASCII letter L or l (ell); otherwise it is of type int (§4.2.1). The suffix L is preferred, because the letter l (ell) is often hard to distinguish from the digit 1 (one).
A decimal numeral is either the single ASCII character 0, representing the integer zero, or consists of an ASCII digit from 1 to 9, optionally followed by one or more ASCII digits from 0 to 9, representing a positive integer:
A hexadecimal numeral consists of the leading ASCII characters 0x or 0X followed by one or more ASCII hexadecimal digits and can represent a positive, zero, or negative integer. Hexadecimal digits with values 10 through 15 are represented by the ASCII letters a through f or A through F, respectively; each letter used as a hexadecimal digit may be uppercase or lowercase.

An octal numeral consists of an ASCII digit 0 followed by one or more of the
ASCII digits 0 through 7 and can represent a positive, zero, or negative integer.

The largest decimal literal of type int is 2147483648 ( ). All decimal literals from 0 to 2147483647 may appear anywhere an int literal may appear, but the literal 2147483648 may appear only as the operand of the unary negation operator -.

The largest positive hexadecimal and octal literals of type int are 0x7fffffff and 017777777777, respectively, which equal 2147483647( ). The most negative hexadecimal and octal literals of type int are 0x80000000 and 020000000000, respectively, each of which represents the decimal value –2147483648 ( ).
The hexadecimal and octal literals 0xffffffff and 037777777777, respectively, represent the decimal value -1.
3.10.2 Floating-Point Literals
See §4.2.3 for a general discussion of the floating-point types and values.
A floating-point literal has the following parts: a whole-number part, a decimal or hexadecimal point (represented by an ASCII period character), a fractional part, an exponent, and a type suffix.
A floating point number may be written either as a decimal value or as a hexadecimal value.
For decimal literals, the exponent, if present, is indicated by the ASCII letter e or E followed by an optionally signed integer.
For hexadecimal literals, the exponent is always required and is indicated by the ASCII letter p or P followed by an optionally signed integer.
For decimal floating-point literals, at least one digit, in either the whole number or the fraction part, and either a decimal point, an exponent, or a float type suffix are required. All other parts are optional.
For hexadecimal floating-point literals, at least one digit is required in either the whole number or fraction part, the exponent is mandatory, and the float type suffix is optional.
A floating-point literal is of type float if it is suffixed with an ASCII letter For f; otherwise its type is double and it can optionally be suffixed with an ASCII letter D or d.
FloatingPointLiteral:
DecimalFloatingPointLiteral
HexadecimalFloatingPointLiteral
DecimalFloatingPointLiteral:
Digits . Digitsopt ExponentPartopt FloatTypeSuffixopt
. Digits ExponentPartopt FloatTypeSuffixopt
ExponentPart FloatTypeSuffixopt
Digits ExponentPartopt FloatTypeSuffix
ExponentPart:
ExponentIndicator SignedInteger
ExponentIndicator: one of
e E
SignedInteger:
Signopt Digits
Sign: one of
+ -
FloatTypeSuffix: one of
f F d D
HexadecimalFloatingPointLiteral:
HexSignific and BinaryExponent FloatTypeSuffixopt

HexSignificand:
HexNumeral
HexNumeral .
0x HexDigitsopt . HexDigits
0X HexDigitsopt . HexDigits
BinaryExponent:
BinaryExponentIndicator SignedInteger
BinaryExponentIndicator:one of
p P



3.10.3 Boolean Literals
The Boolean type has two values, represented by the literals true and false,
formed from ASCII letters.
A boolean literal is always of type boolean.

BooleanLiteral: one of
true false

3.10.4 Character Literals
3.10.5 String Literals
3.10.6 Escape Sequences for Character and String Literals

3.9 Keywords

Keywords are predefined identifiers (name of basic programming elements) which are having a special meaning in java source code.

Rule

Keywords can’t be used as user defined identifiers, means, can’t be used by the programmer for variable, method, class names because keywords are reserved words.

There are 50 keywords are available in java, among them 47 are introduced in java1.0 and 1.1

In java1.2 new keyword strictfp was added

In java1.4 new keyword assert was added

In java1.5 new keyword enum was added

The following character sequences, formed from ASCII letters, are reserved for

use as keywords and cannot be used as identifiers (§3.8):

The keywords const and goto are reserved, even though they are not currently

used. This may allow a Java compiler to produce better error messages if these C++ keywords incorrectly appear in programs.

While true and false might appear to be keywords, they are technically

Boolean literals (§3.10.3). Similarly,

while null might appear to be a keyword, it is technically the null literal

Note

Below three are Literals not key words

Boolean Literals true , false

Object Literal (default value) null

Note

Keywords should write in lower case. Violation leads to compile time error.

The below are the list of java keywords

Java files

class

interface

enum(1.5)

data types

byte , short, Int ,long

float, double ,char

boolean, void

Memory location

static

new

Control statements(11)

Conditional(5)

Loop(3)

Transfer(3)

If , else

switch, case, default

while, do

for(; ;)

for(:) (1.5)

break, continue, return

Access specifier(3)

Private, , protected, public

Access modifiers

static

final

abstract

native

transient

volatile

synchronized

strictfp

Inheritance relationships

extends

implements

Object relationships

this

super

instanceof

package

Package

Import

Exceptional handling

try, catch, finally

throw, throws

assert

Reserved keywords

goto

const

java identifiers

3.8 Identifiers

An identifier is an unlimited-length sequence of Java letters and Java digits, the

first of which must be a Java letter. An identifier cannot have the same spelling

(Unicode character sequence) as a keyword (§3.9), boolean literal (§3.10.3), or the null literal (§3.10.7).

A “Java letter” is a character for which the method Character.isJavaIden

tifierStart(int) returns true. A “Java letter-or-digit” is a character for which

the method Character.isJavaIdentifierPart(int) returns true.

The Java letters include uppercase and lowercase ASCII Latin letters A–Z

(\u0041–\u005a), and a–z (\u0061–\u007a), and, for historical reasons, the

ASCII underscore (_, or \u005f) and dollar sign ($, or \u0024). The $ character

should be used only in mechanically generated source code or, rarely, to access pre-existing names on legacy systems.

The “Java digits” include the ASCII digits 0-9 (\u0030–\u0039).

Two identifiers are the same only if they are identical, that is, have the same

Unicode character for each letter or digit.

Examples of identifiers are:

String i3 αρετη MAX_VALUE isLetterOrDigit