Swap two variable without temporary variable


You can swap numeric primitive types (int, double, float, ...) without a temporary variable:
a=a+b;
b=a-b;
 a=a-b;
For strings (by using a temporary variable for the length):

int len1 = s1.length();
s1=s1+s2;
s2=s1.substring(0,len1);
s1=s1.substring(len1);
Reactions

Post a Comment

0 Comments