• Home
  • DMP
    • DMP Calendar
    • DMP Notes >
      • Media Literacy >
        • Media Ownership
        • Digtial Manipulation
        • Intro to Media Quiz
      • Audio >
        • This American Life
        • Audio Exercises
        • Foleys
      • Storytelling
      • Capturing Video
      • Deconstructions >
        • Magazine Deconstruction
    • DMP Resources
  • 3D Animation
    • 3D Animation Calendar
    • 3D Modeling >
      • Blender Basics >
        • Donut Tutorial
        • Modifiers
        • Sculpting
      • Modeling Tutorials
    • 3D Printing
    • Adv 3d Modeling >
      • Surfaces >
        • Materials
        • Nodes
        • Textures
    • 3D Modeling & Animation Resources
  • Web Design
    • Web Design Calendar
    • Notes >
      • Organization and Planning >
        • Basic Computer Terms
        • Critiquing Web Pages
        • Parts of URL
        • Webmasters & Accessibility
        • File Structure
        • Naming Conventions
        • Web Page Layouts
        • Good Websites
        • Organization & Planning Quiz
      • Setting up a Page >
        • HTML Basics >
          • Code Basics >
            • HR and special char
            • Lists >
              • Basic Lists
              • List Formats
            • HR & Formatting Exercise
            • Headings, PG & Lists Exercise
            • Invitation Assignment
      • Advanced HTML >
        • Tables >
          • Basic Tables
          • Table Exericses 1
          • Advanced Tables
          • Table Exercise 2
        • Color >
          • Color Design
          • Color Exercise
        • Links >
          • Absolute links
          • Relative Links
          • Email Links
          • Named Anchors
          • Links Exercise 2
        • Images >
          • Adding Images
          • Image Mapping
          • Colors & Tables Exercise
        • CSS >
          • About CSS
          • Styles >
            • Inline CSS
            • Embedded Styles
            • External Styles
          • Style Attributes >
            • CSS Text Styles
            • CSS Backgrounds
            • Link Styles
          • Selectors
          • Classes & IDs
          • Div vs Span
          • CSS Hierarchy
          • The Box Model
          • CSS Practice Project 1
          • Transparency
          • CSS Positioning
          • CSS Transform
          • CSS Practice Project 3
      • JavaScript >
        • JavaScript Examples
    • Independent Project
    • Web Design Resources
  • Com Art
    • Com Art Calendar
    • What is Graphic Design? >
      • Black Square Problem
    • History of Design >
      • History of Design Review
      • History of Design Quiz
    • Elements & Principles >
      • Line
      • Shape
      • Form
      • Texture
      • Color
      • Value
      • Space
      • Principles of Design
      • Elements/Principles Review
      • Elements & Principles Quiz
    • Design Process >
      • 99 Thumbnails
      • Road Sign Problem
      • Life And Death Problem
      • Face Mask Design
    • Computer Graphics >
      • Photoshop Filters Project
      • Photoshop Fill Project
      • Photoshop Self Portrait
      • Contrasting Images Project
      • Phrase Blending Project
      • Photoshop Tips & Tricks
    • Com Art Resources
  • General Info
    • Sign Out Form
    • Adding Calendar Info
    • About Google Drive
    • Creating/Sharing a Drive Folder
    • How to join Remind
    • Make Everything Okay
HERMANET THE G.O.A.T.

Expressions: Types and Operators

EXPRESSIONS: TYPES AND OPERATORS

Every expression in a Java program has a type. In Java, there are two kinds of types: primitive types and objects. Examples of primitive types include:

int           double                Boolean

Objects are instances of classes, which are basically encapsulated data and directions. Strings are a special kind of object with their own set of tools.

 Arithmetic Operators:

Addition                         +
Subtraction                    -
Multiplication                 *
Division                         /
Modulus (remainder)    %
 
All of the arithmetic operators can be applied to expressions of type int or double. The addition operator can also be used to perform string concatenation (joining). If at least one of its operands is a String, then the result is the concatenation of that String with the String representation of the other operand. For example:

Expression                                                          Value of the Expression
 
“book” + “worm”                                             “bookworm”
“version” + 3                                                  “version3”
.5 + “baked”                                                   “.5baked”
 
Integer division (when both the numerator and the denominator are integers) results in truncation (cutting off), not rounding. For example, 2/3 is zero, not one; -2/3 is also zero, not minus one. If you want to round a double variable x to the nearest integer (instead of truncating it), you can use
 
(int)(x+.5)
 
when x is positive, and
 
(int)(x-.5)
 
when x is negative.
 
Casting can be used to convert an int to a double (or vice versa). For example:
 
Expression                                                          Value of the Expression
 
(int)3.6                                                                 3
(double)3                                                            3.0
(double)2/3                                                        .667
(int)2.0/3                                                             0
 
Assignment Operators
 
Assignment means to give a value to something. The assignment operators are:
 
Plain assignment                              =
Add-then-assign                              +=
Subtract-then-assign                      -=
Multiply-then-assign                      *=
Divide-then-assign                          /=
Modulus-then-assign                     %=
 
The types of the left- and right-hand sides of an assignment must be compatible, and the left-hand side must be a memory location. Variables, for example, are memory locations.
 
The last five assignment operators listed above are called compound assignments; a compound assignment takes the form:
 
a operator= b
 
And it is equivalent to
 
a = a operator b
 
For example:
 
Compound Assignment                                                                Equivalent Noncompound Assignment
 
a += 2                                                                                               a = a + 2
a -=b                                                                                                 a = a – b
a *= 5.5                                                                                             a = a *5.5

Assignments are expressions, not statements; the value of an assignment expression is the value of its right-hand side. This means that assignments can be “chained”. For example, the following is perfectly legal:
 
int j, k, n;
j = k = n = 0;  // all three variables are set to zero
 
Increment/decrement operators
 
increment ++
decrement –
 
The increment operator adds one to its operand; the decrement operator subtracts one from its operand. For example:
 
Using increment/decrement operator                   Equivalent assignment expression
 
a++                                                                                        a += 1      or     a = a + 1
a--                                                                                          a -= 1       or     a = a - 1
 
Equality, Relational, and Logical operators


Equal to                                                ==
Not equal to                                          !=
Less than                                              <
Less than or equal to                           <=
Greater than                                          >
Greater than or equal to                       >=
Logical NOT                                          !
Logical AND                                        &&
Logical OR                                            ||
 
The equality and relational operators must be applied to expressions with compatible types. The logical operators must be applied to expressions with type boolean. An expression involving equality, relational, or logical operators evaluates to either true or false (so the type of the whole expression is Boolean). Expressions involving the logical AND and OR operators are guaranteed to be evaluated from left to right, and the evaluation stops as soon as the final value is known. This is called short-circuit evaluation. For example, when the expression
 
(5 > 0) || (3 < 2)
 
is evaluated, the expression (3<2) is not evaluated. The subexpression
 
(5 > 0)
 
Is evaluated first, and it evaluates to true. Since logical OR applied to true and any other expression always evaluates to true, there is no need to evaluate the other expression. Similarly, the logical AND applied to false and any other expression always evaluates to false, the expression (3 < 2) is not evaluated in this case:
 
(5 < 0) && (3<2)


Powered by Create your own unique website with customizable templates.