Oracle Certification Associate 1Z0-808: Difference between revisions
Line 422: | Line 422: | ||
double result = x + y; // VALID, no compilation error | double result = x + y; // VALID, no compilation error | ||
System.out.println("x + y= " + (x+y)); // 15.55 - int x is auto promoted to double | 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. | |||
</source> | </source> | ||
Revision as of 17:09, 8 June 2020
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. </source>
Questions: - Double - FLoat - Double
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