July 14 2016

Java compareToIgnoreCase example

Java compareToIgnoreCase example

The method ‘compareToIgnoreCase’ compares two strings ignoring case differences. It is similar to the ‘compareTo’ method, ‘compareTo’ method compares two strings with respect to case.

Syntax

int compareToIgnoreCase(String str)

Return type

The return type of the method is integer. If the return value is

  • >0 current string is greater than input string
  • =0  both the strings are equal
  • <0 current string is less than input string

Example Continue reading

August 18 2015

Array of objects in java

Array of objects in java

Arrays are objects that can store multiple variables of same data type. The element of the array can be accessed using index number. The index of the array will always start from zero. If array’s size 5, the index will be from 0 to 4.

Array can hold either primitives or object references.If array is initialized the elements will get their default values, primitives will get the default values as per the data type whereas object references will get null.

 Array of Primitives

In the below example, marks is an array of primitives(int). Values can be initialized using index.

int[] marks = new int[5];

marks[1] = 10;

Array of Object References

In the below example, empArray is an array of Employee Object References. The Employee object reference has to be assigned to the array using index. Continue reading

August 5 2015

Compare two dates in java

Compare two dates in java

Dates can be compared using different ways

1. Comparing using compareTo method

public int compareTo(Date date)

The java.util.Date.compareTo(Date date) method compares two Dates. compareTo method returns 0 if this date is equal to argument date, positive value (>0) if this date is after the argument date and negative value (<0) if this date is before the argument date.

Example Continue reading

August 5 2015

Java date difference in days

Java date difference in days

Convert the dates into milliseconds using the getTime() method and calculate the difference (subtract) between them.

long diffMs = date2.getTime() – date1.getTime();

diffMs will have the difference between dates in milliseconds. diffMs can be converted to days manually or using java.util.concurrent.TimeUnit

long diffDays = diffMs / (24 * 60 * 60 * 1000);

or

long diffDays = TimeUnit.DAYS.convert(diffMs, TimeUnit.MILLISECONDS)) Continue reading

July 7 2015

Static variables in java

Static variables in java

The variables which are marked with static modifier are called static variables. Static variables will have only one copy in memory and that is shared by all the objects, any modification to it will also affect other objects. Static variables are also called as class variables.

The instance variables are per object whereas static variables are per class. Static variable can be accessed with class name as well as object reference.

Local variables cannot be marked as static. Static variable are not of object’s state and are not serialized. Static variable gets loaded when class is loaded by ClassLoader, and would be removed when it is unloaded.

Example Continue reading

July 3 2015

Local variables in java

Local variables in java

Variables which are declared inside the methods are called local variables. They are visible only inside the method in which they are declared. Local variables cannot have any access specifiers and they cannot be declared with any modifiers except final.

Syntax

[modifier] datatype variables=intialvalue;

modifier – final

datatype – primitives | object references

Local variables do not get default values like instance variables and have to be initialized before they are used. Continue reading

July 2 2015

Instance variables in java

Instance variables in java

Instance variables are the variables defined inside the class and outside the methods. Instance variables can be accessible throughout the class. They are also called object variables. Instance variables are accessible using object and every object will have its own copy of the class’s instance variables.

Syntax

accessspecifier  [modifier] datatype variablename;

access specifier – public | protected | default | private

modifier – final | transient

datatype – primitives | object references Continue reading