Showing posts with label java. Show all posts

instanceof operator



instanceof operator is also known as type comparison operator. It compares an object with any specific type(may be class, interface etc.). It returns either true or false.
It is used to test whether given object is an instance of specified class or not.

Example :

class Test{
public static void main(String args[]){
Test t=new Test();
System.out.println(t instanceof Test ) ;

}

Output :
true

Ternary operator



In the last post we learned some basic operators. Apart from these operators, there are some other operators we need to know about.

In this post post, we will learn Ternary operator.


Ternary operator(?:) : 
                                   This operator is also called conditional operator. It   contains three operands and is used to evaluate boolean expressions. Goal of this operator is to decide which value should be assigned to variable.
Syntax :
 Variable v=(expression) ? value if true : value if false ;                                                                               
Example : 

  public class TernaryOperatorTest {
  public static void main(String args[]) {
      int x, y;
      x = 5;
      y = (x == 1) ? 10: 15;
      System.out.println( "Value of y : " +  y );
      y = (x == 5) ? 10: 15;
      System.out.println( "Value of y : " + y );
   }
}


Output : 


Value of y : 15
Value of y : 10

Previous topic                                                                  Next topic
Java operators                                                                 instanceof operator







Operators in Java




Operator is a symbol to perform a certain operation on given operands.

Type of operators

1 . Arithmetic operators

2 . Relational operators

3 . Bitwise operators

4 . Logical operators

5 . Assignment operators


Now we will go through each kind of operator one by one


Arithmetic operator : 

These operators are used in mathematical expressions in the same way that they are used in algebra.
Assume integer variables X and Y having values 10 and 5 respectively.

   + (Addition) : Adds values on either side of operator.  
                                X+Y=10+5 gives 15 as result.

    - (Subtraction) : Subtracts second value from the first one.
                              X-Y =10-5 gives 5 as result.

   * (Multiplication) :  Multiplies values given on either side.
                                X*Y=10*5 gives 50 as result.

   / (Division) : Divides first value by second one.
                       X/Y=10/5 gives 2 as result.  

  % (modulus) : Gives remainder if, a value is divided by another.
                          X%Y=10%5 gives 0 as result.

  ++ (Increment operator) : Increases a value by 1.
                                        X++ is equivalent to x+1 i.e. 10+1 gives 11 as 
                                         result.

  -- (Decrement operator) : Decreases a value by 1.
                                         X-- is equivalent to X-1 i.e. 10-1 gives 9 as result. 

Relational Operators : 

 Relational operators are used to compare operands.

Assume two integers X and Y having 10 and 5 respectively.

There are following type of relational operator.

==(equal to) :  Checks if the values of two operands are equal or not, if yes  
                          then condition becomes true.
                         (X==Y) is false.

!= (not equal to) : Checks if the values of two operands are equal or not, if 
                              values are not equal then condition becomes true.
                             (X!=Y) is true.

> (greater than) : Checks if the value of left operand is greater than the                                      value of right operand, if yes then condition becomes                                    true.
                           (X>Y) is true.

< (less than) :  Checks if the value of left operand is smaller than the value 
                        of  right operand, if yes then condition becomes true.
                        (X<Y) is false.

>= (greater than or equal to) :  Checks if the value of left operand is 
                                                  greater or equal to value of the left operand.
                                                  (X>=Y) is true.

<= (less than or equal to) :   Checks if the value of left operand is smaller
                                                   or equal to value of the left operand.
                                                  (X<=Y) is false.  

Bitwise operator :

Java Bitwise operators can be applied to integer types, long, int, short, char and byte.
Bit wise operator works on bits.
Assume int X=10 and Y=5 and their bit conversions are as follows
X=00001010
Y=00000101

 & (Bitwise AND) : Binary AND operator copies a bit to the result if it exists  
                              in both operands.
                             X&Y=00000000.

| ( bitwise OR) :    Binary OR operator copies a bit if it exists in either 
                               operand.
                               X | Y = 00001111.

^ (Bitwise XOR) : Binary XOR operator copies bit if it is set in one operand                                    but not both.
                              X ^ Y = 00001111.

~ (Bitwise complement) : It is used to determine the negation of given 
                                       value.It is 2's complement of the given value
                                       ~X = 11110110

<< (Left shift operator) : The left operand value is moved left by the  
                                       number of bits specified by right operand.
                                       X<<2 = 10<<2 = 00101000

>> ( Right shift operator) : The left operand value is moved right by 
                                             the number of bits specified by right operand.  
                                            X>>2 = 10>>2 = 10000010


Logical Operator :

This operator returns result in boolean(true or false).
Assume there are two boolean variables X and Y having values true and false respectively.

&& (Logical AND) : If both operands are true then (X&&Y) becomes true 
                             otherwise becomes false.

|| (Logical OR) : If any one of the operands has value true (X||Y) becomes 
                          true.

! (Logical NOT) : Reverse the logical state of the operand (!X) is false (!Y) is 
                          true 

Assignment Operator : 
           
This operator is used to assign a value to a variable.
e.g. int x=5; 


Previous topic                                                                     Next topic
Unicode system                                                                  Ternary operator

            
                                     



     

JDK, JRE and JVM



                                                                                                               
JVM- jvm(java virtual machine) is a specification which does the following
         works.

1 . Loads code.
2 . Verifies code.
3 . Executes code.
4 . Provides Runtime environment in which byte code gets executed.



JVM internal architecture






JRE - jre acronym for java runtime environment  is an implementation of
         JVM. It contains set of libraries and other files that JVM uses at runtime.
         We can say JVM is a part of JRE.

     



JDK - JDK is the java development kit which contains development tools and
         JRE.




JVM, JRE and JDK all are platform dependent.

                                                                                             Next topic
                                                                                             Unicode system

                                                                                                                           

Detect IP Address of device when network change detected

When network change is detected whether it is wifi to mobile network or mobile network to wifi, the IP address allocated to device is changed.

1 . If network changes from wifi to mobile data then you can find the new IP 
     address allocated to device by writing the following code


try {
    for (Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
 enumeration.hasMoreElements();) 
    {
        NetworkInterface nInterface = enumeration.nextElement();
        for (Enumeration<InetAddress> enumIpAddresses = nInterface.getInetAddresses(); 
             enumIpAddresses.hasMoreElements();) 
        {
            InetAddress inetAddress = enumIpAddresses.nextElement();
            if (!inetAddress.isLoopbackAddress()) 
            {
                return inetAddress.getHostAddress().toString();
            }
        }
    }
} catch (Exception ex) {
      ex.printStackTrace();
}
 
2 . If network changes from mobile data to wifi then you can find the new IP
     address allocated to device by writing  the following code.

WifiManager wifiManager=(WifiManager)context.getSystemService(Context.WIFI_SERVICE);
String ipAddress=Formatter.formatIpAddress(wifiManager.getConnectionInfo().getIpAddress());