Eyestech
  • Home
  • Apps and Services
  • Mobiles
  • Tech News
  • Contact us
Instagram
Eyestech
Eyestech
  • Home
  • Apps and Services
  • Mobiles
  • Tech News
  • Contact us
  • programming

7 Best Java Tips and Tricks of 2020

  • June 29, 2020
  • java hub
java
Total
0
Shares
0
0
0

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

java

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

Example

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

Variables

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.

Example

//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.,

Example

static final int MIN_WIDTH = 4

Packages

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

Example

com.sun.eng
com.apple.quicktime.v2
java.lang
Java.util

Iterating over an array

java

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

Example

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

Prefer int over long

java

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

Example

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
here

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.

Snippets

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

Example

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

Also read 5 best java ide’s

Total
0
Shares
Share 0
Tweet 0
Pin it 0
Avatar
java hub

Previous Article
  • Apps and Services

Best learning apps for students (2020)

  • June 28, 2020
  • Ani Singh
View Post
Next Article
video services
  • Apps and Services

5 Best Video Streaming Apps of 2020

  • June 29, 2020
  • Anu Singh
View Post

Subscribe

Subscribe now to our newsletter

You May Also Like
View Post
  • programming

Best Ide for Machine Learning and AI (2020)

  • user
  • November 2, 2020
View Post
  • java
  • programming

Strings in java: All the Methods and Definition

  • java hub
  • July 22, 2020
java ide's
View Post
  • programming

5 Best Java ide’s in 2020: Get the best development environment

  • java hub
  • June 20, 2020

Leave a Reply Cancel reply

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

Featured Posts
  • 1
    Real Truth of Micromax in note 1 : Specification & Reality
    • January 17, 2021
  • 2
    Best 6 Music Apps That Let You Take Your Music Offline
    • January 16, 2021
  • Instagram Apps for iPad 3
    5 Best Instagram Apps for iPad of 2021
    • January 14, 2021
  • 4
    6 Best Drawing Apps For Chromebook Of 2021
    • January 10, 2021
  • 5
    3 Best Voice Chat Apps For Gamers you must try
    • January 9, 2021

Subscribe

Subscribe now to our newsletter

Eyestech
Juggernaut of positrons

Input your search keywords and press Enter.