Please enable JavaScript.
Coggle requires JavaScript to display documents.
OCA Java Programmer I (operators (logical operator (short-circuit…
OCA Java Programmer I
operators
-
logical operator
short-circuit operators &&, ||
-
-
-
-
-
-
compound operator =, += (will cast automatically int = float)
-
-
-
-
-
numeric promotions
-
after promotions, the results will have the same data type as promotion
byte, short and char are promoted to int in operations (short = short + short) will not compile
-
if two values has different data type, one will be promoted to the largest
-
-
statements
advanced control flow
-
label is also used for break in inner loop, as break OUTER_LOOP;
-
-
for
-
-
multiple statements: for(x=1,y=1;x<10 && y<10;y++,x--)
for(;;) infinite - will compile, but for() won't
pay attention with variables declared in initialization, it is for scope only
-
while
pay attention if the variable of the expression is changed, to avoid infinite loop
switch
will just compile if the value is literal, enum or final (except if the final variable is passed as parameter)
-
-
-
-
if else
-
Pay attention if(x), if(x=1) does not compile
-
Java Core API
String Builder
Mutable sb.append("bc") -> "abc" the same object, no garbage action is required
-
-
-
-
String
Methods
-
.replace('a', 'A') --> "AbcAbc"
-
-
-
-
-
.substring(3) or (3,4) ->> (3,3) = empty, (3,2) exception
return the string starting from the requested index and stops right before of requested end index
.indexOf("char or string", "from position"). Return -1 if not found
-
-
-
-
-
-
-
-
Array
new int[3] > new int[]{1,2,3}; or int[] x = {1,2,3};
pay attention: int ids[], types; (Will generate array od ids and a integer of type
equals does not look at the elements of the array, just reference
the array does not allocate space for the String objects, it allocates space for a reference to where the objects are really stored
String[] to Object[] no cast necessary
Object[] to String[] cast is necessary
Object[0] = new StringBuilder(); Accepted but will throw an array tye exception
-
method
-
Arrays.binarySearch(array,"value") -> return index (although, it works properly just for sorted array, it can not be predicted for unsorted
List<String> x = Arrays.asList(array) \ convert an array to a list (pay attention, the size of the list cannot bet changed
-
ArrayList
-
-
-
ArrayList x = new ArrayList(); stores everything, except primitives
methods
add("value") or add(position, value). It will not replace just insert
-
set(index, value) can throw an outbound exception
isEmpty(), size(), clear()
-
-
toArray(), return Object[], if you wish new array = arraylist.toArray(new String[0])
-
wrapper
-
-
-
pay attention on remove method, if you type remove(1) it will remove the index, you should type remove(new Integer(1))
Date and ime
-
-
-
-
-
declaration
LocalDate date = LocalDate.of(2015,Month.JANUARY,20) or 1 replacing JANUARY
LocalTime time = LocalTime.of(6,15, 30) // 6 hours and 15 minutes and 30 seconds
LocalDateTime dateTime = LocalDateTime.of(date,time) or (2015,1,20,6,15,30)
-
-
methods
date = date.plusWeeks(1)
plusDays(2)
plusMonths(4)
plusYears(5)
minus...
pay attention there is no plusMinute on LocalDate
-
-
parse("11:00");
parse("11:00",formatter);
Period class
Period period = Period,ofMonths(3) // 3 months
-
Period perido = Period.of(1,0,7) // Every year seven days
-
Date and Time Formatting
-
-
-
DateTimeFormatter short = DatetimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
short.format(date) // 1/20/18
-
-
Data
-
Declaration
-
class variable
-
-
Default
-
-
-
byte, short, int, long: 0
-
-
-
Identifier
-
Can have letter, _, $ and digit
-
double A, double B; (Does not compile)
String x, int y; (Does not compile)
-
Data Type
Reference
All references are the same size, the objects is the entity in the memory
-
-
Primitive
-
-
-
-
-
int: 32-bit int (2,147,483,647)
-
-
-
Import
-
-
Static import (child packages, fields and methods)
-
-
-
-
-
-
-