Narasimha Murthi

Narasimha Murthi

13 Articles Published

Articles by Narasimha Murthi

13 articles

Can we override final methods in Java?

Narasimha Murthi
Narasimha Murthi
Updated on 30-Jun-2020 2K+ Views

Overriding is a one of the mechanisms to achieve polymorphism. This is the case when we have two classes where, one inherits the properties of another using the extends keyword and, these two classes same method including parameters and return type (say, sample).Since it is inheritance. If we instantiate the subclass a copy of superclass’s members is created in the subclass object and, thus both methods are available to the subclass.When we invoke this method (sample) JVM calls the respective method based on the object used to call the method.Overriding final methodsNo, you cannot override final method in java. If ...

Read More

What are the main shift operators provided by Java? Explain with an example?

Narasimha Murthi
Narasimha Murthi
Updated on 29-Jun-2020 177 Views

Java provides three shift operators namely −Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.Shift right zero fill operator. The left operands value is moved right by the number of bits specified by the right operand and shifted values are filled up with zeros.Example Live Demopublic class Test {    public static void main(String args[]) {       int a = 60;/* 60 = 0011 1100 */   ...

Read More

Java.util.StringJoiner in Java8

Narasimha Murthi
Narasimha Murthi
Updated on 29-Jun-2020 180 Views

This class is used Join a sequence of characters separating using the delimiter.Examplepublic class StringJoinerSample {    public static void main(String[] args){       StringJoiner sj = new StringJoiner(", ");       sj.add("Krishna");       sj.add("Raju");       sj.add("Satish");       sj.add("Pruthvi");       System.out.println(sj);    } }OutputKrishna, Raju, Satish, Pruthvi

Read More

What is the difference between object and reference in java?

Narasimha Murthi
Narasimha Murthi
Updated on 29-Jun-2020 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

How many times finalize method will be invoked? who invokes finalize() method in Java?

Narasimha Murthi
Narasimha Murthi
Updated on 29-Jun-2020 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

Can we pass objects as an argument in Java?

Narasimha Murthi
Narasimha Murthi
Updated on 29-Jun-2020 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

When will be an object eligible for garbage collection?

Narasimha Murthi
Narasimha Murthi
Updated on 29-Jun-2020 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 a Java object could be unreachable/unused.Object inside a method − In Java a method is stored in the stack memory. When you call a method, JVM fetches it into the stack and executes it. After the execution it is popped out of the stack then, all its variables will be ...

Read More

What is ‘this’ reference in Java?

Narasimha Murthi
Narasimha Murthi
Updated on 29-Jun-2020 3K+ 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 variables from local variables if they have same names, within a constructor or a method.class Student {    int age;    Student(int age) {       this.age = age;    } }Call one type of constructor (parametrized constructor or default) from other in a class. It is known as ...

Read More

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

Narasimha Murthi
Narasimha Murthi
Updated on 29-Jun-2020 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

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

Narasimha Murthi
Narasimha Murthi
Updated on 29-Jun-2020 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
Showing 1–10 of 13 articles
« Prev 1 2 Next »
Advertisements