Found 4338 Articles for Java 8

What is Overloading in Java?

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

703 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 Sample{    public static void add(int a, int b){       System.out.println(a+b);    }    public static void add(int a, int b, int c){       System.out.println(a+b+c);    }    public static void main(String args[]){       Sample obj = new Sample();       obj.add(20, 40);       obj.add(40, 50, 60);    } }Output60 150

What is ‘this’ reference in Java?

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

When will be an object eligible for garbage collection?

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

Can we override the equals() method in Java?

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

Can we pass objects as an argument in Java?

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

9K+ 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

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

Narasimha Murthi
Updated on 29-Jun-2020 14:57:55

861 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

What is the difference between object and reference in java?

Narasimha Murthi
Updated on 29-Jun-2020 14:58:28

8K+ 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

Explain the equals() method of the Object, String and, StringBuffer classes.

Narasimha Murthi
Updated on 29-Jun-2020 15:00:03

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

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

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

804 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 format a date to String in Java?

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() 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

Advertisements