Home » Java » Core Java » Page 8

Core Java

Java 8 – Collections sort() method – List Custom Sort Example By Employe Objects (Id, Name, Age)

A complete guide to Sorting Custom Objects in java. Collections.sort() method does the sorting based on Comparable or Comparator implementation. Example custom sorting for sorting Employee objects 1. Introduction In this tutorial, You’ll learn how to sort Custom objects in java. First, We’ll show the example program to sort List of Strings and Next move to theCustom sorting of Arraylist ...

Read More »

Java 14: Looking at the updated switch statement

JDK 14, released in March 2020, comes with an updated version of the switch statement. This has been a preview feature in JDK 12 and JDK 13. To see the difference, let’s look at a simple example. Assume we want to compute the daily working time based on a DayOfWeek enum. With the old way of using the switch statement, ...

Read More »

HelloWorld.java – Java Program to Print Hello World

Java Beginners’ first example program to print Hello World. 1. Introduction In this tutorial, We’ll learn how to print “Hello World” in java. This is called as first java program to beginners to the programming language. If you understand each and every word in this program then you are good to start learning java concepts. 2. Java Hello World Program ...

Read More »

Java puzzlers from OCA part 2

Welcome to the second part of Java Puzzlers from OCA. In this part we will see an interesting case about the underscore separator in numeric literals which came with Java 7. In the below class you can see the separator underscore in the decimal literal. Also notice the class compiles now without a problem. Octal is the octal representation, binary ...

Read More »

Java puzzlers from OCA part 1

java-interview-questions-answers

I’m reading Oracle Certified Associate Java SE Programmer book from Mala Gupta in my spare time and I’m surprised with some of the new things I learn. Some of the time they really don’t make sense, some of the time they make sense but really surprising to see. So in this article series, I wanted to share them as “Java ...

Read More »

Java 14: Pattern Matching for instanceof

Java 14 introduces Pattern Matching for instanceof, another preview language feature, that eliminates the need for casts when using instanceof. For example, consider the following code: 1 2 3 4 if (obj instanceof String) {     String s = (String) obj;     System.out.println(s.length()); } This code can now be rewritten as: 1 2 3 if (obj instanceof String s) {     System.out.println(s.length()); } ...

Read More »

Java 14: Records

Java 14 arrived a few weeks ago and introduces the Record type, which is an immutable data carrier class designed to hold a fixed set of fields. Note that this is a preview language feature, which means that it must be explicitly enabled in the Java compiler and runtime using the --enable-preview flag. I’m going to jump straight in with ...

Read More »

Class JavaDocs Should Be Mandatory in OOP

I’ll admit, the title is a bit of a lie. I actually believe JavaDocs are necessary everywhere: methods, attributes and classes. But let’s focus on classes for now. I should also mention that this idea applies to all object-oriented programming languages. I suppose there must be an equivalent to JavaDocs in most languages. There are many different opinions about the ...

Read More »