Monday, July 5, 2010

Operators

Explain about the operators in java or Explain about the types of operators
An operator is a symbol that tells the computer to perform certain mathematical or logical manipulations . java operators can be classified into a number of related categories.

Arithmetic operators
Relational operators
Logical operators
Assignment operator
Increment and Decrement operators
Conditional operators
Bitwise operators
Special operators


Arithmetic operators

Java provides all the basic arithmetic operators. The operator +,-,*, / all work the same way as they do in other language.
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo operation

Relational operators:

Relational operators are used to compare two quantities. Java support six relational operators.
< is less than
<=is less than or equal to
> is greater than
>= is greater than or equal to
== is equal to
!= is not equal to

Logical operators:
In addition to the relational operators. Java has three logical operators which are given below.

&& logical AND
|| logical OR
! logical not
Example
a>b && x==10;
An expression of this kind which combines two or more relational expression is termed as logical expression or compound relational expression. Logical expression yields a value of true or false .

Assignment operators

Assignment operators are used to assign the value of an expression to a variable . the operator is = java has a set of short hand assignment operators which are used in the form.
V op=exp;
Where v is the variable exp is as expression and op is a java binary operator. The operator op = is known as shorthand assignment operator.

Advantage
What appear on the left hand side need not be repeated and therefore it becomes easier to read.
The statement is more concise and easier to read
Efficient code as result


Increment and decrement operators:


Java has two very useful operators not generally found in many other languages. These are increment and decrement operators ++ and --.
The operator ++ adds 1 to the operand while - - subtract 1. Both are unary operators and are used as follows
++m or m++
--m or m - -

++m and m++ means the same thing when they form statement independently. They behave differently when they are used in expressions on the right-hand side of an assignment statement.
Considered the following example
m=5
Y=++m

in this case the value of y and m would be 6 suppose if we rewrite the above statement as
m=5
Y= m++
then the value of y would be 5 and m would be 6.

Prefix operators first add 1 to the operand and then the result is assigned to the variable on the left.

Post fix operator first assigns the value to the variable on left and then increment the operand.

Example

class demo
{
public static void main(String asd[])
{
int x=10,y=20;
System.out.println(“m=”+x);
System.out.println(“n=”+y);
System.out.println(“++x=”+ ++ x);
System.out.println(“y++=”+ y++);
}
}

Conditional operators


The character pair ?: is a ternary operator avialable in java. This operator is used to construct conditional expression of the form.
exp1? exp2 : exp3

Where exp1,exp2,exp3 are the expressions. The operators ?: works as follows exp1 is evaluated first. If it is non zero then the exp2 is evaluated and becomes the value of the conditional expression. If the exp1 is false exp3 is evaluated and its value becomes the value of the conditional expression.

Example
int a=10;
int b=15;
x=(a>b) ? a :b;

x will be assigned the value of b. this can be achieved using the if-else statements as follows

if(a>b)
{
x=a;
}
else
{ x=b; }

Bit wise logical operators
Java has a distinction special operators known as bitwise operators for manipulation of data at values of bit level. These operators are used for testing the bits or shifting them to the right or left. Bit wise operators may not be applied to float or double.
Type Casting

The process of converting one data type to another is called casting. We must convert the values to be stored by
proceeding it with the type name in parentheses.

Syntax
type variabel1= (type) variable2

Example
int m=50;
byte n= (byte)m;
long count=(long) m;

Automatic Conversion:

Java does the conversion of the assigned values automatically. This is known as automatic type conversion.
Automatic type conversion is possible only if the destination type has enough precision to store the source
value.

The process of assigning a smaller type to a larger one is known as widening or promotion.Assigning a larger type to a smaller one is known as narrowing. Narrowing may results in loss of information.
Interaction with keyboard

