Posts

Showing posts from 2019

Explain briefly about POJO in Java?

Explain briefly about POJO in Java? POJO is Plain old java object. The class with its private attributes and the having the getter and setter method. these methods are public so any class from out side can access this attributes via the methods only not directly. Example : import java.util.Date; public class Student { private int id; private String name; private Date dob; public Date getDob() { return dob; } public void setDob(Date dob) { this.dob = dob; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } Here you can see the Student class POJO with id , name and DOB attributes and having the getter and setter methods.

How do I create an array of a string?

How do I create an array of a string? Here is the example code. public class ArrayDemo { public static void main(String[] args) { String[] strArray = new String[2]; strArray[0] = "Hello"; strArray[1] = "World !"; System.out.println(strArray[0] +" "+strArray[1]); } } Output: Hello World !

In Java, how would you create a function that prints out a string every 10 seconds?

In Java, how would you create a function that prints out a string every 10 seconds? Below is the program to print string in every 10 seconds. import java.util.TimerTask; public class PrintHelper extends TimerTask { int i = 0; @Override public void run() { System.out.println("Timer ran " + ++i); } } import java.util.Timer; import java.util.TimerTask; public class TimerTest { public static void main(String[] args) { Timer timer = new Timer(); TimerTask task = new PrintHelper(); timer.schedule(task, 0, 10000); } }  Here I am using java Timer class to print string in every specified time. Output: Timer ran 1 Timer ran 2 Timer ran 3 Timer ran 4 Timer ran 5 Timer ran 6 Timer ran 7 Timer ran 8

What are the predefined abstract classes in Java?

What are the predefined abstract classes in Java? There are many but some of them are AbstractMap<K,V>, AbstractMap<K,V>,AbstractCollection<E>,AbstractList<E>  ext.

What is abstract modifier in Java?

What is abstract modifier in Java? Abstract is keyword in java which can be used with class and the method. If You define the any method as abstract in any class then you don't need to provide the implementation of that method but, its child concrete class must have to provide the  implementation of it. If you declare any method as abstract in any class you must have to declare the class as abstract. but if the class is abstract that does not mean that you will have the abstract method in it.

How to know the compiled version of jar file or class file?

How to know the compiled version of jar file or class file? To know the complied version of the jar file or class file below is the code. You have to run the MainClass and pass the file name in the argument. You have to put jar or class file in same path of MainClass file. import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.nio.file.CopyOption; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.util.Enumeration; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.logging.Logger; public class CheckVersionUtil { private static final Logger LOGGER = Logger.getLogger(CheckVersionUtil.class.getSimpleName()); public static void copyToTemp(String destinationDir, String srcFile) throws IOException { LOGGER.info(...

How can I find the current Java version running in my computer?

How can I find the current Java version running in my computer? To check the java running version run below command. java -version To check the running java compiler version on your computer run below command. javac -version

Main method not found in class Test, please define the main method as: public static void main(String[] args)

Image
Main method not found in class Test, please define the main method as: public static void main(String[] args) Ans. Check the main method with argument type String[].

What are all the Classes and Interfaces that are available in the collections in Java?

Image
What are all the Classes and Interfaces that are available in the collections in Java? You can check in eclipse ide.  1. Go to the interface Collection in eclipse. 2. right click it and see type hierarchy. You can see as below. You can check all the methods also.

How do I sort objects of ArrayList by its date?

How do I sort objects of ArrayList by its date? Here is the example How to sort the Students by their Date of birth. import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; public class ArrayListSort2 { public static void main(String[] args) throws ParseException { SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy"); List list = new ArrayList<>( Arrays.asList(new Student[] { new Student(1, "Hardik", format.parse("31/12/1998")), new Student(2, "Rohit", format.parse("30/06/1991")), new Student(3, "Aaryan", format.parse("05/05/1995")) })); System.out.println("Sort by ascending order."); Collections.sort(list, new ConpareByDate(ShortBy.ASC)); System.out.println(list); System.out.println("Sort by d...

Java: How do you sort an ArrayList in descending order?

Java: How do you sort an ArrayList in descending order? There can be 2 scenarios to shot the ArrayList.  (1) Sort the ArrayList which contains the in build types like Long, String,Integer ect. (2) Sort ArrayList which contains the custom type. Let's say List of Student class and you want to sort by name. See the code for both scenario. (1) Sort List of inbuilt types import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class ArrayListSort1 { public static void main(String[] args) { List list = new ArrayList<>(Arrays.asList(new Long[] { 50l, 4l, 10l, 1l, 2l, 3l })); System.out.println("Before Short"); System.out.println(list); System.out.println("After Short"); list.sort(Collections.reverseOrder()); System.out.println(list); } } You can see the output as below: Before Short [50, 4, 10, 1, 2, 3] After Short [50, 10, 4, 3, 2, 1] ...

How many ways can I create a thread in Java?

How many ways can I create a thread in Java? There are 2 ways to create thread in java. 1. Using the Thread class. 2. Using the Runnable Interface. Below are the examples: 1. Using the Thread class. public class ThreadDemo1 { public static void main(String[] args) { Printer1 thread1 = new Printer1(); thread1.setName("Simple Thread"); thread1.start(); } } class Printer1 extends Thread{ @Override public void run() { System.out.println("Inside "+Thread.currentThread().getName()); } } Here you can see that we extends the Thread class and implements its run method. We have to create thread class object and call the start() method of it to start the execution of the thread. 2. Using the Runnable Interface. public class ThreadDemo2 { public static void main(String[] args) { Printer2 task = new Printer2(); Thread thread2 = new Thread(task); thread2.setName("Runnable Thread"); thread2.star...

How do I copy an object in Java?

How do I copy an object in Java? Upcoming post...

How do you count the number of String objects created in a program in Java? What is the easiest way to identify it?

How do you count the number of String objects created in a program in Java? What is the easiest way to identify it? First of all you need to understand that the String object is the special object in java. There are two types of memories which are used for Sting object. One is string pool and other is the main memory where object will be stored. Upcoming answer of this question in details...

What is meant by method overriding in Java?

Method overriding in Java The meaning of method overriding is that You have parent class which has any method and you inherits that class so you can call that method at run time. but, If you want specific behavior when that method is called then you can override it. See below example. public class Animal { public Animal(String name) { super(); this.name = name; } private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public void display(){ System.out.println("Displaying Animal. name = "+name); } } public class Dog extends Animal { public Dog(String name) { super(name); } public static void main(String[] args) { Animal dog = new Dog("Abc"); dog.display(); } } When you run the Dog class it will display below output. o/p: Displaying Animal. name = Abc Now when you edit the Dog class as below. public c...