Skip to main content

Posts

How i+=j works in Java?It will surprise you?

According to the specification of Java for mathematical operations ,  If the left-hand operand expression is not an array access expression, then: First, the left-hand operand is evaluated to produce a variable. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason; the right-hand operand is not evaluated and no assignment occurs. Otherwise, the value of the left-hand operand is saved and then the right-hand operand is evaluated. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason and no assignment occurs. Otherwise, the saved value of the left-hand variable and the value of the right-hand operand are used to perform the binary operation indicated by the compound assignment operator. If this operation completes abruptly, then the assignment expression completes abruptly for the same reason and no assignment occurs. Otherwise, the result of the binary operation is converte...

Java : Most Frequently Asked Star Patterns in Coding Interview

Today we are you going to learn some coding tricks to show the patterns on console using stars (*). Let jump into the sections. 1. Christmas Tree :  public class ChristmasTree { public static void main(String[] args) { Scanner sc = new Scanner(System.in);         System.out.println("Christmas tree drawing program.");         System.out.print("Enter the height: ");         int n = sc.nextInt();         System.out.print("Enter the levels: ");         int level = sc.nextInt(); if (n < 1) { System.out.println("Height must be positive number."); } else if (level < 1) { System.out.println("Number of levels must be positive"); } else { for (int i = 0; i < level; i++) { printStars(n); } }         sc.close();     } private static void printStars(int height) { for (int j = 0; j < height; j++) { // Number of rows!!! f...

JDK 11 have nasty surprise? It has fast, scalable and easy to tune new garbage collector i.e. ZGC?

Java 11 is ready with new exciting features and it have nasty surprises as well, It have a newly designed Garbage Collector which is fast, scalable and easy to tune. Let's find out what is it. It is one of the most exciting feature in JDK 11. This brand new Garbage Collector, ZGC, which is being developed by Oracle that promises very low pause times on multi-terabyte heaps. JVM tuning will not be a tedious job because it is easy to tune as well. It is an experimental feature in JDK 11 later on it will available as full feature. It is scalable low latency Garbage Collector which means  it will have pause time less than 10ms no matter how big your heap is.  it is scalable, it can handle multi-terabyte heaps without affecting the performance.  Key features of ZGC are : 1. Concurrent  Marking Relocation/compaction Relocation set selection reference processing  StringTable cleaning 2. Single Generation 3. Load barriers 4. Colored pointers 5. Region based : heap ...

Why LinkedList (Java) sucks? Performance comparison with ArrayList? When to use ArrayList over LinkedList?

Collection is the most used concept in Java after String, in collection framework we are mostly rely on ArrayList and LinkedList for keeping the objects in sequential form. these two are most used implementations of List interface but LinkedList always sucks in performance although LinkedList have some advantages as well. ArrayList is what you want. LinkedList is almost always a (performance) bug. Why LinkedList sucks: It uses lots of small memory objects internally to keep connecting the objects, and therefore impacts performance across the process. Lots of small objects are bad for cache-locality. Any indexed operation requires a traversal, i.e. has O(n) performance. This is not obvious in the source code, leading to algorithms O(n) slower than if ArrayList was used. Getting good performance is tricky. Even when big-O performance is the same as ArrayList, it is probably going to be significantly slower anyway. It's jarring to see LinkedList in source because it is probably the wr...

Why rt.jar is missing from Jdk9?

Why rt.jar is missing from Jdk9? This is one of the most common question comes to our mind when we see the JDK 9 structure. Previous to JDK 9, Java was having all classes and packages into one single jar I.e. rt.jar aka Java Runtime. Which have 1000+ classes and packages. Rt.jar helps to execute the source code. It provides native support to the application. Let’s get understand with the example if you have written a program just to add two values and print the result.   Class example { public static void main(String args){ int a= 10; int b=20; int c=a+b; System.out.print(“Result ”+c); } } For example, to execute the above program on a machine that can be a PC, mobile device, calculator powered by Java must have this rt.jar and it’s huge in size. Other than PC these devices have very limited space and memory as they are developed for some specific purpose. So Java 9 came up with a new approach that is modular Java, with this approach they break this rt.jar into nearly 100 mod...

Introduction to Project Jigsaw aka Java Platform Module System? How it is going to change the big project development?

Java 9 was a major update in the Java Platform as Java introduced Java Platform Module System, also sometimes referred to as Project Jigsaw.  Jigsaw was the internally used project name during development. Later Jigsaw changed name to Java Platform Module System ( JPMS ). Java platform is totally changed by this implementation. Now java supports module based implementation. It was a must require feature and high on demand for last couple of years as it will help the developers to manage big projects in smaller independent modules and it also going to help the developers to maintain encapsulation among the packages as well, till Java 8 java supports class level abstraction only. In JDK 9, java rt.jar is also divided into the modules java 9 have 93 modules and later on with the release of java 10 these modules are increased to 99. These modules can be found in jdk home under jmods directory as you can see in the below image. What is a Module? Modularity adds a higher level of aggrega...

What is enhanced switch statement??? How Java 12 is going to change the way of writing Switch statement?

Oracle provided the Java 12 EA (Early Access) version before releasing Java 11.It shows that Oracle is committed to provide the best features to the developers asap. In order to make coding easy and more readable Java 12 is going to change the way we are writing switch statement. In the traditional way we write the code as I have written below. public static void oldSwitchStatementDemo() {      System.out.println("Old Switch Statement:");      final int inputNumber = 3;      String resultStr;    switch (inputNumber){         case 1 :            resultStr = "ONE";            break;         case 2 :            resultStr = "TWO";            break;         case 3:         ...

What is SpotBugs? How it works?

SpotBugs is the spiritual successor of FindBugs , carrying on from the point where it left off with support of its community. SpotBugs is a program which uses static analysis to look for bugs in Java code. It is free software, distributed under the terms of the GNU Lesser General Public License . SpotBugs checks for more than 400 bug patterns. If you want to check the description of these 400 bugs please go to Description of all the 400 bug Patterns of SpotBugs . SpotBugs Eclipse Plugin integrates SpotBugs seamlessly with the most popular Java IDE and allows to run SpotBugs analysis incrementally on changed files or on demand per project. A quick plugin introduction is available at http://andrei.gmxhome.de/findbugs/index.html , and a short video demo is available on Youtube as Five minutes FindBugs Eclipse Plugin Demo . SpotBugs is valuable for both beginner and experienced programmers. If you just start to write Java applications, the tool will help you to learn better programmin...

Pro Tips to improve your coding skills

All the professional coder are involve in the learning new technology and new programming language as programming world is evolving and as a coder we are facing some of the common issues like how to learn new things quickly, how to improve the coding skill etc. etc.Well learning new programming or technology is a different thing than improving coding skills. Coding skill doesn't bound with a programming language. Coding skills means the way a programmer writes his code and how he can improve the comments, code format, business logic implementation, adopting the new features of the programming like java 8 and 9 introduced some new features which are one of its kind.  Now I am going to share some of the tips to improve the coding style. First I want to make it in two categories. 1. When you are working on your workstation/official project. Gather complete requirements : Try to gather the complete requirements from your lead, project manger or client. Discuss the things with them as ...

What is cooking at Oracle for Java 11? Java 11 will include more than just features LTS and more?

Oracle’s move towards a faster release cycle means that more features and abilities are coming our way, faster than ever before.The Java 11 launch date is scheduled for September 2018, and the repository is currently open for bug fixes, as well as propositions for more JDK Enhancement Proposals (JEP) to be added to the upcoming version.  Local-Variable Syntax for Lambda Parameters (JEP 323) Epsilon: An Arbitrarily Low-Overhead Garbage Collector (JEP 318) Dynamic Class-File Constants (JEP 309) Remove the Java EE and CORBA Modules (JEP 320) Java 11 will include more than just features While this feature list is still not final and we’re expecting more JEPs to be added in the following months, Java 11 will have a little something extra. One of the main highlights that will come with Java 11, is long-term support of the platform. To understand what it means, let’s take a trip back to September 2017, when According to Oracle Java SE Support Roadmap Oracle is moving to long-term support...