Monday, 5 March 2018

Java can be mischievous

Umm all JAVA lover would have come to a point where they would have found some crazy stuffs in JAVA.

This article covers few of them. Let's start point by point.

1)Ever heard 2+2=5 ??

  Well Java can defy simple mathematics. What we have been taught at school can be easily faked by     java.
   Yes with the help of  Java you can indeed prove 2+2=5.
   But how ?
   Well the ans is simple with the help of Reflection (most of the mischievous things in Java are             basically done by Reflection).

   Let's look up the code for it .?

  import java.lang.reflect.Field;


  public class ByteEg {

public static void main(String[] args)throws Exception {
// TODO Auto-generated method stub

  Class cache = Integer.class.getDeclaredClasses()[0];
        Field c = cache.getDeclaredField("cache");
        c.setAccessible(true);
        Integer[] array = (Integer[]) c.get(cache);
        array[132] = array[133];

        System.out.printf("%d",2 + 2);

}

  }

   What happened actually?
  "array[132] = array[133]" , this is the most important line. This is where magic happens.
   First understand cache concept in java . In java for int , a cache array is created from value -128 to    127.

  This means cache[0]=-128 ,  cache[1]=-127 ... ... ......cache[128]=0

  Thus , cache[132] is actually int 4. And what we are doing here cache[132]=cache[133].
  This means the value 4 which is at 132 is replaced by value which is at  index 133 (i.e. 5).

  Thus if we wanted to prove 3+3 = 8;
  We have to change cache value of 6 and set it to 8.
   cache index for 6 is 134.
   array[134] = array[136]
 
   This will give result as 8.

   A generic code will be something like this.
   int sum=num1+num2;
   array[132+sum]=array[132+sum+shift]

  eg, 2+2=6 , here sum is 4 and shift is (6-4)=2
*******************************************************************************

2)Does X/0 in java always give Divide by zero exception??

   Well if your answer is Yes for this question , then my dear friend you got it wrong .
 
   Try something like this in java
   System.out.println(5/0.0)
   
   For the above case there will be no runtime exception . You will get a proper output.

    And the output is "Infinity" printed on console.
    
    In case of double value java has a predefined value as Infinity.

  Also you can try ,
   System.out.println(0/0.0)

  The output will be "NaN"  
*********************************************************************************

3)Comments that can actually execute :


   This can be shocking as many will be thinking how on Earth this can happen that comment can           execute.

  For those people I have an example here ?
  
  public class Testing {
    public static void main(String[] args)
     {
         // the line below this gives an output
         // \u000d System.out.println("comment executed");
      }
  }

 Output for the above example 
  comment executed.

 Actually what happen here is that, the above code got changed into 
 public class Testing {
    public static void main(String[] args)
    {
        // the line below this gives an output
        // \u000d
        System.out.println("comment executed");
    }
}

The reason for this is that the Java compiler parses the unicode character \u000d as a new line and gets transformed into above code.

*************************************************************************************************************

Hope you would have learn new things. In future I will share more on Java interesting facts.

Till then you can try to solve the following questions?

1) int a=5;
     if(a<10||a/0==0)
     {
     System.out.println("Hi");
    }
    else
    {
    System.out.println("Hello");
    }

a)Hi
b)Hello
c)Runtime Exception
d)None of these

*********************************************************************************

2)  if(a>10&&a/0==0)
     {
     System.out.println("Hi");
    }
    else
    {
    System.out.println("Hello");
    }

a)Hi
b)Hello
c)Runtime Exception
d)None of these

*****************************************************************************

3) class A
     {
    private void function()
    {
   System.out.println("Inside A");
    }
   }

  class B extends A
{
protected void function()
{
System.out.println("Inside B");
}
}

class C
{
public static void main(String args[])
{
A ob = new B();
ob.function();
}
}

Predict what will happen on running the above code and why ?

*******************************************************************************

Thanks a lot for reading.
Stay Connected.



No comments:

Post a Comment