Found 7442 Articles for Java

Why String class is immutable or final in Java?

Shriansh Kumar
Updated on 16-May-2025 15:48:37

9K+ Views

The general meaning of immutable is something that cannot be changed or modified after creation. In Java, a string is immutable; we cannot change the object itself, but we can change the reference to the object. The string is made final to not allow others to extend and modify it. When you modify a string, a new copy of the string with your modifications is created. This article will explain what a String is and why it is immutable and final in Java. What is a String? Like other object-oriented programming languages, almost every component of Java is an object, ... Read More

Floating Point Operations and Associativity in C, C++ and Java

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

289 Views

In C, C++, and java, we do some mathematical operations with floating point numbers. Now here we will check whether the floating point numbers are following the associativity rule or not.The answer is NO. The floating point numbers does not follow the associativity rules in some cases. Here we will see some examples.Example Code#include using namespace std; main() {    float x = -500000000;    float y = 500000000;    float z = 1;    cout

Templates in C++ vs Generics in Java

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

2K+ Views

Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.A template is a blueprint or formula for creating a generic class or a function. The library containers like iterators and algorithms are examples of generic programming and have been developed using template concept.There is a single definition of each container, such as vector, but we can define many different kinds of vectors for example, vector or vector .Example Code#include #include using namespace std; template inline T const& Max (T const& a, T const& b) { ... Read More

How to call an interface method in Java?

raja
Updated on 11-Feb-2020 08:06:59

22K+ Views

In order to call an interface method from a java program, the program must instantiate the interface implementation program. A method can then be called using the implementation object.Examplepublic interface InterfaceDemo{     default public void displayNameDefault(String name){        System.out.println("Your name is : " + name);    }     public void displayName(String name);     public void displayNameAndDesignation(String name, String designation); }The above interface defines three methods for displaying a name and optionally a job title. One method is a default method that contains implementation logic. The remaining two methods do not include implementation logic.public class InterfaceDemoImpl ... Read More

Can we declare an abstract method, private, protected, public or default in java?

Maruthi Krishna
Updated on 29-Jun-2020 13:45:25

12K+ Views

A method which does not have body is known as abstract method. It contains only method signature with a semi colon and, an abstract keyword before it.public abstract myMethod();To use an abstract method, you need to inherit it by extending its class and provide implementation to it.Declaring an abstract method privateIf a method of a class is private, you cannot access it outside the current class, not even from the child classes of it.But, incase of an abstract method, you cannot use it from the same class, you need to override it from subclass and use.Therefore, the abstract method cannot ... Read More

Can we declare a main method as private in Java?

raja
Updated on 11-Feb-2020 07:49:21

5K+ Views

Yes, we can declare the main method as private in Java.It compiles successfully without any errors but at the runtime, it says that the main method is not public.Example:class PrivateMainMethod {    private static void main(String args[]){        System.out.println("Welcome to Tutorials Point");     } }The above code is working successfully at compile time but it will throw an error at the runtime.Output:Error: Main method not found in class PrivateMainMethod, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application

Can we execute a java program without a main method?

raja
Updated on 07-Oct-2023 02:45:53

31K+ Views

Yes, we can execute a java program without a main method by using a static block.  Static block in Java is a group of statements that gets executed only once when the class is loaded into the memory by Java ClassLoader, It is also known as a static initialization block. Static initialization block is going directly into the stack memory. Example class StaticInitializationBlock{    static{       System.out.println("class without a main method");       System.exit(0);    } } In the above example, we can execute a java program without a main method (works until Java 1.6 version). ... Read More

Can we declare an abstract method final or static in java?

Maruthi Krishna
Updated on 29-Jun-2020 13:36:04

12K+ Views

A method which does not have body is known as abstract method. It contains only method signature with a semi colon and, an abstract keyword before it.public abstract myMethod();To use an abstract method, you need to inherit it by extending its class and provide implementation to it.Declaring abstract method staticIf you declare a method in a class abstract to use it, you must override this method in the subclass. But, overriding is not possible with static methods. Therefore, an abstract method cannot be static.If you still, try to declare an abstract method static a compile time error is generated saying ... Read More

Can we create an object for the abstract class in java?

Maruthi Krishna
Updated on 29-Jun-2020 13:36:39

2K+ Views

A method which does not have body is known as abstract method. It contains only method signature with a semi colon and, an abstract keyword before it.public abstract myMethod();To use an abstract method, you need to inherit it by extending its class and provide implementation to it.A class which contains 0 or more abstract methods is known as abstract class. If it contains at least one abstract method, it must be declared abstract.Instantiating an abstract classOnce a class is abstract it indicates that it may contain incomplete methods hence you cannot create an object of the abstract class.If you try ... Read More

Can we define an abstract class without abstract method in java?

Maruthi Krishna
Updated on 29-Jun-2020 13:37:31

6K+ Views

A method which does not have body is known as abstract method. It contains only method signature with a semi colon and, an abstract keyword before it.public abstract myMethod();To use an abstract method, you need to inherit it by extending its class and provide implementation to it.Abstract classA class which contains 0 or more abstract methods is known as abstract class. If it contains at least one abstract method, it must be declared abstract.And yes, you can declare abstract class without defining an abstract method in it. Once you declare a class abstract it indicates that the class is incomplete ... Read More

Advertisements