How you swap two numbers without using third variable in java

 

How do you swap two numbers without using third variable in java?

Sample Solution

Swapping two numbers without using a third variable in Java can be done by making use of arithmetic operators. This method involves subtracting the first number from the second, saving it in the first number’s spot; then adding that difference to the original value of the second number and saving it in its place. This can be accomplished with a single line of code as follows:
firstNumber = firstNumber – secondNumber;
secondNumber = firstNumber + secondNumber;
firstnumber = secondnumber – firstnumber;
By doing this, we are effectively transferring both values between each other without requiring any additional space to store an intermediary value. It is important to note however that this technique only works for integers and not floating point numbers since they contain decimals which may lead to inaccuracies when performing calculations such as subtraction or addition (Cahill 2020).

This leads to question of what qualifies to be a combatant, and whether it is lawful to kill each other as combatants. Combatants are people who are involved directly or indirectly with the war and it is lawful to kill ‘to shelter the innocent from harm…punish evildoers (Begby et al (2006b), Page 290).However, as mentioned above civilian cannot be harmed, showing combatants as the only legitimate targets, another condition of jus in bello, as ‘we may not use the sword against those who have not harmed us (Begby et al (2006b), Page 314).’ In addition, Frowe suggested combatants must be identified as combatants, to avoid the presence of guerrilla warfare which can end up in a higher death count, for example, the Vietnam War. Moreover, he argued they must be part of the army, bear arms and apply to the rules of jus in bello. (Frowe (2011), Page 101-3). This suggests Frowe seeks a fair, just war between two participants avoiding non-combatant deaths, but wouldn’t this lead to higher death rate for combatants, as both sides have relatively equal chance to win since both use similar tactics? Nevertheless, arguably Frowe will argue that combatant can lawfully kill each other, showing this is just, which is also supported by Vittola, who states: ‘it is lawful to draw the sword and use it against malefactors (Begby et al (2006b), Page 309).’

This question has been answered.

Get Answer