Skip to main content

Posts

Showing posts from July, 2018

Java 8 - Default and static methods in Interfaces

Do you know about defaults methods which got added to Java language in JDK 8 release? If yes then that’s very good but if you don’t then you have a good news. In this article we will cover everything you need to know about default methods. What are default methods? Let’s take a look at existing stuff first. As of Java 7, Java interface declares method/s which define a contract. Any class that implements the interface must provide an implementation for each method defined by the interface or inherit the implementation from a superclass. Note: These methods declared in the interface are implicitly public so you need not mention it in the declaration. Let’s see an example: interface Orderable {     int getProduct( int serialNumber);     String getProductSpecification();     float getPrice();     float getDiscount(); } Example of class implementing above interface public class Television implements Orderable {  ...

Install and play with Java 11

If you got the chance to read my other article about new features in Java 11 you already know what all features we are going to get when Java 11 releases on September 25 th . In case you haven’t read already please do check it out “Are you ready for Java 11?” here . Are you going to wait till September 25 th to try out new features? I am sure definitely not. So what you should be doing now to have some hand’s on JDK 11 features? This article is exactly about that in which I will be guiding you on how you can download JDK 11 and play with it. So without wasting any more time let’s begin. So Step 1, grab the JDK 11. You will need to head to http://jdk.java.net/11/ and download JDK 11 Early-Access build. You can download either OpenJDK build or Oracle JDK build. In order to download Oracle JDK build you will have accept the Early Adopter Development License Agreement at this point. So once you finish downloading, extract and install it on your local system. I am sur...

Local variable type inference : Welcome 'var' in Java

Yes, you read it right. ‘var’ is now available for Java developers as well. JDK 10 which got released on March 20 th , 2018 rolled out a shiny feature which allows developers to use var for declaring local variables. We all know that Java is static typed language. But wait a minute, does this inclusion of var now make Java a dynamic typed language? As we know JavaScript allows to define variables using var and it is indeed dynamically typed language. Quick refresher of static and dynamic types: Static type – Types cannot change at runtime. For example, below java code will not compile int myvar = 1; myvar = “Toyota”; myvar = ["Saab", "Volvo", "BMW"]; Dynamic type – Types can change at run time. For example, below code in JavaScript is valid var myvar; myvar = 1; myvar = “Toyota”; myvar = ["Saab", "Volvo", "BMW"]; So to answer the question “Does this inclusion of var now make Java a dynamic t...