Call   0208 3131682   Mon-Fri 9am-6pm
Tag

Java Archives - Freelance Full Stack Developer in London, UK

How to speed up Web Development in Java

By | Uncategorized | No Comments

This is an article I’m currently working on (its just rough notes at the moment).

Java features ( Java 8 and beyond )

eg var for variables in methods

Spring Boot

Can be up and running as fast as a Laravel app ( can even have a prod grade server, so just start server and you’re good)

Spring Dev Tools – Spring fast restrarts

JRebel ( rem costs £400 per year )

Spring STS Eclipse

Clean Code

Make it obvious was code does from variable and function names. Reading less code, saves alot of time.

Testing – use BDD tests

BDD gives the most bang for the buck, when it comes to testing your code works, fine grained TDD tests will slow development.

Lombok

For handy annotations such as @Data to remove boiler plate but will have the relevant stuff compiled into the class (such as getters, setters, toSting ). Automatic getter/setters on wrapper type classes save reading through code.

IDE – use its full power

Some good tips here e.g. save actions ( eg format source on save )

Use the your owe predefined templates for common actions

Disclaimer: All content on this site, is use at your own risk (Always backup before changing anything in your software/database/servers etc). Techs change, go out of date etc...
I/we accept no liability if anything you use on this site adversely affects you.

Installing ChromeDriver for WebDriver – For the Techies (Java)

By | Uncategorized | No Comments

If your using Webdriver to automate your web testing you’ll already know what a great tool it is.

This is a quick note on installing ChromeDriver for WebDriver (to test using the Chrome Browser).

1. get the latest version of chrome driver from this url http://chromedriver.storage.googleapis.com/index.html

2. unzip this and place it whenever you want

3. next in your java code tell it where to find the unzipped file (replace ‘path goes here’ with your location). And thats it job done 🙂

System.setProperty(“webdriver.chrome.driver”,     “/path goes here/chromedriver”); 

driver = new ChromeDriver(); 

 

I highly recommend installing ChromeDriver for WebDriver , especially if your only using one type of browser for your testing. Hopefully you found this useful.

Disclaimer: All content on this site, is use at your own risk (Always backup before changing anything in your software/database/servers etc). Techs change, go out of date etc...
I/we accept no liability if anything you use on this site adversely affects you.

Java Generics (2008)

By | Uncategorized | No Comments

Notes on SCJP I did in 2008

Generics

 

Straight forward Type Safe Collections

 

List<Animal> myList = new ArrayList<Animal>();

 

the generic type in the declaration must match on both side of = , in this case <String>

(unless a wildcard is used to the left of the equals OR the ref has no gen type eg List myList).

 

can only add <Animal> or subtypes to myList

eg

 

myList.add(new Animal()); // GOOD

myList.add(new Dog()); // GOOD

 

myList.add(new Integer()); //ERROR

 

Passing / Returning from method

void myMeth(List<Animal> animals){ …

 

can accept

List<Animal>, Vector<Animal> // GOOD

 

cannot accept

List<Dog>, ArrayLis<Cat> // ERROR

 

 

Mixing Old and New Collections

List<Integer> list = new ArrayList<Integer>();

 

oldClass.alterList(list)

class OldClass{

    void alterList(List List){

       list.add(” a string ! “); // works

   }

 

Its not until someone calls a Integer method on the String obj in the collection that it will blow up ! 

However the compiler will issue a warning.

 

 

Type Safe Collections with wildcards

When you dont need to add to a collection

void lookAtThis(List<? extends Animal>){ …

 

can take:

 

List<Animal>

ArrayList<Dog>

Vector<Lion>

etc

 

can also take class type that implements an interface. [eg Animal implements ManEater]

 

List<? extends Animal> li = new ArrayList<Dog>(); // GOOD

this means assign anything to li that is Animal or subclass BUT will not be able to add to it

 

List<?> list is equivalent to : List<? extends Object>

 

Wildcards when you need to add to a collection 

void addTo(List<? super Dog> list){ …

 

this can take:

 

List<Dog>

List<Animal>

List<Object>

 

adding to:

 

list.add(new Dog()); // GOOD

 

list.add(new Object()); // ERROR

list.add(new Animal()); // ERROR

 

 

List<? super Dog> li = new ArrayList<Animal>(); // GOOD, looks abit weird usually more specific on right eg List l = new ArrayList();

li.add(new Animal()); // ERROR weird but true

li.add(new Alsatian()); // GOOD

 

Summary:

When see a ref that has notation like List<?> this can take list of any type but cannot add to it.

 

Generic Type ReferenceTaskis task valid?Notes
 
List<Animal> listlist.add(new Dog());YOK add

NOT OK poly pass list type

pass this to it List <Dog>N
list.add(new Animal());Y
 
List<? extends Animal> listlist.add(anything);NOK pass poly generic subtypes

CAN NOT add to

pass ArrayList<Dog>Y
pass List<Alsatian> listY
 
List<? super Dog> listlist.add(new Dog());YOK pass generic supertype to it

OK ADD only the generic type to it

list.add(new Animal());N
pass List<Dog>Y
pass List<Animal>Y

 

 

Templating your own Classes  

 A class can be templated (such as the collections classes have been in the API). Instance vars, method pass and return types can all be templated eg:

 

class GenExample<T>{

    T obj;

    List<T> myList;

 

    void meth1(T var){

       this.obj = var;

    }

 

    List<T> getList(){

       return myList;

    }

 

    T getObj(){

       return obj;

    }

}

 

if the class above was instantiated as  GenExample<String> eg = new GenExample<String>();

it would produce an instan ce of the class below. 

 

class GenExample<String>{

    String obj;

    List<String> myList;

 

    void meth1(String var){

       this.obj = var;

    }

 

    List<String> getList(){

       return myList;

    }

 

    String getObj(){

       return obj;

    }

}

 

Templating your own Methods 

 if you done need to template a whole class you could just template one method eg

 

public <T> void aMethod(T obj){ ….

 

here the template type MUST come before anything else including the return type.

 

For both class and methods you can put boundaries on them e.g.

 

public <T extends CharSequence> void aMethod(T obj){ ….

 

note the syntax is slightly different to that used when declaring a generice reference to something e.g. List<? extends Object> list

Disclaimer: All content on this site, is use at your own risk (Always backup before changing anything in your software/database/servers etc). Techs change, go out of date etc...
I/we accept no liability if anything you use on this site adversely affects you.