Basic and Other Assignment Operators, Multiple Assignment Process and Variable Swap at Java
What Will I Learn?
- Basic Assignment Operator
- Other Assignment Operators
- Multiple assignment process
- How to swap two variables.
Difficulty
- Basic
Tutorial Contents
In applications we write, we may want to do some mathematical and comparison operations. These are indispensable for software. By way of example, in a login process, the username and password values that the user enters must be compared with the values in the database. If both the username and password match, the login process is completed. These are provided by operators in each software language. In addition to these many basic operations are done with operators. With this tutorial we will learn the usage of these operators with examples.
Assignment operators are used to assign a variable to another variable or to assign a value to a variable. Arithmetic operators can be used at the same time when this assignment is done.
- The most basic assignment operator. Assignments are always right to left.
=
- The value on the left adds the value on the right together with its own value, and assigns the value on the left again.
+=
- The value on the right subtracts the value on the left. The result will assign the value to the left again.
-=
- The value on the right multiplies the value on the left. The result will assign the value to the left again.
*=
- The value on the right divides the value on the left. The result will assign the value to the left again.
/=
Basic Assignment Operator
Basic assignment operations are performed with the "=" operator. The variable on the right is discarded on the left.
int a = 5 ; // assigned value "5" to "a" variable
int b = a ; // assigned value "a" to "b" variable
Detailed demonstration on sample:
package com.org.operators;
public class Operators2 {
public static void main(String[] args)
{
String name=" Jack" ;
int a=5;
int c=a;
System.out.println(name) ;
System.out.println (a) ;
System.out.println (c) ;
}
}
If you write the code like this, the screen will show:
Jack
5
5
Other Assignment Operators (+=, -=, *=, /=)
These operators process the value on the right with the value on the left and the result on the left again. Which operator is in the intervening transaction.
int a;
a = a+5; // we added "5" to the variable "a". we can do it the short way like below
a + = 5; // we added "5" value to the "a" value assigned back to "a"
a * =10; // "a" value multiplied by 10 and result assigned back to "a"
We did int a; definition. So, we do not need to write an int before the variable "a" in the following definitions.
int a=20;
a*=5; // a=a*5
System.out.println (a) ; // this code will output 100
Let's make a collective sample with this operators.
package com.org.operators;
public class Operators2 {
public static void main (String[] args)
{
int a=10,b=20,c=100,d=40;
a+=5;
b-=25;
c*=2;
d /=10;
System.out.println (a) ;
System.out.println (b) ;
System.out.println (c) ;
System.out.println (d) ;
}
}
So the screen will show:
15
-5
200
4
If the intervening sign is what was done, the operation was done and the result was again assigned to the variable on the left.
The multiple assignment process
Assignment of one value to another may also be in the form of multiple assignments. So we can assign one value to another while at the same time assign it to another value.
package com.org.operators;
public class Operators {
public static void main (String[] args)
{
int number1=3;
int number2=5;
int k=number1=number2;
System.out.println (k) ;
System.out.println (number1) ;
System.out.println (number2) ;
}
}
On screen you'll see;
5
5
5
At here, while we were assigning "number2" to the "number1", we also assigned number "1" to the "k" variable. So the right most value "number2" is assigned to all variables, and the other variables havae the value of "number2".
Trading the values of 2 different variable
What do we need to do if we want to change the value of the two variables in our hands?
E.g; variable "a" = 5
variable "b" = 10. And we want the change the values of them (b=5, a=10). We do this using the assignment operator. Now we're going to learn this.
Example:
package com.org.operators;
public class Operators {
public static void main (String [] args)
{
int a=5 ;
int b=10 ;
int c ;
c=a ; // c has value 5
a=b ; // a has value 10
b=c ; // b has value 5
System.out.println (" a value " +a) ;
System.out.println (" b value " +b) ;
}
}
Screen will show:
a value 10
b value 5
As you see, "a" value was 5 but now it's 10. "b" was 10 and now it's 5. So, the variables have traded their values. We used "c" variable and it was a temporary value.
Curriculum
Posted on Utopian.io - Rewarding Open Source Contributors