class sample
{
public static void main(String asd[])
{
DataInputStream din =new DataInputStream(System.in);
try{
int x=Integer.valueOf(din.readLine()).intValue();
int y=Integer.valueOf(din.readLine()).intValue();
int sum=x+y;
System.out.println(“Addition=”+sum);
}catch(IOException s){}

}
}
Java standard library or API includes hundreds of classes and methods grouped into several functional packages. Most commonly used packages are
Language Support Package A collection of classes and methods required for implementing basic features of java
Utilities Package: A collection of classes to provide utility functions such as date and time functions.
Input output Package: A collection of classes required for input and output manipulation
Networking Package: A collection of classes for communicating with other computers via internet
AWT package : The abstract window toolkit package contains classes that implements platform independent graphical user interface.
Applet package: This includes a set of classes that allows us to create java applets . Compiling the program

Java Environment
Java environment includes a large number of development tools and hundreds of classes and methods. The development tools are part of the system known as java development kit JDK and the classes and methods are part of java standard library also known as Application Programming Interface(API).

Java development kit –jdk
The Java development kit comes with a collection of tools that are used for developing and running java program.They include.
Appletviewer

  • javac -compiler
  • java -interpreter
  • javap -disassembler
  • javah -header files
  • javadoc- for creating HTML document
  • jdb -java debugger

A sample java program -Beginers Approach


class sample

{

public static void main(String asd[])

{

System.out.println(“salem,Tamil nadu”);

}

}
Executing a java program involves two steps Compiling source code into bytecodes Excecuting the bytecode program using java interpreter

public static void main(String args[])


Defines a method name main. Conceptually this is similar to the main() in C language and C++ language.


public : Its an access specifies that makes the main method to be accessible to all other classes
static: which declares this method as one that belongs to the entire class and not a part of any object of the class.
void : The type modifier states that the main method does not return any values.
String args []: Its an array variable of String variable, which is used to receive the command line arguments while executing

Java Evolution1990 sun Microsytems decide to develop a special software.1991 Announced a new language named Oak1992 Demonstrate the application of this new language to control the home appliances1993 Came up with the idea of developing web based applets1994 Hot java browser was developed to run and locate the applet program1995 Oak was renamed as Java due to legal snags.

  • Features of Java
    Compiled and interpreted
  • Platform independent and portable
  • Object oriented
  • Robust and secure
  • Distributed
  • Familiar simple and small
  • Multithreaded and interactive
  • High performance
  • Dynamic and extensible
    Compiled and Interpreted Java compiler translates source code into byte code. Byte codes are not machine instruction and therefore in the second stage java interpreter generates machine code that can be directly executed by the machine that is running the java program.
    Platform Independent and portableJava programs can be easily moved form one computer system to another anywhere and any time. Changes and upgrades in operating system, processor and system resources will not force any changes in java programs. Java ensures portability in two ways byte code that can be implemented on any machine which is machine independent.
Object OrientedJava is object oriented programming language. All programming codes and data to be reside within the classes.

Robust and SecureJava is robust language because of the following two reasons It is designed as a garbage collected language relieving the programmers virtually all memory management problems. Java also incorporates the concept of exception handling which captures series errors and eliminates any risk of crashing the systems.

DistributedJava is designed as a distributed language for creating applications on networks.
Simple Small and familiarJava is a small and simple language. Many features of C and C++ that are either redundant or sources of unreliable code are not part of java.

MultithreadedMultithreaded means handling multiple tasks simultaneously. Java supports multithreaded programs.High performanceJava performance is impressive for an interpreted language mainly due to the use of intermediate byte codes

Dynamic and ExtensibleJava is dynamic language. java is capable of dynamically linking in new class libraries methods and objects. Java can also determine the type of class through a query, making it possible to dynamically link.

java differs from C language and C++ language.?Java language was designed after C and C++ languages it differs from C and C ++ language in many ways. Java does not incorporate a
number of features in C and C++.

Java and C language

JAVA does not include the C unique statement such as keywords goto sizeof type def etc.
Java does not contain the data type ,struct, union and enum
Java does not support type modifier keywords such as auto ,extern ,register,signed and unsigned values
Java does not support an explicit pointer type.
Java does not support preprocessor #
Java and C++
Java does not support operator overloading
Java does not support the template concept
Java does not support multiple inheritance of a classes and does not support the implementation of global variables
Java replace the destructor function with finalize() There is not header files in java
Java does not support scope resolution operator and inline function