- attribution operator,
=
, store a value to a variable; byte < short < int < long < float < double
;char
has the same size asbyte
but can't be negative;
- 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);
- 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
;
- 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;
- Equality operators:
==
and!=
; - Order operators:
>
,<
,>=
and<=
; - A comparation only returns
true
orfalse
; - 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;
- And
&
, Or|
, Exclusive Or^
, Negation!
; - Short circuit operators:
&&
and||
;
- 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 asi += 2
;i = i - 2
is the same asi -= 2
;i = i * 2
is the same asi *= 2
;i = i / 2
is the same asi /= 2
;i = i % 2
is the same asi %= 2
;- On this cases the compilator allows you to do oparations with incompatible types. Ex:
byte b1 = 3; b1 += 4;
;
boolean condiction ? if_true : if_false
;- Should always return a value that can be used to an attribution;
.
- Used to navegate on an object;
- Can use the operator
+
to concatenateString
; String
is the only class that accepts an operator besides.
;- String concatenation with
+
is a syntax sugar, once the code is compiled it uses aStringBuilder
;
- Order of execution:
- pre increment/decrement
- multiplications, divisions and mod;
- subtraction addition;
- shifts;
- pos increment/decrement;
double d = 0; float f = (float) d
;- Casting with primitive types can result on precision lost;
- b
- b
- d
- d
- d
- Linha 2, 3 e 4
- b
- d
- e
- Não compila.
- c
- c
- a
- f
- c
- f
- b
- c
- a
int a = 15 * 4 + 1; // 15 * 4 = 60, 60 + 1 = 61
;int b = 15 * (4 + 1); // 4 + 1 = 5, 15 * 5 = 75
;
- a
- b
- When using
==
on objects, it compares the object reference; - Two newly declared Strings are never equal (two distinct objects);
- Strings are immutable;
- 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;
- 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;
- b
- d
- c
- e
- a
- 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
- methods that return something, may not return something with a condition that returns something when true but lacks the else statement
Exercises
- c
- a
- a
- a
- b
- Switch allowed arguments are
int
, wrapper smaller thanInteger
,String
orEnum
. - 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
- d
- a
- b
- a
- a
- a
pg172