practice for ocp

Important: review code written in Eclipse (ocp package)

review

  • inheritance is-a etc (left and right assignments and the rules ) Standard unique test 1 Q 10
  • Valid java identifiers
  • labels and loops (and breaking out of)

Enums

  • An enum is allowed to implement interfaces
  • Enum constants must be declared first (non-blank) line e.g. DOG, CAT, FISH;

see my class EnumsPractice (eclipse)

the natural order of enums is the order in which they are defined (Treeset sorts elements in their natural sort order)

Inheritance

Review IS-A rule from both sides of the equals. Remember is-a on right is different rule to IS-A on left:

b = (B)(I) a;

 

Inheritance and static and hiding

look at inserting this at // INSERT

 

interfaces

this is fine review it

Interface can have private methods (provided method is implemented below ):

Interface MUST implement static methods ( below is not valid ):

Interface (that inherits from another can do this below ): FIRST CONCRETE CLASS MUST IMPLEMENT

 

 

API Knowledge

  • Collections (List, Map, Q, etc and subclasses )
  • Function / Streams ( Optional , andThen, collect etc … )
  • Threads (CyclicBarrier etc)
  • Date / Number formats ( Format, NumberFormat ,  DecimalFormat … )
  • Database stuff ( Statement, PreparedStatement … )

 

  • Streams (inc return type of count(), sum(), average() ) : re-read chapter in Book ?

go over how this works (order )

 

Arrays.compare method:

What will the following code print?

var a = new int[]{ 1, 2, 3, 4, 5};
var b = new int[]{ 1, 2, 3, 4, 5, 3};
var c = new int[]{ 1, 2, 3, 4, 5, 6};

int x = Arrays.compare(a, c);
int y = Arrays.compare(b, c);
System.out.println(x+” “+y);

 

List.of() , Set.of() , Map.of ()

Returns an unmodifiable list ( Elements cannot be added, removed, or replaced or sorted ! ).

 

Concurrency API

AtomicInteger important

 

To review

  • Modules
  • Streams (especially the parallel stuff )
  • Inner and Outer classes
  • Annotations
  • Security
  • Exceptions (especially try with resources rules )
  • Files (especially File and Path )
  • Generics bounds etc <? extends Shape> etc

 

Items to review (near exam , as I’ll forget them !)

Java I/O API – Serialization

if fields are added to a previously serialized object (ie it was saved to File orginally with fewer fields )

When de-serializing (reading into a newer version of the object where fields have been added ), the new fields will be blank (set to default values). Provided the original (and newer version of the class, have a serialVersionUID field )

Threads

 

runnable.run() // doesnt return any thing

callable.call()   // returns something

 

ok

int a, b, c=100;

 

not ok

var a = 100, b = 10;

 

ok

float foo = 2, bar = 3, baz = 4;    //1
float mod1 = foo % baz, mod2 = baz % foo;  //2
float val = mod1>mod2? bar : baz; //3
System.out.println(val);

 

Lambdas

Important study collectors like this:

List<Student> ls = Arrays.asList(new Student(“S1”, Student.Grade.A), new Student(“S2”, Student.Grade.A), new Student(“S3”, Student.Grade.C));

Map<Student.Grade, List<String>> grouping = ls.stream().collect(

    Collectors.groupingBy(Student::getGrade,

    Collectors.mapping(Student::getName, Collectors.toList())));

 

Arrays

Review before exam (close to , I’ll forget otherwise !!)

int[ ] a[ ] = new int [5][4] ; // good 

int a[ ][ ] = new int [5][4] ; // good 

int a[ ][ ] = new int [ ][4] ;  // bad invalid

int[ ] a[ ] = new int[4][ ] ;  // good

int[ ][ ] a = new int[5][4] ; // good

var[ ][ ] a = new int [5][4] ; // bad can’t use var here

 

Pre and Post Increment (important)

 

 

Switches

only accept the following as params:

  • byte
  • short
  • int
  • char
  • enum
  • String

null throws NullPointerException