OCP

JDBC

Posted on

Java Database Connectivity I’ll start off with a small example and then give a couple of pointers regarding JDBC.   Example This example connects to a MySQL database. I’m querying the superhero database which originally contains 2 rows: id, name, first_name, last_name, good ‘1’,’Superman’,’Clark’,’Kent’,’1′ ‘2’,’Batman’,’Bruce’,’Wayne’,’1′ The code queries the database for all the names of […]

OCP

Java NIO.2

Posted on

Non blocking IO in Java, released with Java 7. Path Path represents a path on the storage system to a directory or file. Conceptually it is a replacement for the java.io.File class. Path path = Paths.get(/bla/bli/blu/blue.txt); Paths is the factory class for the interface Path. The  Paths class contains static factory methods that return a Path. […]

OCP

Java IO

Posted on

Yes, there are so many different Stream classes. (Now I am talking about IO Streaming) Here are some tips to keep them apart: All these classes inherit (in)directly from on of the following four abstract Classes InputStream OutputStream Reader Writer ! You cannot instantiate one of the above classes since they are all abstract If […]

OCP

Concurrency: threats or Threads

Posted on

In this post I go over the basis of concurrent execution in Java. I advise you to run the code snippets yourself to get the most out of it. This is an overview, not a tutorial. If you are able to explain the working of the example on the bottom yourself, you have a good […]

OCP

All is good.. except for Exceptions

Posted on

Point Out The Mistakes public class Mistakes { public static void main(String[] args) { try { throwException(); } catch (MissingResourceException e){ } catch (InputMismatchException e | FileNotFoundException e){ } catch (ParseException | ArrayIndexOutOfBoundsException e){ } catch (MissingResourceException | IllegalArgumentException e){ } catch (Throwable e){ } catch (IOException e){ } } private static void throwException() throws […]

OCP

It’s time..

Posted on

Did you know: Month.JANUARY is the same as Month.of(1) Month is an enum Month.JANUARY == 1 does not compile since Month is an enum and enums cannot be compared to an int Month.JANUARY.getValue() returns 1 because getValue is implemented as return ordinal() + 1 ZonedDateTimes can be created from a LocalDate, LocalTime and a ZoneId: LocalDate […]

OCP

Trick Question

Posted on

What is the output of the following code snippet: import java.time.*; public class DaylightSaving { public static void main(String[] args) { LocalDate date = LocalDate.of(2019, Month.MARCH, 31); LocalTime time = LocalTime.of(2,30); LocalDateTime dateTime = LocalDateTime.of(date, time); ZoneId zone = ZoneId.of(“Europe/Paris”); ZonedDateTime zonedDateTime = ZonedDateTime.of(dateTime, zone); System.out.println(zonedDateTime); } } Scroll down..           […]

OCP

OCP: Functional Programming in Java

Posted on

Java8 revolutionised Java programming with the introduction of functional programming. Here is a little quiz (you ‘ll find the answers below): 1 . What is the name of the abstract method of the Consumer interface? A. apply B. accept C. test D. get   2 . Which method can you call on Optional to perform […]

OCP

Collections: Did you know ..

Posted on

Did you know: that LinkedList implements both Queue and List that this means you can remove and add items from the beginning and end of the list in constant  time that you should use  ArrayDeque instead of Stack that a Set does not allow duplicate entries that a HashSet uses hashCode() to store its entries […]

OCP

Generics

Posted on

Did you know that: classes used as generic type parameters possibly have nothing to do with each other. public class Store<T>{…} Store<Candy> candyStore = new Store<>{}; Store<Animal> animalStore = new Store<>{}; Type erasure is removing the generics syntax from your code at compile time. public static <E> boolean containsSomeThing(E [] listOfThings, E someThing){ for (E […]