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

This Reference in Java

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

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

Serialize Static Variables in Java

Venkata Sai
Updated on 29-Jun-2020 14:48:49

5K+ Views

In Java, serialization is a concept using which we can write the state of an object into a byte stream so that we can transfer it over the network (using technologies like JPA and RMI).But, static variables belong to class therefore, you cannot serialize static variables in Java. Still if you try to do so, the program gets compiled successfully but it raises an exception at the time of execution.In the following java program, the class Student has a static variable and we are trying to serialize and desterilize its object in the another class named ExampleSerialize.Exampleimport java.io.FileInputStream; import java.io.FileOutputStream; ... Read More

Remove Leading Zeroes from a String in Java

Venkata Sai
Updated on 29-Jun-2020 14:48:21

3K+ Views

Whenever you read an integer value into a String, you can remove leading zeroes of it using StringBuffer class, using regular expressions or, by converting the given String to character array.Converting to character arrayFollowing Java program reads an integer value from the user into a String and removes the leading zeroes from it by converting the given String into Character array.Exampleimport java.util.Scanner; public class LeadingZeroes {    public static String removeLeadingZeroes(String num){       int i=0;       char charArray[] = num.toCharArray();       for( ; i

Convert Date Object to LocalDate Object in Java

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

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

Declare an Interface Inside a Class in Java

Maruthi Krishna
Updated on 29-Jun-2020 14:45:53

893 Views

An interface in Java is a specification of method prototypes. Whenever you need to guide the programmer or, make a contract specifying how the methods and fields of a type should be you can define an interface.To create an object of this type you need to implement this interface, provide body for all the abstract methods of the interface and obtain the object of the implementing class.Nested interfacesJava allows writing/declaring interfaces within another interface or, within a class these are known as nested interfaces.ExampleIn the following Java example, we have a class with name Sample which contains a nested interface ... Read More

Can We Write an Interface Without Any Methods in Java

Maruthi Krishna
Updated on 29-Jun-2020 14:44:24

2K+ Views

Yes, you can write an interface without any methods. These are known as marking interfaces or, tagging interfaces.A marker interface i.e. it does not contain any methods or fields by implementing these interfaces a class will exhibit a special behavior with respect to the interface implemented.ExampleConsider the following example, here we have class with name Student which implements the marking interface Cloneable. In the main method we are trying to create an object of the Student class and clone it using the clone() method. Live Demoimport java.util.Scanner; public class Student implements Cloneable {    int age;    String name;    public ... Read More

What is a Remote Interface in Java

Maruthi Krishna
Updated on 29-Jun-2020 14:43:24

4K+ Views

A Remote interface is available in the java.rmi package it is a marking/tagging interface, it is used with remote method invocation(RMI).RMI is a mechanism that allows an object residing in one system (JVM) to access/invoke an object running on another JVM.To it is a marking interface, to mark an object of a class remote, you need to implement this interface.To create a remote interface −Create an interface that extends the predefined interface Remote which belongs to the package or, implement the Remote interface with the class, which you need to make remote.Declare all the business methods that can be invoked ... Read More

Format String to Date as DD-MM-YYYY in Java

Maruthi Krishna
Updated on 29-Jun-2020 14:42:48

17K+ 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.Parsing String to DateYou can parse 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 parse 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");Parse/convert ... Read More

Find Age When Date of Birth is Known Using Java

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

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

Advertisements