Let’s talk about “this”

Ya I mean “this” a keyword in object oriented programming..,
Look at the Example 1 in the below gist.

In the temp class you have two reference variables named x,y and two local variables x,y inside special method.

Now you have another class say temp1 as shown above when run produces the following output:

Now the output will be
when you are write “this” in a method you are actually referring to the object of the current class created during the run time. Hence when you write this.x it refers to the reference variable but not the local variable but when you just mention x in special method you are referring to local variables.
When you are using this(args) you are actually referring the constructor of this class. Look at the example 3 for the usage of “this” to call a constructor.

Bit wise operators

Most of us rarely use bit wise operators but if we know where and when to use them they are really going to be awesome.., Let’s check out an application of bit wise operators right now:)

Bit Vectors(series of bits) can be used to represent sets(gotta remember from school days mathematics).

Lets say we have to perform union operation of two sets It becomes increasingly easy if you use bit wise operators


lets say the set we have is {0,3,5,6}

then represent it as follows i.e 7654321 and corresponding status 01101001 where 1 represents the presence of the number in the set and viceversa

Let’s take another set {0,2,4,6}

which is represented as 7654321 and corresponding status 01010101

Now union of two sets is bit wise union( | ) of two statuses and intersection is bit wise intersection( & ) of two statuses and Now Symmetric difference is similarly with bit wise operator ^(exclusive or) and for compliment operator we can use ~ operator on statuses

A smarter Application of bitwise operators

Suppose If you would like to represent if-else conditional in a smarter way Check
out here..,
Way one is this a=x?y:z
The other interesting way is:
a= ((x<<31)>>31)&y+(((!x)<<31)>>31)&z
Here we assumed that the boolean x is either 1 or zero

Another Application is here:

Suppose you are asked to swap two numbers without using a third variable here bit wise operators comet to our rescue i.e An important concept here is if you xor a n bit binary number with any arbitary number twice you get that back the original number; Say you have to swap x and y:
int x=53,y=43;
x=x^y; // “^” operator is associative

Buffer OverFlow Attacks!!!!

Let’s see what a buffer attack is and the bad programming practices that can result in this attack.
Static memory allocation is done on stack where as dynamic memory allocation is done on a heap which happens during run time.
Now Let’s come to Buffer Overflow Attacks.., Suppose if you use functions like gets(var) and many other which doesn’t check for the overflow Lets check the following example which can tell you about the intensity of the attacks of Buffer Overflow

Input 1
$ ./a.out
Enter the password :
Correct Password
Root privileges given to the user
Input 2
$ ./a.out
Enter the password :
Wrong Password
Root privileges given to the user

By this time if you have gone through the above code carefully you should have got the intensity of Buffer Overflow attacks actually here in the second case though the statistical memory allocated on stack was only for 15 characters but you have overridden the next few characters in the heap with the string and these few locations happen to be the memory allocations of pass whose values are overridden hence though the password was incorrect pass has got a value which is non zero and the condition is passed and if this code actually grants users permissions then All Root Previleges must have gone to the malicious user

Problem of nextInt and nextLine methods in Scanner class

Ever wondered why does nextInt() and nextLine() methods in java doesn’t work properly when used simultaneously…Here you go..,

Let us say we have an object named input which is an instance of class Scanner.Now if you have a code snippet like the one below

var2 = input.nextLine();

and if your input is say
Now here var1 has the number 1 but the var2 doesn’t have the string hello.this is because nextInt() method only takes input an Integer but it doesn’t take the newline character “\n” so next thing in the input stream is new line character which will be saved into var2 when you now use a nextLine() method

You can insert a small snippet of code to remove that new line character from buffer i.e
var2 = input.nextLine();

Use two different objects of the class scanner call the method of first object when you use nextInt() method and use other second object for the nextLine() method..,