12 Java Basic Features You May not Know

Maikel Chandika
Kongkow IT Medan
Published in
7 min readNov 10, 2017

Recently I took the Oracle Certified Associate Java Programmer 8 (OCAJP). Even has been become a Java programmer for many years, there are still several new things, especially the detail things or the standard term about Java basic features which was I just know. Some of those may happened because I rarely did the systematically and elaborate learning before, I just learn by practice, or some features are not frequently used too.

This post is intended for beginner to intermediate programmer in Java. As you are going to know something basic yet useful to implement into your code or comprehension. If you are experienced Java programmer, feel free to skip it.

I have summarized into 12 sections, with explanation for each section. Please note that all code are test within JDK 8.

1. Initializer Block & Initialization Order

There’re static and non-static initializer block. The block of code which will be execute while creating instance of the object, before the constructor blocks be executed.

The non-static initializer block is look like this:

The static initializer block is look like this:

So, while you instance a new object, the initialization order (execution) will be:

  1. Static variable direct assignment.
  2. Static initializer block.
  3. Instance variable direct assignment.
  4. Non-static initializer block.
  5. Constructors.

2. Logical Operators

Java have 2 kind of logical operator, the short-circuiting and not short-circuting.

For not short-circuiting operator, both left and right side operant will be evaluate (execute). Conversely, for short-circuiting operator, dont evaluate the right side operant if it isnt necessary.

Example, for && operator, if the left side is false, it will skip to evaluate the right side. As well as, for || operator, if the left side is true, it will skip to evaluate the right side.

Take a look at example below:

And the result is going to be:

As shown above, the short-circuiting operator just evaluated the left side operand when the right side does not necessary.

3. Integer Wrapper Cache Implementation (Cache)

Integer cache implementation is the feature since Java 5. It’s related to memory efficient and performance improvement for Integer type object handling. Integer objects are cached internally and reused via the same referenced objects. The rules are:

  • Applicable for Integer values in range between –127 to +127.
  • This Integer caching works only on auto-boxing. Integer objects will not be cached when they are built using the constructor.
  • This auto-boxing is also similar, if we’re using the valueOf method.

Is all of the rules above applied, then Integer will be cache.

4. Checked & Unchecked Exception

There are 2 kind of exceptions in Java, the checked and un-checked exception. The super class of all exception class in Java is java.lang.Throwable. It also have 3 different purpose sub-classes.

  1. java.lang.Exception, is categorized as checked exception, and their sub-classes.
  2. java.lang.RuntimeException, is categorized as un-checked exception, and their sub-classes.
  3. java.lang.Error, is categorized as un-checked exception, and their sub-classes.

The checked exception are the exceptions that need to be handled, try ... catch ... finally or throws statement on method/constructor header. Conversetly, it no need for un-checked exceptions.

5. Covariant Return Type

The covariant return type is the return type that may vary in the same direction as the subclass. So, it is possible to override method by changing the return type as long as it is not primitive type and its wrapper class, moreover it should be the sub-class type.

Take a look at example below:

6. Multi-Dimensional Array Initialization

When we initialize a multi-dimensional array, it only necessary for us to define the first dimension size and left the rest of others size initialize as needed (1+n). For instance, we usually define a two dimensional array like this:

Actually it is equivalent to something like this:

Further more, we can define different array size for 1+n dimension. Take a look at the example below:

And the output:

Bonus:

All of these are valid way to define the empty two dimensional array.

7. Access Modifier Hierarchy on Method Overriding.

Java Access Modifier Hierarchy, from the most isolated are private -> <default> -> protected -> public. Except for private access modifier, because the resources only can be access from inside the class or its inner class. For the others, when overriding methods, the access modifier level must be same or at higher hierarchy.

The TestB class will have compile time error cause of downgraded access modifier on method printSomething. We can solve this by change its access modifier to protected or public.

8. StringBuffer vs StringBuilder

Basically, StringBuffer methods are synchronize, while StringBuilder are not. StringBuilder is the commonly use, unless we really are trying to share a buffer between threads, cause using synchronized methods in a single thread is overkill. From performance perspective, StringBuilder is relatively faster than StringBuffer because of not synchronized. Take a look at example classes below and the result, that run separately on same machine.

Base on each output, StringBuilder seems faster than StringBuffer.

9. Numeric Wrapper Literal Assignment Behaviour

The numeric wrapper class Byte,Short,Integer,Long,Float,Double can be assigned to value literally because of auto-boxing features. But it have different interpretation from its primitive one.

For integer number type wrapper, it only apply to Byte,Short,Integer. And for floating number type wrapper only accept fractional value to Double, unless we use literal type suffix (L,F,D).

10. Type Casting

Type Casting is when we assigning a value of one type to a variable of another type.

As above example, We have a x variable which is int type, then assigned to y which is byte type.

There are 2 kind of Type Casting:

  1. Implicit Cast (Widening Casting), It take place when two types are compatible or the target type is larger than the source type. In object reference type, the target type (reference class) should be the super-classes or higher hierarchy from the source type (object reference).
  2. Explicit Cast (Narrowing Casting), It happen when assign the larger type value to a variable of smaller type. In object reference type, the source type should be the one that have been assigned with object reference of same class or its lower hierarchy.

Take a look at example classes below:

The last line will cause RuntimeException because of class D cannot be cast to B. These casting behaviour apply for interface and its implements classes as well.

11. Passing by Value & Reference

When we pass the primitive data type variable to method, it actually pass the value only, and it will create another local variable within the methods, this is called Pass by Value.

This behaviour also apply when you pass String and other Wrapper Class (Integer,Double, etc) variable, you will pass the reference, but since they are immutable object, when you manipulate the value, it will create a new reference object instead of manipulate the existing one. But for mutable object it will pass the reference, and these are called Pass by Reference.

Take a look at the code example below and its output.

12. Numeric Promotion

Numeric Promotion is the conversion of a smaller numeric type into a larger numeric type implicitly.

The Numeric Promotion rules:

  1. If two values have different data types, it will automatically promote one of the values to the larger of the two data types.
  2. If one of the values is integral and the other is floating-point, it will automatically promote the integral value to the floating-point value’s data type.
  3. Smaller data type, byte, short and char, are first promoted to int any time they are used with a Java binary arithmetic operator, even if neither of the operands is int.
  4. The resulting value will have the same data type as its promoted operands.

Take a look at these cases.

Case-1: i * l , long is larger than int, so int value is promoted to long, and resulting value is long.

Case-2: d + f , double is larger than float, so float value is promoted to double, and resulting value is double.

Case-3: x / y , both will promoted to int before operation, and resulting value is int (not double).

Case-4: a * b / c , first a will promoted to int because of short, then a will be promoted to float so it can be multiplied with b. The result of a * b will automatically promoted to double, so it can be divide with c, and resulting value is double.

Furthermore about Java Conversions and Promotions, please visit this page.

Now we are reach the end of this post. These 12 list are based on my experience and observation, of course there are still plenty of Java platform features, keep exploring.

I hope you enjoy and found something useful.

--

--