
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 7442 Articles for Java

10K+ Views
Yes, you can pass objects as arguments in Java. Consider the following example: Here we have a class with name EmployeeExampleIn the following Java example, we a have a class with two instance variables name and age and a parameterized constructor initializing these variables.We have a method coypObject() which accepts an object of the current class and initializes the instance variables with the variables of this object and returns it.In the main method we are instantiating the Student class and making a copy by passing it as an argument to the coypObject() method. Live Demoimport java.util.Scanner; public class Student { ... Read More

1K+ Views
The finalize() method belongs to the Object class. Right before closing an object, the garbage collector makes sure that there are no more references to it and, calls the finalize() method on it.Therefore, once you override the finalize() method in it, you can do all the cleanup activity like closing the resources like database connection, network connection, etc.protected void finalize throws Throwable{}It is invoked only once during the execution of a program.Following are some notable points about the finalize method.Since this method belongs the Object class, which is the super class of all the classes in java, you can override ... Read More

11K+ Views
A class in a blue print/user defined datatype in java that describes the behavior/state that the object of its type support.Examplepublic class Student { String name "Krishna"; int age = 20; void greet() { System.out.println("Hello how are you"); } }An object is an instance of a class created from it using the new keyword. Once you create an object of a class, using it you can access he members of the class. In the below given code an object of the class Student is created.public class Example { public static void main(String ... Read More

4K+ Views
To compare two objects the object class provides a method with name equals(), this method accepts an object and compares it with the current object. If the references of these two objects are equal, then it returns true else this method returns false.ExampleIn the following example we have a class Employee with two variables name, age and a parameterized constructor.From the main method we are creating two objects by passing same values and, comparing both values using the equals() method.Since the equals() method of the Object class returns true only if the references of the two objects are equal, this ... Read More

1K+ Views
To convert a Date object to LocalDate object in Java −Convert the obtained date object to an Instant object using the toInstant() method.Instant instant = date.toInstant();Create the ZonedDateTime object using the atZone() method of the Instant class.ZonedDateTime zone = instant.atZone(ZoneId.systemDefault());Finally, convert the ZonedDateTime object to the LocalDate object using the toLocalDate() method.LocalDate givenDate = zone.toLocalDate();ExampleFollowing example accepts a name and Date of birth from the user in String formats and converts it to LocalDate object and prints it. Live Demoimport java.text.ParseException; import java.text.SimpleDateFormat; import java.time.Instant; import java.time.LocalDate; import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.Date; import java.util.Scanner; public class DateToLocalDate { public static void ... Read More

3K+ Views
Both JRadioButton and JCheckBox components can extend the JToggleButton class in Java, the main difference is that JRadioButton is a group of buttons in which only one button can be selected at a time, whereas JCheckBox is a group of checkboxes in which multiple items can be selected at a time. JRadioButton A JRadioButton is a component that represents an item with a state selected or unselected. Usually, a group of radio buttons is created to provide options to the user, but only one option can be selected at a time. JRadioButton will generate an ActionListener, ChangeListener, and ItemListener interfaces. Instantiating JRadioButton: JRadioButton Radio_Name = new JRadioButton(); ... Read More

2K+ Views
When there are no more references to an object, the object is finalized and when the Garbage Collection starts these finalized objects get collected this will done automatically by the JVM. We can call garbage collection directly but it doesn't guarantee that the GC will start executing immediately.We can call the Garbage Collection explicitly in two waysSystem.gc() methodRuntime.gc() methodThe java.lang.Runtime.freeMemory() method returns the amount of free memory in the Java Virtual Machine (JVM). Calling the gc() method may result in increasing the value returned by the freeMemory.ExampleLive Demopublic class GarbageCollectionTest { public static void main(String args[]) { System.out.println(Runtime.getRuntime().freeMemory()); ... Read More

4K+ Views
A common question in Java OOPs is whether abstract classes can have parameterized constructors. Yes, we can define a parameterized constructor in an abstract class. What is a Parameterized Constructor in Java A parameterized constructor is a special type of class constructor that accepts parameters/arguments when creating an object. Unlike a default (no-arg) constructor, it allows you to initialize an object with specific values at the time of creation. Syntax The Following is the syntax: public class ClassName { private dataType field1; private dataType field2; // Parameterized ... Read More

2K+ Views
The java.text package provides a class named SimpleDateFormat which is used to format and parse dates in required manner (local).Using the methods of this class you can parse String to Date or, format Date to String.Formatting Date to StringYou can format a given String to Date object using the parse() method of the SimpleDateFormat class. To this method you need to pass the Date in String format. To format a String to Date object −Instantiate the SimpleDateFormat class by passing the required pattern of the date in String format to its constructor.//Instantiating the SimpleDateFormat class SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");Format/convert the ... Read More

5K+ Views
Java provides a class named Period in the java.time package. This is used to calculate the time period between two given dates as days, months and, years etc.The between() method of this class accepts two LocalDate objects and finds out the period between the two given dates (number of years, months, and days) and returns as a period object.From this, you can extract the number of days, months and, years of the period in between using the getDays(), getMonths() and, getYears() respectively.Finding the ageIf you already have the date of birth of a person, to find the age −Get the ... Read More