Narasimha Murthi has Published 23 Articles

Can we override the equals() method in Java?

Narasimha Murthi

Narasimha Murthi

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

3K+ 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 ... Read More

When will be an object eligible for garbage collection?

Narasimha Murthi

Narasimha Murthi

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

2K+ Views

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 ‘this’ reference in Java?

Narasimha Murthi

Narasimha Murthi

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

2K+ Views

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

661 Views

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

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

Narasimha Murthi

Narasimha Murthi

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

739 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 ... 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

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

How to format a date to String in Java?

Narasimha Murthi

Narasimha Murthi

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

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

How can I search a character or substring in java?

Narasimha Murthi

Narasimha Murthi

Updated on 30-Jul-2019 22:30:26

89 Views

You can search for a particular letter in a string using the indexOf() method of the String class. This method which returns a position index of a word within the string if found. Otherwise it returns -1.Example Live Demopublic class Test {    public static void main(String args[]){       ... Read More

What is System.exit() in Java?

Narasimha Murthi

Narasimha Murthi

Updated on 30-Jul-2019 22:30:26

233 Views

This method belongs to the System class of the java.lang package. It terminates the current JVM (Java Virtual Machine).This method accepts an integer value representing the status code, it accepts two values 0 or, 1 or, -1. Where, 0 indicates a successful termination and 1 or, -1 indicates an unsuccessful ... Read More

Can I overload private methods in Java?

Narasimha Murthi

Narasimha Murthi

Updated on 30-Jul-2019 22:30:26

2K+ Views

Overloading is a one of the mechanisms to achieve polymorphism where, a class contains two methods with same name and different parameters.Whenever you call this method the method body will be bound with the method call based on the parameters.Overloading private methodsYes, we can overload private methods in Java but, ... Read More

Advertisements