Found 9313 Articles for Object Oriented Programming

Initializer for final static field in Java

Rishi Raj
Updated on 30-Jul-2019 22:30:24

780 Views

The final static field variable is a constant variable. There is only one copy of this variable available. It is mandatory to initialize the final static field variable explicitly as the default value for it is not provided by the JVM. Also, this variable cannot be reinitialized.A program that initializes the final static field variable using a static initialization block is given as follows:Example Live Demopublic class Demo {    final static int num;    static {       System.out.println("Running static initialization block.");       num = 6;    }    public static void main(String[] args) {     ... Read More

Get Hash Code for Integer in Java

Arushi
Updated on 30-Jul-2019 22:30:24

2K+ Views

The HashCode for an Integer can be obtained using the hashCode() method in Java. This method does not accept any parameters and it returns a hash code value of the Integer object.A program that demonstrates the hashCode() method in Java is given as follows:Example Live Demoimport java.lang.*; public class Demo {    public static void main(String args[]) {       Integer i = new Integer(60);       System.out.println("The integer value is: " + i);       System.out.println("The Hashcode for the above value is: " + i.hashCode());    } }OutputThe integer value is: 60 The Hashcode for the above ... Read More

Obtain the hash code for a string in Java

Fendadis John
Updated on 30-Jul-2019 22:30:24

167 Views

The hashCode() method is used to obtain the hash code for a string. This method does not accept any parameters as it is a default method and it returns a hash code value.A program that demonstrates the hashCode() method in Java is given as follows:Example Live Demoimport java.io.*; public class Demo {    public static void main(String args[]) {       String str = new String("The sky is blue");       System.out.println("The string is: " + str);       System.out.println("The Hashcode for the string is: " + str.hashCode());    } }OutputThe string is: The sky is blue The ... Read More

Using the finalize() method in Java Garbage Collection

Vikyath Ram
Updated on 30-Jul-2019 22:30:24

1K+ Views

When a garbage collector determines that no more references are made to a particular object, then the finalize() method is called by the garbage collector on that object. The finalize() method requires no parameters and does not return a value.A program that demonstrates the finalize() method in Java is given as follows:Example Live Demoimport java.util.*; public class Demo extends GregorianCalendar {    public static void main(String[] args) {       try {          Demo obj = new Demo();          System.out.println("The current time is: " + obj.getTime());          obj.finalize();       ... Read More

Override the toString() method in a Java Class

Rishi Raj
Updated on 30-Jul-2019 22:30:24

5K+ Views

A string representation of an object can be obtained using the toString() method in Java. This method is overridden so that the object values can be returned.A program that demonstrates this is given as follows:Example Live Democlass Student {    private int rno;    private String name;    public Student(int r, String n) {       rno = r;       name = n;    }    public String toString() {       return rno + " " + name;    } } public class Demo {    public static void main(String[] args) {       Student ... Read More

Use static Import for sqrt() and pow() methods in class Math in Java

Fendadis John
Updated on 30-Jul-2019 22:30:24

731 Views

Static import means that the fields and methods in a class can be used in the code without specifying their class if they are defined as public static.The Math class methods sqrt() and pow() in the package java.lang are static imported. A program that demonstrates this is given as follows:Example Live Demoimport static java.lang.Math.sqrt; import static java.lang.Math.pow; public class Demo {    public static void main(String args[]) {       double num = 4.0;       System.out.println("The number is: " + num);       System.out.println("The square root of the above number is: " + sqrt(num));       ... Read More

Static import the Math Class Methods in Java

Arushi
Updated on 30-Jul-2019 22:30:24

15K+ Views

Static import means that the fields and methods in a class can be used in the code without specifying their class if they are defined as public static.The Math class method sqrt() in the package java.lang is static imported.A program that demonstrates this is given as follows:Example Live Demoimport static java.lang.Math.sqrt; public class Demo {    public static void main(String[] arg) {       double num = 36.0;       System.out.println("The number is: " + num);       System.out.println("The square root of the above number is: " + sqrt(num));    } }OutputThe number is: 36.0 The square root ... Read More

How to import all classes in Java?

Vikyath Ram
Updated on 30-Jul-2019 22:30:24

11K+ Views

All the classes in a package can be imported using the import statement along with the character *. For example - All the classes in the package java.util can be imported using import java.util.*;A program that demonstrates this in Java is given as follows:Example Live Demoimport java.util.*; public class Demo {    public static void main(String args[]) {       Stack stack = new Stack();       stack.push("Apple");       stack.push("Mango");       stack.push("Guava");       stack.push("Pear");       stack.push("Orange");       System.out.println("The stack elements are: " + stack);    } }OutputThe stack elements ... Read More

Multiple inheritance by Interface in Java

Arushi
Updated on 02-Sep-2023 10:19:21

73K+ Views

An interface contains variables and methods like a class but the methods in an interface are abstract by default unlike a class. Multiple inheritance by interface occurs if a class implements multiple interfaces or also if an interface itself extends multiple interfaces.A program that demonstrates multiple inheritance by interface in Java is given as follows:Example Live Demointerface AnimalEat {    void eat(); } interface AnimalTravel {    void travel(); } class Animal implements AnimalEat, AnimalTravel {    public void eat() {       System.out.println("Animal is eating");    }    public void travel() {       System.out.println("Animal is travelling");   ... Read More

What does Interface consist of in Java

Jai Janardhan
Updated on 30-Jul-2019 22:30:24

219 Views

An interface can be defined using the interface keyword. It contains variables and methods like a class but the methods in an interface are abstract by default unlike a class. An interface is primarily used to implement abstraction and it cannot be instantiated.A program that demonstrates an interface in Java is given as follows:Example Live Demointerface AnimalSound {    abstract void sound(); } class CatSound implements AnimalSound {    public void sound() {       System.out.println("Cat Sound: Meow");    } } class DogSound implements AnimalSound {    public void sound() {       System.out.println("Dog Sound: Bark");    } } ... Read More

Advertisements