How to Use Assignment Operators in Java

Java has a number of assignment operators, or statements which adjust the value of a variable. Besides the simple assignment and initialization operator, there are a number of shorthand arithmetic operators to reduce repetition.

Instructions

  1. Use the Assignment and Initialization Operator

    • 1

      Use the assignment operator, the symbol "=", to assign the value of an expression to a variable. This is the most commonly used operator in Java.

    • 2

      Assign to a variable as it's being declared by using variable initialization. This a slight variation of the assignment operator. The symbol is the same and the initialization operator can also assign to a final (constant) variable.

    • 3

      Type your Java variable so that the left-hand variable is the same type as the right-hand expression. Otherwise, there must be an implicit conversion from the right-hand type to the left-hand type (such as float to int). For example, you cannot assign a string to an int variable.

      ""int a = 7; // Initialization
      final int b = 10; // Final initializationa = b * 10; // Right-hand type is int (int multiplied by an int yields an int)
      float c = 21 / 2; // Right-hand type is actually int, implicitly converted to float
      float d = 21.0 / 2; // Right-hand type is float""

    Use the Increment and Decrement Operators

    • 4

      Use increment and decrement operators, the shorthand operators for the action of adding or subtracting 1 from a variable.

    • 5

      Realize that each increment and decrement operator comes in two forms, the prefix and postfix forms. The postfix form evaluates to the variable, incrementing and decrementing it afterward. The prefix version increments and decrements before evaluating, so that the expression evaluates to the variable's new value.""int a = 10;
      int b = a++; // b = 10, a = 11
      int c = ++b; // c = 11, b = 11
      c--; // c = 10""

    Use the Combination Operators

    • 6

      Combine an arithmetic operator and an assignment operator by using the arithmetic and assignment combination operators. The combination operators are the equivalent of applying the arithmetic operator with the right-hand side of the expression to the variable, and assigning the result to the variable. For example, "a += 10;" is shorthand for "a = a + 10;".

    • 7

      Use anything that can go on the right-hand side of the arithmetic expression as the type on the right-hand side of the expression. There are five of these operators: addition (+=), subtraction (-=), multiplication (*=), division (/=) and modulus (%=).""int a = 10;
      a /= 2; // a = a / 2
      a *= 32; // a = a * 32
      a += 2; // a = a + 2""

    • 8

      Employ the shift and assignment combination operators in your Java code. Similar to the arithmetic assignment combination operators, these operators perform bitwise shifts on integers instead of arithmetic.

    • 9

      Utilize one of the three variants: <<= (shift left assignment), >>= (shift right assignment) and >>>= (shift right without sign extension assignment). The operators are unique in that both the left -and right-hand side of the operator must be integers.""int a = 128;
      a >>= 4; // a = a >> 4""

    • 10

      Work with the bitwise and assignment combination operators. These are a lot like the shift operators in that they perform a bitwise operation (AND, OR or XOR) before assignment. The type of both the left- and right-hand side must be an integer. There are three variants: &= (AND assignment), |= (OR assignment) and ^= (XOR assignment).

Tips & Warnings

  • The use of the "//" symbol notes that text following it is a programming note, not usable code.

Related Searches:

Comments

You May Also Like

  • How to Use Logical Operators in Java

    The logical operators introduce the concept of boolean algebra to the Java language. They're a fundamental part of the language and can...

  • How to Use Operator Overloading in C++

    Operator overloading is handy in C++ when object-oriented programming is used. It's used in C++ classes whenever you need to define special...

  • How to Use Filefilter in Java

    The Java programming language was developed in the mid-1990s by Sun Microsystems. Java programs can run on any operating system that supports...

  • About Java Certification

    The Java certification program is offered by Sun Microsystems (the creators of the Java technology platform). Sun's Java certification is globally recognized...

  • How to Use an Operator in an Access Query

    When querying an Access database it may be necessary for you to search for a specific criterion. To satisfy a condition, the...

  • Two Types of Assignments in Insurance

    Assignment describes transferring ownership of your life insurance policy to another individual, corporation or trustee. You are still insured under the policy,...

  • How to Navigate Work Assignments

    Exactly, what am I going to be doing on a daily basis? It seems that throughout my interviewing experience I have never...

  • How to Set a Java Path

    Once the Java SDK is installed the path variables must be set so that programs written in Java will be executable from...

  • How to Capture Standard Input in Perl

    One of Perl's mottos is TIMTOWTDI (pronounced "tim toady"). It's an acronym that stands for "there is more than one way to...

  • JavaScript Tutorial for Arithmetic Operators

    Combine each of the arithmetic operators, excluding the increment and decrement operators, with the assignment operator to create a shorthand for simple...

  • How to Create Algebra 2 Symbols

    When you begin studying Algebra 2, one of your first tasks will be learning to recognize and write the various symbols that...

  • How to Use PrintStream in Java

    The Java printstream class works with files to retrieve information and output the results to the user. The unique aspect about printstream...

  • How to Create an Array in Ruby

    Add, or append, elements to the end of the array with the "" operator. Used on the end of arrays, "" is...

  • How to Check for Null Value in Visual Basic

    In computer programming languages, "null" means nothing. It is different from any other value, including a blank string that actually contains the...

  • DIY Floating Table Numbers

    Wedding guests are traditionally assigned to tables during the reception. The assignments are made to allow for serving plans, socialization and to...

Related Ads

Featured