Wednesday, 23 March 2011

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

Thursday, 24 February 2011

what is object? and what is class? in java

OOPS in java

Opps is required

to design and develop,

to support and represent

a real world object in programming languages

Definition of Opps

Opps is an approach that provides a way of modularizing of program,

by creating memory area for both data and methods,

that can be used as template for creating copies of such modules (objects) on demand.

Opps is a technique to create programs based on real world objects, unlike procedural programming here in the opp programming model, programs are organized around objects and data rather than actions and logic.

Building Blocks Of Opps: Classes, Objects

Definition of Class

A class is a software construct that defines the instance variables and methods of an object.

A class itself is not an object.

A class is a template that defines how an object will look and behave when the object is created or instantiated from the specification declared by the class.

You obtain concrete objects by instantiating a previously defined class.

You can instantiate many objects from one class definition.

just as you can construct many houses that area all the same from a single architect’s drawing.

A class is a blue print of an object.

A class is a speciation that defines what sort of objects should be created.

A class is a logical construct of an object.

Hence a class defines the structure, state and behavior.

Definition of an object:

In the programming implementation of an object, it’s (An object’s) state is defined by its instance variables. Instance variables are private to the object. Unless explicitly made public or made available to other “friendly” classes, an object’s instance variables are inaccessible from outside the object.

An object’s behavior is defined by its methods. Methods manipulate the instance variables to create new state; an object’s methods can also create new objects.

The small picture to the left is a commonly used graphical representation of an object. The diagram illustrates the conceptual structure of a software object—it’s kind of like a cell, with an outer membrane that’s its interface to the world, and an inner nucleus that’s protected by the outer membrane. An object’s instance variables (data) are packaged, or encapsulated, within the object. The instance variables are surrounded by the object’s methods. With certain well-defined exceptions, the object’s methods are the only means by which other objects can access or alter its instance variables. In Java, classes can declare their instance variables to be public, in which cases the instance variables are globally accessible to other objects. Declarations of accessibility are covered in later in Access Specifiers.

Object is a physical construct of a class.

It can also define as Instance of a class.

Technically object can be defined as it is an encapsulated form of all instance variables & instance methods of a particular class.

Monday, 21 February 2011

What is encapsulation


Encapsulation:

Encapsulation is the process of protecting /hiding data, by declaring variables as private and providing public setter and getter method.

(or)

Wrapping or binding variables of a class with methods which operated on those variables is called as Encapsulation.

1) If it is needed how can we give access to outside member?

By using public methods. Declare variables as private and access them into public methods with in the class, call this public methods form outside the class.

2) What is the difference between in accessing variables directly and via public setter and getter methods?

If you allow user to access variables via method, we can validate by authentication logic if required.

This the main reason behind encapsulation, suggesting us to protect variables with private access specifier and provide the accessibility via methods.