Type to search


7 Best Java Tips and Tricks of 2020


Naming Convention

Class and Interface

Class and Interface names should be nouns in Pascal Case (Each internal word’s first letter must be capitalized). Use Whole words to avoid unwanted abbreviations and acronyms.

Example :

Class MonsterCar
Interface SortedMap



Methods should be named after verbs. It must be written in camelCase (First internal word are small letter, other internal words first letter capitalized)


void speedUp(int speed);
void addPower(int power);


Variable names should be sharp and meaningful. Should not start with an underscore(‘_’) or dollar sign ‘$’ characters. Should be mnemonic i.e, designed to indicate to the casual observer the intent of its use. One-character variable names should be avoided except for temporary variables. Common names for temporary variables are i, j, k, m, and n for integers; c, d, and e for characters.


//Variables for MonsterCar class
int speed = 0;
int gear = 1

Constant variables

Constant variables names should be all uppercase with words separated by underscores (“_”). Many constants are available in Float, String, etc.,


static final int MIN_WIDTH = 4


The prefix of a unique package name is always written in all-lowercase ASCII letters and should be one of the top-level domain names, like com, edu, gov, mil, net, org. Subsequent components of the package name vary according to an organization’s own internal naming conventions



Iterating over an array


If we want to access all elements of the array without considering the order we can use enhanced for loop for this purpose


int[] arr = new int[]{1,2,3,4,5};
for (int val : arr) 
System.out.print(val + “ “);
1 2 3 4 5

Prefer int over long


Some cases only need large numbers. So use int instead of long in those cases. In the overall project, you’ll save some memory


Int health = 0; ( health is meant to be between 0 to 100 )
If you want to use numbers less than Integer.MIN_VALUE(-2147483648) and greater than Integer.MAX_VALUE(2147483647). But when only when
it is actually needed

Mutable and Immutable Strings

Understand the difference between the Mutable and Immutable Strings. Mutable strings are meant to be modifiable while Immutable string
is not modifiable.

Mutable Strings – StringBuffer, StringBuilder
Immutable String – String

Note: whenever you need to modify a string many times use mutable strings over immutable strings.

Useful Math Library

Computer science is correlated with Math. So Whenever you solve any problems. You are in the need of some constant values and the Basic
Mathematical functions. java.lang.Math serves this purpose.

Some examples are
Constants – Math.PI, Math.E, Methods random(), floor(), ceil, round(), log(), sin() and much more .For a full list of methods and constants check

Ternary Operator

Ternary operator or Conditional Operator is very much use full when you are in the need of assigning a value to a variable based on the
condition and also printing something based on the condition.


int up = value > 100 ? 1 : 0;
System.out.println(age >= 18 ? “Adult” : “Child”);

Prime Number

Prime numbers are pretty cool right. We might end up finding a prime number in some of the problems. For that, we have an inbuild function from the BigInteger which is isProbablePrime()

BigInteger b = new BigInteger(821); returns true

Note: Returns true if this BigInteger is probably prime, false if it’s definitely composite. If certainty is ≤ 0, true is returned.

Reversing Data

There is a case where we need to reverse the things in the group. We also have dedicated methods for that too.

Collections.reverse() to reverse the specified Collections. A string can also be reversed in by using the reverse() function in the StringBuffer or StringBuilder class


List<Integer> al = new ArrayList<Integer>();
System.out.println(al) // [1, 2, 3, 4]
System.out.println(al) // [4, 3, 2, 1]
StringBuffer sb = new StringBuffer(“Hello”);
System.out.println(sb); //olleH

Also read 5 best java ide’s

Leave a Comment

Your email address will not be published. Required fields are marked *