Posts

Showing posts from 2018

Facts about Main Block In Java

Java When any java projects run, the first method called by JVM is main method. The entry point into application is the public static void main(String args[]) method:- Below are some facts about main method:- 1. Overload : We can overload main method with as many parameter as we want. But JVM call only the public static void main(String args[]) or  public static void main(String[] args) method. 2. Override :   We can't override main method because it is static. And secondly because overriding a method leads to method hiding.  3. Final :   We can declare main method final, but it cannot be overridden. 4. Synchronized : We can definitely synchronized main method. 5. Non-static method call : We can call non-static method from main by creating an object as a local variable in main. Then, use the object to call non-static method as usual.    

Different way to iterate over collection in Java

Collection Frameworks helps to store data in data structure. In order to get our data from collection we require some techniques to get the data. Iteration is one way. All other way to iterate are:- forEach method : It was introduced in Java 8 Enhanced for each loop :   It was introduced in Java 5 Iterator : It was introduced in Java 2. Using java.util.Iterator, we can manipulate the collection elements with the help of additional methods such as hasNext, next, etc. Loop Method : Traditional loop method is still used to iterate over indexed collection as Lists.

Different way to print Date and Time in Java

import java.util.*; import java.io.*; import java.text.*; import java.time.LocalDate; class DateTime{  public static void main (String[] args) {        Date d = new Date();   System.out.println(d);   //Mon Dec 03 10:27:49 UTC 2018   SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");   System.out.println(sdf.format(new Date()));  //03-12-2018   LocalDate dash= java.time.LocalDate.now();      String da=dash.toString();      System.out.println(da); //2018-12-03           String months[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September",  "October", "November", "December"};             int year;           GregorianCalendar gcalendar = new GregorianCalendar();       System.out.print("Date: ");       System.out.print(months[gcalendar.get(Calendar.MONTH)]);       System.out.print(&q