Gadgets

Sunday, 17 February 2013

Features of Java




Features of java: -
1.       Simple: - Java is a simple programming language. Learning & practicing java is easy because of its resemblance C and C++ pointers are not available on JAVA.
Why pointers are not available on java?
A)    1. Pointers lead to confusion for a programmer
        2. Pointers easily crash a program.
                       
2.       Object Oriented: - Java is a purely object-oriented programming language. Java programs use objects & classes.
                      
3.       Distributed: - Information is distributed on various on a network. Using java, we can write programs which capture information & distribute it to clients.

4.       Robust: - Robust means strong. Java programs are strong. Java programs will not crash easily because of its execution handling and its memory management features.
5.       Secure: - Java enables the constructions of various free and tamper-free systems.

6.       Architecture Neutral or independent: - Java’s byte code isn’t machine dependent. It can be run any machine with any processor & with any operating system.

7.       Portable: - Java programs give same results on all machines. Everything is clearly defined in java specification & nothing is left to o / s. If the Program is eliding give same results are called portability.

8.       Interpreted: - Java programs are compiled to generate the byte code. This byte code can be downloaded & interpreted by the interpreter in JVM. In java we use interpreter. This interpreter is slow that’s why problem is occurred
9.       High performance: - Along with interpreter, there will be JIT (just in time) compiler which enhance the speed of execution.
10.   High performance: - Along with interpreter, there will be JIT (just in time) compiler which enhance the speed of execution.

11.   Multi-Threaded: - A thread is a process or execution. We can create different processing in java called ‘threads’. This as an essential feature to design server side programs.

12.   Dynamic: - We can develop programs in java which dynamically interact with the user on internet.
Ex: - Applets.

Polymorphism in Java with examples

Polymorphism:-
More than one function with the same is called polymorphism.
There are two types of polymorphisms.
1.      Method overloading (compile time polymorphism).
2.      Method overriding (run time polymorphism).
Method overloading:-
Two or more methods with the same name but difference in methods signatures.
Example:-
void add(int a, int b)  
void add(int a,int b,int c)
  Example program:- 
 public class MethodOverloading {
    public static void main(String args[])
    {
       Sum s=new Sum();
       s.add(10,20);
       s.add(10, 20, 30);
    } 
}
class Sum{
    void add(int a,int b)
    {
               System.out.println("sum of two is"+(a+b));
    }
     void add(int a,int b,int c) {
               System.out.println("sum of three is"+(a+b+c));
    }
}   
Method overriding:-
Two or more methods in super and sub classes with the same name and with same method signatures is called method overriding.
Example:-
public class Boy{
void add(int a,int b){
}
}
class Girl{
void add(int a,int b){
}
}
Example Program:-
public class MethodOverriding {
    public static void main(String args[])
    {
        Sum1 s1=new Sum1();
        s1.add(10, 20);
        s1.add(10, 30);
    }   
}
class Sum1{
    void add(int a,int b){
        System.out.println("sum is:"+(a+b));
    }
}
class Sum2 extends Sum1{
    void add(int a,int b){
        System.out.println("sum is:"+(a+b));
    }
}

Data Types in Java


Data types: -
               A data type represents the type of data stored in variable.
Integer data types: -
These data types represent integer numbers 

Data type                     Memory size                           min and max value
1. byte                           1  Byte                                            - 128 to +127
2. short                           2  Bytes                                 - 32768 to +32767
3. int                               4  Bytes                                 - 21474836 to +2147483647
4. long                            8 Bytes                                  -9223372036854775808 to 
                                                     +9223372036854775807                                                                                                                                                                         
Float data types: - Float means floating point numbers. Float data type represent numbers with decimal points. 

Ex: - 1.5, 3.14159,-2.2, 100.0 etc….

         Float means single precision floating point number. Double means double precision floating point number. Precision means accuracy.

Data type                 Memory size                           min and max value
1) float                       4 bytes                                    1.4e-45 to +3.4e38.
2) double                    8 bytes                                    4.1e-324 to+1.8e308.
 e means into 10 powers. (x 10power).

Character data type: -

Data type                Memories size                          min and max valve
  char                     2 bytes                                        0 to 65535 

     This data type represents a single character. In C or CPP languages using  only one byte in a char.
Ex: -    char ch = ‘x’;

    Single character literal write inside the ‘ ’(single code).
    write string in the double codes (“ ”).
A string represent group of characters.
EX: -                                                                                   
            String name=“narendar reddy”;

ASCII: - American Standard Code for Information Interchange.

Boolean data types: - These data types represent only 2 values either a true or false.
EX: -   boolean response=true;

Operators: - An operator is a symbol that performs on an operation.
EX: -
If an operator acts on a single variable, it is called unary operator.
If an operator acts on a 2 variables, it is called binary operator.
If an operator acts on a 3 variables, it is called ternary operator.

Operators in java: -

1)      Arithmetic operators: - + , - , * , / , %
These operators perform basic arithmetic calculations.
EX: - If a=5, b=5;
                                                     Operator
                        a   +   b
   
         Operands
2) Unary operators: -   - , ++ , --
            These operators act upon a single operand.
àUnary minus ( - ):- This operator negates the value of a variable.
EX:-
            int  x=5;
            System.out.println(-x);             o/p:- -5
            System.out.println(-(-x));        o/p:-  5

Increment (++): - This operator increases the value of a variable by ‘1‘.

EX: - public class UnaryOperator {
    public static void main(String args[])
    {
        int a=10;
        System.out.println(" a is"+a); // a=10
        ++a;
        System.out.println(" a is"+a); //a=11
    }
 }
Writing ++ before a variable is called pre – incrementation
Writing ++ after a variable is called post – incrementation

àWhat is the value of the following expression? Given a=7? ++a * a++?
A) A) 49                      B) 64               C) 72               D) none of these

Decrement (--): - This operator decreases the value of a variable by ‘1‘.
EX: - int x=1;
            --x;       O/P:  0
            x--;       O/P: -1
Writing -- before a variable is called pre – decrementation
Writing -- after a variable is called post – decrementation


3) Assignment operators: -  = , += , -= , *= , /= , %=
            a) This operator is used to store a value in to a variable.
EX: - int x=5;
            b) It is used to store the value of a variable in to another variable.
EX: - int  x=y;
            c) It is used to store the value of an expression in to a variable.
EX: - int x = y + z - 4;
            Expressions mean combination of a variables & values.
Note: - We can not write more than 1 variable at the left side of assignment.
EX: - x + y = 15;   // invalid

4) Relational operator: - < , > , <= , >= , == , !=
            These operators are useful to compare to quantities. They are used in construction of condition.
EX: -
            if(a>b)
           if(a<=b)

5) Logical operators: - &&(and) , | |(or) , !(not)
            These operators are useful to create compound condition. The compound condition is condition of several simple conditions.
EX: -
            If(a>b||b>c||c>d)-----;
                                              
            Any one thing is true this will be executed;
            If(x==10&&y!=10)----;
                                                   
            Both are true this will be executed.
            If(!(x==!))------;
            If(!(x==1 && y==2))----;


6) Boolean operators: - & , | , !
            Boolean operators act upon Boolean variables and they return Boolean result
boolean a,b;
a=true;
b=false;
EX: -
            a & b à false
            a | b   à true
            a & a à true                        boolean type results
            b | b   àfalse
            ! a     à false
            ! b     à true


7) Bitwise operators: -  ~ , & , | , ^ , << , >> , >>>
           These operators act up on individual bits binary digits, (0&1) of numbers.