Skip to content

Latest commit

 

History

History
193 lines (144 loc) · 5.65 KB

3.md

File metadata and controls

193 lines (144 loc) · 5.65 KB

Chapter 3 USING OPERATORS AND DECISION'S CONSTRUCTORS

3.1 USE JAVA'S OPERATORS

  • attribution operator, =, store a value to a variable;
  • byte < short < int < long < float < double;
  • char has the same size as byte but can't be negative;

Reference and attribution

  • attributions are ALWAYS by value copy, on primitive types we copy the value, on object reference we copy the reference (we do not duplicate the object);

Arithmetic operators

  • Used to do operations on numeric primitive variables: +, -, *, / and %;
  • The type of the result variable from the operation is the most embracing type or, at least, int;

Division by zero

  • Divide an integer by zero will result on a ArithmeticException;
  • Divide an float/double by zero will result on a positive/negative infinity;
  • NaN (Not a Number) is generated by the radiciation of some numbers and by some operations with infinity numbers;

Comparation operators

  • Equality operators: == and !=;
  • Order operators: >, <, >= and <=;
  • A comparation only returns true or false;
  • Numeric comparations ignores the value type;
  • References and boolean can only be compared with == or !=;
  • Can't compare incompatible types;
  • Operations with floating number are not always accurated;

Logic Operators

  • And &, Or |, Exclusive Or ^, Negation !;
  • Short circuit operators: && and ||;

Increment and Decrement operators

  • Pos Add 1 i++;
  • Pos Subtract 1 i--;
  • Pre Add 1 ++i;
  • Pre Subtract 1 --i;
  • Attributions and operations:
    • i = i + 2 is the same as i += 2;
    • i = i - 2 is the same as i -= 2;
    • i = i * 2 is the same as i *= 2;
    • i = i / 2 is the same as i /= 2;
    • i = i % 2 is the same as i %= 2;
    • On this cases the compilator allows you to do oparations with incompatible types. Ex: byte b1 = 3; b1 += 4;;

Ternary operator

  • boolean condiction ? if_true : if_false;
  • Should always return a value that can be used to an attribution;

Reference operators

  • .
  • Used to navegate on an object;

Strings concatenation

  • Can use the operator + to concatenate String;
  • String is the only class that accepts an operator besides .;
  • String concatenation with + is a syntax sugar, once the code is compiled it uses a StringBuilder;

Precedence

  • Order of execution:
    1. pre increment/decrement
    2. multiplications, divisions and mod;
    3. subtraction addition;
    4. shifts;
    5. pos increment/decrement;

Casting of primitive types

  • double d = 0; float f = (float) d;
  • Casting with primitive types can result on precision lost;

Exercises

  1. b
  2. b
  3. d
  4. d
  5. d
  6. Linha 2, 3 e 4
  7. b
  8. d
  9. e
  10. Não compila.
  11. c
  12. c
  13. a
  14. f
  15. c
  16. f
  17. b
  18. c
  19. a

3.2 USE PARENTHESES TO OVERRIDE OPERATORS PRECEDENCES

  • int a = 15 * 4 + 1; // 15 * 4 = 60, 60 + 1 = 61;
  • int b = 15 * (4 + 1); // 4 + 1 = 5, 15 * 5 = 75;

Exercises

  1. a
  2. b

3.3 Equality between Strings and other Objects

  • When using == on objects, it compares the object reference;
  • Two newly declared Strings are never equal (two distinct objects);
  • Strings are immutable;

String Pool

  • Java only uses string pool for literal types;
  • Strings created with the new operator does not go to pool;
  • when concatenating literal strings, the result string goes to the pool;
  • the result from concatenating a literal string and a new string object does not go into the pool;
  • the result from calling string methods are new Strings;
  • the result from calling string methods are not aqquired from pool;
  • when calling a method from a string resulting in the string itself, no new string is created;
  • "word" and "Word" are different, two Strings are added to the pool;

Equals

  • each Class has a .equals() method that can be overwritten;
  • new Integer(10) == new Integer(10) returns false;
  • new Integer(10).equals(new Integer(10)) returns true;
  • equals method must be public and receive an Object parameter;
  • equals method, by default, compares the object content;

Exercises

  1. b
  2. d
  3. c
  4. e
  5. a

3.4 Utilizing If/Else Statement

if/else

  • It's all about flux control.
  • = for assignment and == for comparison, don't let yourself be fooled.
  • assignments inside an if statement must return true or false, otherwise: compilation error
  • braces are optional given the conditional is only one instruction long
  • else is optional when nothing is to be executed in case the statement returns false
  • we must have a statement to execute after an ´if´
  • there is no elseif, for this, if's are placed inside elses, ex: else if

Unreachable Code and Missing return

  • methods that return something, may not return something with a condition that returns something when true but lacks the else statement

Exercises

  1. c
  2. a
  3. a
  4. a
  5. b

3.5 Using the Switch

  • Switch allowed arguments are int, wrapper smaller than Integer, String or Enum.
  • The switch variable must match the argument type
  • When using an int argumento and a double value in the case: compile error
  • The compiler casts the value of the argument if the case type is compatible
  • A case can be a literal, a final variable with a litteral value or expressions envolving both
  • Simple variables cannot be used as cases: compile error
  • null is not a valid case
  • To be considered a valid final variable, it must be declared as its initialized: final int num = 5;
  • The default case may appear in any order.
  • Breaks must be used between cases, otherwise there will be a cascade execution from the first matching case

Exercises

  1. d
  2. a
  3. b
  4. a
  5. a
  6. a

pg172