Narasimha Murthi has Published 29 Articles

Can we pass objects as an argument in Java?

Narasimha Murthi

Narasimha Murthi

Updated on 29-Jun-2020 14:57:14

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 ... Read More

Can we override the equals() method in Java?

Narasimha Murthi

Narasimha Murthi

Updated on 29-Jun-2020 14:56:35

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 ... Read More

When will be an object eligible for garbage collection?

Narasimha Murthi

Narasimha Murthi

Updated on 29-Jun-2020 14:55:12

Java Garbage collector tracks the live object and objects which are no more need are marked for garbage collection. It relieves developers to think of memory allocation/deallocation issues.When an object created in Java program is no longer reachable or used it is eligible for garbage collection.Following are some scenarios where ... Read More

What is Inheritance in Java? Explain with an example

Narasimha Murthi

Narasimha Murthi

Updated on 29-Jun-2020 14:53:01

Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another. With the use of inheritance, the information is made manageable in a hierarchical order.The class which inherits the properties of other is known as subclass (derived class, child class) and the class ... Read More

What is ‘this’ reference in Java?

Narasimha Murthi

Narasimha Murthi

Updated on 29-Jun-2020 14:52:30

The this is a keyword in Java which is used as a reference to the object of the current class, with in an instance method or a constructor. Using this you can refer the members of a class such as constructors, variables and methods.Using “this” you can −Differentiate the instance ... Read More

What is Overloading in Java?

Narasimha Murthi

Narasimha Murthi

Updated on 29-Jun-2020 14:51:18

When a class has two or more methods by the same name but different parameters, at the time of calling based on the parameters passed respective method is called (or respective method body will be bonded with the calling line dynamically). This mechanism is known as method overloading.Example Live Demopublic class ... Read More

What is overriding in Java can explain briefly with an example?

Narasimha Murthi

Narasimha Murthi

Updated on 29-Jun-2020 14:50:48

If superclass and subclass have methods with same name including parameters. JVM calls the respective method based on the object used to call the method. i.e. if the current object using which the method is called, is of superclass type the method of the superclass is executed.Or, if the object ... Read More

How to convert a Date object to LocalDate object in java?

Narasimha Murthi

Narasimha Murthi

Updated on 29-Jun-2020 14:46:51

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 ... Read More

How to find the age when date of birth is known? Using Java?

Narasimha Murthi

Narasimha Murthi

Updated on 29-Jun-2020 14:41:59

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, ... Read More

How to format a date to String in Java?

Narasimha Murthi

Narasimha Murthi

Updated on 29-Jun-2020 14:41:19

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() ... Read More

Advertisements