Oracle Certification Associate 1Z0-808
Links
Installation
Windows
<source lang="Shell"> C:\Program Files\Java </source>
- Java Se Development Kit 8
- Java FX SDK
- Private JRE - Java Runtime Edition
- Java Mission Control Tools
Can also be installed:
- Development Tools
- Source Code
- Public JRE
Linux
<source lang="Shell"> sudo apt-get install oracle-java8-installer cd /usr/lib/jvm/java-8-oracle
- Java version
java -v
- Java Compiler
javac -version </source>
Basics
Main Method
<source lang="Java"> public static void main(String[] args]) </source>
Main Method is a static method and is not associated with any instance of a class. It is called only one time at program start.
<source lang="Java"> public class TestClass {
public static void main(String[] args) { TestClass c = new TestClass("Hello World"); System.out.println("ça marche-tu ? "+args.toString()); }
} </source>
Comments
<source lang="Java"> // Single Line Comment
/* Multi Line Comment
- /
/**
* Java Doc Comment * @param * @return */
</source>
Import Conflicts
<source lang="Java"> import java.util.*; import java.sql.*;
// ==> in code = COMPILATION error Date date; </source>
Static Imports
It is not a good idea to do static imports a lot in a projet because it makes difficult to understand where the methods / constants come from
- Static imports are for importing static members
- Classic imports are for importing classes
it can be used for methods and constants for example <source lang="Java"> import static java.lang,Math.*;
// Instead of typing Math.min(1,2); we can just type min(1,2) // Because of the static import </source>
Primitives
It is not a good practice to use "l" instead of "L" while declaring long numbers because "l" looks like a "1" number so it can be confusing
Since Java 7 we can use "_" to makes number easier to read.
- Can be anywhere except: beginning of literal, end of literal and right before or right after the decimal point.
- Error: Illegal Underscore ==> the code does not compile
KeyWord | Size | Examples | Smallest Data-Type |
---|---|---|---|
Boolean | - | true | |
Numbers without decimal points | |||
byte | 8-bits | 1 | |
short | 16-bits | 12 | comment |
int | 32-bits | 100 | Default all numbers in Java |
long | 64 bits | 12 | Requires "L" letter to avoid compilation error, even when stored in long variable |
float | 32-bits | 123.45 | Requires letter "f" following the numbers |
double | 64-bits | 123.45 |
Default all primitives or all that similar types. Can use "d" / "D" letter to specify Can convert a Float to Double |
char | 16-bits | 'a' |
Octal Numbers
Octal Numbers are numbers from 0 to 7. They can be used in java as
<source lang="Java"> // Valid int octalNumber = 07;
// Invalid - Number is too large, Compilation Error ! int octalNumber = 08; // it is not an octal number
// Valid - BUT coding standards prohibit use of octal literals, as they may be easily confused with decimal literals int octalNumber = 010 // 8 in decimal;
// Valid int declaration int octalNumber = 022; // 18 in decimal
// Valid int declaration int octalNumber = 8;
System.out.println(octalNumber); // print it's int value // Integer to octal String Integer.toOctalString();
</source>
Hexadecimal
Hexadecimals are numbers from 0 to 9 and letters from A to F <source lang="Java"> // Valid int firstHexNumber = 0xF; // 15 in decimal int secondHexNumber = 0x1E; // 30 in decimal
// Print the decimal value System.out.println(firstHexNumber); // print the Hexadecimal value: Integer.toHexString(firstHexNumber);
// p indicates hexadecimal floating point number double hexPi = 0x1.91eb851eb851fp1
int amount = 0xE; // 0x is prefix for hexadecimal </source>
Binary
<source lang="Java"> // Valid int firstBinary = 0xF; // 15 in decimal int secondBinary= 0x1E; // 30 in decimal
// Print the decimal value System.out.println(firstBinary); // print the Binary value: Integer.toBinaryString(firstBinary);
int amount = 0b101; // 0b is the prefix for Binary </source>
Scientific Notation
<source lang="Java"> // Both are valid and compile double scientificNotation = 5.000125E3 // in double 5000.125 double scientificNotation = 5.000125E03 // in double 5000.125 </source>
Chars
<source lang="Java"> char ch = 'a'; char ch1 = '1'; // it is still a char char uniChar = '\u03A9'; // upper case greek omega character char romanNumber= '\u216C'; // roman 50 number </source>
Booleans
<source lang="Java"> boolean booleanTrue = true; System.out.println(booleanTrue); // the ouput is "true"; </source>
Variables
Declaring and initializing
Allowed Characters in variables names: letter digit, dollar sign, underscore.
Declare and or initialize multiple variables on the same line is OK but not a good practice
- with coma speration: If some are declared only and other initialized, it will compile !
- with semi colomn separation, but on the same line
<source lang="Java"> int myVariable; // Declaration
// compilation error - Variable might not have been initialized System.out.println(myVariable);
myVariable = 10; // Initialization
//int i3; i4; // does not compile - they are not the same statement
// Java is case sensitive, but it can make the code really hard to understand char char; // does not compile char Char; // compile - not a good practice char cHar; // compile - not a good practice
int _myString = "It is valid identifier"; // It is valid
// Illegal examples
double 3Dpoint; // Variable name cannot start with number double my@Street; // @ is not a letter digit, dollar sign or underscore </source>
About Initialization
<source lang="Java"> public class DefaultInitialization{
static boolean myBoolean; static byte myByte; static short myShort; static int myInt; static long myLong; static float myFloat; static double myDouble; static char myChar;
public static void main(String[] args){ int localInt;
// System.out.println("localInt= " + localInt); // compilation error - Variable might not have been initialized
System.out.println("myBoolean= " + myBoolean); // false System.out.println("myByte= " + myByte); // 0 System.out.println("myShort= " + myShort); // 0 System.out.println("myInt= " + myInt); // 0 System.out.println("myLong= " + myLong); // 0 System.out.println("myFloat= " + myFloat); // 0.0 System.out.println("myDouble= " + myDouble); // 0.0 System.out.println("myChar= " + myChar); // }
}
</source>
Scope
<source lang="Java"> public class VariableScope {
static int globalInt = 5; // global variable
public static void main(String[] args) { int localInt = 10; // local variable
{ int microInt = 10; // local variable - can be only accessed in this curcly brackets block { { // int microInt = 20; // will not compile because is defined in the parent block and in the same scope } } } System.out.println(microInt); // compilation error
int microInt = 30; // can be defined, because it has a different scope }
} </source>
Class
Any error in this order will be followed by a compilation error.
- Package Declaration
- Import Statements
- Class Declaration
- Field Declarations
- Method Declarations
Null
A Null object will print "null" with a sout. But if we try to use the value to do anything, it will return <source lang="Java"> Java.lang.NullPointerException // then a stacktrace </source>
Wrapper Types
Boxing: converting primitive to wrapper int --> Integer
Unboxing: converting wrapper to primitive Integer --> int <source lang="Java"> Integer myInteger = new Integer(10); // Unnecessary boxing Integer myInteger = 20; // as primitive Integer myInteger = Integer.valueOf(10); // Unnecessary boxing Integer myInteger = Integer.parseInt("3"); // parsed from string Integer myInteger = null; int myInteger = null; // does not compile
printSum(1, 5); // autoboxing
private static void printSum(Integer first, Integer second){
System.out.println(first+second);
} </source>
Java Benefits
- Object
- Allows Functional Programming
- Encapsulation
- Support Access Modifiers to prodect data from an intended access and modifications
- Plateform Independant
- Robust
- Memory Leaks
- Manage Memory
- Garbage Collector
- Elimiates Pointers
- Simple
- Secure
Exam Questions
Traps
- Says "local" or "global" variable
- Constructor or Static void main ?
- Curly Brackets
- Binary, Hexadecimal, Decimal ?
Operators
Operator | Symbols and Examples |
---|---|
Post-unary operators | expression++, expression-- |
Pre-unary operators | ++expression, --expression |
Other unary operators | +, -, ! |
Multiplications/Divisions/Modulus | *, /, % |
Addition/Substraction | +, - |
Shift operators | >>, >>> |
Relational operators | <, >, <=, >=, instanceof |
Equal to / not equal to | ==,!= |
Logical operators | &, ^, | |
Short-circuit logical operator | &&, || |
Ternary operators | booleanExpression ? expression1 : expression2 |
Assignement operators | =, +=, -=, *=, /=, %=, &=, ^=, !=, <<=, >>=, >>>= |
Numeric Promotion and Casting
Converting a smaller type into a bigger primitive type Rules
- When 2 values have a different data types, Java will auto promote one of the values to the larger datatype
- When one of the values is integral and the other is floating-point, Java will auto promote the integral (byte, short, int, long) value to the floating-point (float, double) value's data type
- Small data types (byte, short, int, char), are the first promoted to int any time they are used with binary arithmetic operator, even when neither of the operand is int
- After all promotion has finished and the operands are the same data type (e.g all double), the result value will have the same data type as its promoted operants
<source lang="Java"> int x = 5; double y = 10.55;
double result = x + y; // VALID, no compilation error System.out.println("x + y= " + (x+y)); // 15.55 - int x is auto promoted to double
// "f" is REQUIRED float z = 3.35; // Does not compile, Default is Double in 64 bits so it will not be auto converted to float.
short x = 14; // auto-promoted to integer short y = 6; // auto-promoted to integer System.out.println(x/y); // Output of type int // short z = x * y; // does not compile - because of java auto promotion from short to integer on each site // short z = (short) x * (short) y; // does not compile - because when multiplying two shorts, java auto-promote the result to int short z = (short) x * y; // so we need to convert the result - integer - to short; </source>
Unary Operators
<source lang="Java"> ++x; // the operator is before it is applied first and the value returned is the new value x++; // the operator is placed after the operand referred as both increment operator and both decrement operator then the original value of expression is returned with operator applied after the value is returned.
int wrong = !5; // does not compile boolean wrongBoolean = -true; // does not compile boolean wrongBoolean = !0; // does not compile - 0 and false are not related in any way, and ! cannot be aplied to integers
int myInt = 5; // myInt = myInt+1; // int otherInt = myInt; int otherInt = ++myInt; // update the reference myInt and then assign its value to otherInt System.out.println("myInt= " + myInt + " otherInt= " + otherInt); // myInt= 6 otherInt= 6
int myInt = 5; // int otherInt = myInt; // myInt = myInt+1; int otherInt = myInt++; // assign myInt value to otherInt and then update its reference System.out.println("myInt= " + myInt + " otherInt= " + otherInt); // myInt= 6 otherInt= 5
int count = 0; System.out.println(count); // 0 System.out.println(++count); // 1 System.out.println(count); // 1 System.out.println(count--); // 1 System.out.println(count); // 0 </source>
Exam Questions
<source lang="Java"> // Exam Question int e = 3; int f = ++e * 5 / e-- + --e; System.out.println(e); // 2 System.out.println(f); // 7
int g = 6; int h = 2; int i = ++h + --g * 3 + 2 * g++ - h-- % --g; // 3 + 5 * 3 + 2 * 5 - 2 % 5; // 3 + 15 + 10 - 3; // 25 System.out.println(i); System.out.println(3%5); // 3 System.out.println(9%5); // 4 </source>
Assignment Operators
<source lang="Java"> int x = (int) 1.0; // output 1 short y = (short) 19812345; // output 20473 int z = (int) 9f; // output 9 // long t = (long) 32165498731654897; // Integer number too large long u = 32165498731654897L; // output 32165498731654897 </source>
Overflow and Underflow
- Overflow: when a number is too large to fit its data type
- Java automatically wraps a round to the next lowest value and starts counting from the lowest rate
- Underflow: when a number is too small to fit its data type;
- Java automatically wraps a round to the next largest value and starts counting from the largest rate
Sample with shorts
<source lang="Java"> short y = (short) 32_800; // OverFlow with number: 32_800 // The largest number in short is: 32_767 // Then java goes to the negative, the number becomes: -32_767 // Then java continue to count // Result: -32_736
short y = (short) -32_800; // Underflow with number: -32_800 // The lowest number in short is: -32_767 // Then java goes to the negative, the number becomes: 32_767 // Result: 32_736 </source>
Sample with Bytes
<source lang="Java"> byte positiveByte = 127; //positiveByte = positiveByte + 1 // Compilation error. int and byte = incompatible types. positiveByte++;
byte negativeByte = -128; negativeByte--;
System.out.println(positiveByte); // -128 System.out.println(negativeByte); // 127 </source>
Compound Assignment Operators
<source lang="Java"> int x = 2; int y = 3; // int a+= 5; // does not compile
x *= z; // x = x * z - same as above
// Without explicit cast long a = 10; int b = 4; // b = b * a; // does not compile // b = (int)(b * a); // compile, but too long to write it
b *= a;
// Weird assignment long c = 4; long d = (c=2); // shorter form of c = 2; long d = c; - It is not a good practice and confusing but it can be at the exam System.out.println(c); // 2 System.out.println(d); // 2
long e = 3; long f = 2; long h = 1; long i = e + 3 * (f = 3) - (h -= 2); //long i = 3 + 3 * 3 - -1; //long i = 3 + 9 + 1; System.out.println(i); </source>
Relational Operators
Tricks in exams <source lang="Java"> int d = 12; long e = 15L; double f = 14.5;
System.out.println((d < e + " " + (e > f)); </source>
Equality Operators
Java can compare primitive numbers but not a number and boolean, a string or object and a boolean and so on
<source lang="Java"> int a = 5; int b = 3; boolean c = a == b; // valid boolean d = a != b; // valid // boolean g = true == 0; // does not compile - cannot use it with numbers // boolean g = false != "test"; // does not compile </source>
Object Equality
Range [-128, 127] must be interned(JLS7 5.1.7)
<source lang="Java"> // 100, is an object in the cache provided by Integer, so it has the same reference Integer inRangeA = 100; Integer inRangeB = 100; System.out.println(inRangeA == inRangeB); // true because == Compare the references
/*
Different Object References / Memory Accessors
- /
// 200, is not an object in the cache provided by Integer, so it has the same reference Integer inRangeC = 200; Integer inRangeD = 200; System.out.println(inRangeC == inRangeD); // false because == compare the references
int inRangeE = 300; // creates the reference in memory int inRangeF = 300; // 300 exist now in memory, so java just use the reference System.out.println(inRangeE == inRangeF); // false because == compare the references
int inRangeG = 400; int inRangeH = inRangeG; System.out.println(inRangeG == inRangeH); // false because == compare the references
// Unique Number for objects in memory - also Memory Location / Adress System.out.println(System.identityHashCode(inRangeA)); System.out.println(System.identityHashCode(inRangeB)); System.out.println(System.identityHashCode(inRangeC)); System.out.println(System.identityHashCode(inRangeD)); System.out.println(System.identityHashCode(inRangeE)); System.out.println(System.identityHashCode(inRangeF)); System.out.println(System.identityHashCode(inRangeG)); System.out.println(System.identityHashCode(inRangeH)); </source>
Character Arithmetic
<source lang="Java"> char myCharA = 'A'; char myCharNum = '1'; System.out.println(Character.isLetter(myCharA)); // true System.out.println(Character.isDigit(myCharNum)); // true
//Exam Questions char letter = 65; // A in ASCII table int myInt = letter + 3; // 68 char myNewLetter = (char) myInt; // 68 is D in ASCII table
char newChar = (char) (letter + 1); // B System.out.println(newChar == 'B'); // true System.out.println(newChar++ < 'C'); // true - 67 < 68 System.out.println(newChar); // C - 68 </source>
Questions
<source lang="Java"> /*
Valid answers: A-B-D int can be auto-promoted to long or double
- /
byte x = 5; byte y = 10; __?__ z = x + y // default result of x + y is int, but it can be auto-promoted
A. int B. long C. boolean D. double E. short // int cannot be promoted to short, because int is larger F. byte </source>
Exam Tips
- Accolades mal placées
- E scientifique remplacé par un F
- Declare and or initialize multiple variables on the same line is OK but not a good practice
- with coma speration: If some are declared only and other initialized, it will compile !
- with semi colomn separation, but on the same line