Category Archive : Java

1. Write a program to concat two string

Method name: String.concat();

public class Main {
    public static void main(String[] args) {
        String sentence1="Hello ";
        System.out.println(sentence1.concat("Java"));
    }

}
Output:
Hello Java

2. Write a program to find the length

Read more

1. Write a program to sort numeric numbers.

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] numbers={14,12,9,15,17,21,23,5,8,25};
        Arrays.sort(numbers);
        System.out.println(Arrays.toString(numbers));

    }
}
Output:
[5, 8, 9, 12, 14, 15, 17, 21, 23, 25]

2. Write a

Read more

Java HashMap is a map interface that allows us to store data by key-value pair where key should be unique.

HashMap Syntax:

HashMap<datatype1, datatype2obj= new HashMap<datatype1, datatype2();

Let’s see a simple example of HashMap to store key and … Read more

String replace method is: String.replace(“oldValue”,”newValue”);

public class Main {
    public static void main(String[] args) {
      String sentence1="Java programming is very hard";
      String newSentence=sentence1.replace("hard","easy");
      System.out.println(newSentence);
    }

}
Output:
Java programming is very easy
Read more


public class Main {
    public static void main(String[] args) {
        String format = "dd-MM-yyyy hh:mm:ss aa";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format); //passing format as constructor
        String date = simpleDateFormat.format(new Date());
        System.out.println(date);

    }
}
Output:
12-12-2020 04:06:07 PM
Read more