In the movie The Avengers how does HULK differentiate between the enemy and the allies? Previously when he turns into the beast aboard the avengers space craft, Hulk fights with Thor which means that he does not know who is enemy and who is ally. But during the last fight Hulk smashes the aliens only. How? And what makes him differentiate the enemy from the allies?
If you think of Banner and Hulk as 2 different entities, then the Hulk's main objective is to protect Banner from coming to any harm. This explains why he stops Banner from killing himself (this is mentioned in the Avengers).
It also makes sense that when Banner's heart rate gets too high the Hulk appears, it's his defense response kicking in. The same when he gets angry, it's the Hulk trying to attack whatever Banner is angry at.
We can assume in the time Banner was in hiding that he has learned to control the Hulk better, so that he can suppress him coming out or call him when he needs him. Apparently he does this by staying on the brink of anger all of the time.
However in a situation like when Hawkeye blows up the Helicarrier, the Hulk comes out because he perceives Banner to be in need of him (whether that's through loss of control, anger, heart rate is not explained).
So when the Hulk is released unbidden, he will generally smash anything and everything nearby, in that case Black Widow, because he is trying to protect Banner. This quickly gets sidetracked though as soon as Thor, and later the fighter jet, get involved.
If you ever notice in the films, the Hulk always attacks whoever or whatever is attacking him. He will stop attacking or chasing something if something else is being more aggressive or threatening (like the fighter jet).
In the battle of New York, however, Banner calls out the Hulk, so has more control over him. This is how it helps to distinguish who is a friend or foe.
So if a fighter jet just showed up, he probably wouldn't pay it much notice. But if it started shooting at him he probably would attack it just as soon as any of the aliens.