Maruthi Krishna

Maruthi Krishna

500 Articles Published

Articles by Maruthi Krishna

Page 13 of 50

Why String class is popular key in a HashMap in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 4K+ Views

A map is a collection in Java which stores key value pairs. The keys of this must not be null and each key should point to only one value. It is represented by the Map interface of java.util package. There are various classes which provides implementation to this interface.The HashMap is a class which implements the Map interface. It is based on the Hash table. It allows null values and null keys.In short, you can store key value pairs in the HashMap object. Once you do so you can retrieve the values of the respective keys but, the values we ...

Read More

How to convert String to StringBuilder and vice versa Java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 22K+ Views

The String type is a class in Java, it is used to represent a set of characters. Strings in Java are immutable, you cannot change the value of a String once created.Since a String is immutable, if you try to reassign the value of a String. The reference of it will be pointed to the new String object leaving an unused String in the memory.Java provides StringBuffer class as a replacement of Strings in places where there is a necessity to make a lot of modifications to Strings of characters.You can modify/manipulate the contents of a StringBuffer over and over again ...

Read More

Default method vs static method in an interface in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 9K+ Views

An interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.Since Java8 static methods and default methods are introduced in interfaces.Default Methods - Unlike other abstract methods these are the methods can have a default implementation. If you have default method in an interface, it is not mandatory to override (provide body) it in the classes that are already implementing this interface.In short, you can access the default methods of an interface using the objects of the implementing classes.Exampleinterface MyInterface{    public static int num = 100;    public default void ...

Read More

What is the reason behind the error “Could not found or load main class” in java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 1K+ Views

When write a Java program/class first of all you need to compile it using the javac command as −javac [name of the class].javaIf your program gets compiled without errors, a .class file (byte code) is created with the specified name. Then you need to execute it using the java command (JVM) as −java [class name]ExampleSuppose we Have created a simple class Calculator which adds two or, three numbers in the file with name Calculator.java in the path D:\sample as −public class Calculator {    int addition(int a , int b){       int result = a+b;       ...

Read More

Can super class reference variable hold sub class's object in java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 4K+ Views

Yes, the super class reference variable can hold the sub class object actually, it is widening in case of objects (Conversion of lower datatype to a higher datatype).But, using this reference you can access the members of super class only, if you try to access the sub class members a compile time error will be generated.ExampleIn the following Java example, we have two classes namely Person and Student. The Person class has two instance variables name and age and one instance method displayPerson() which displays the name and age.The Student extends the person class and in addition to the inherited ...

Read More

How do you prevent a method from getting overridden in java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 418 Views

Inheritance can be defined as the process where one (parent/super) class acquires the members (methods and fields) of another (child/sub).If two classes are related to each other with inheritance. If the super class and sub class contains same methods (same name and arguments), When we invoke this it using the sub class object, the method of the sub class will be executed. This mechanism is known as overriding.Overriding final methodsOnce you declare a method final it cannot be overridden If you try to do so, it generates a compile time error −Exampleclass Super{    public final void demo() {     ...

Read More

Can we call a constructor directly from a method in java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 10K+ Views

A constructor is similar to method and it is invoked at the time creating an object of the class, it is generally used to initialize the instance variables of a class. The constructors have same name as their class and, have no return type.There is no need to invoke constructors explicitly these are automatically invoked at the time of instantiation.The this keyword in Java is a reference to the object of the current class. Using it, you can refer a field, method or, constructor of a class.Therefore, if you need to invoke a constructor explicitly you can do so, using "this()".Invoking a ...

Read More

Sequence of execution of, instance method, static block and constructor in java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 8K+ Views

A static block is a block of code with a static keyword. In general, these are used to initialize the static members of a class. JVM executes static blocks before the main method at the time loading a class.Examplepublic class MyClass {    static{       System.out.println("Hello this is a static block");    }    public static void main(String args[]){       System.out.println("This is main method");    } }OutputHello this is a static block This is main methodA constructor is similar to method and it is invoked at the time creating an object of the class, it is ...

Read More

What is diamond problem in case of multiple inheritance in java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 13K+ Views

Inheritance is a relation between two classes where one class inherits the properties of the other class. This relation can be defined using the extends keyword as −public class A extends B{ }The class which inherits the properties is known as sub class or, child class and the class whose properties are inherited is super class or, parent class.In inheritance a copy of super class members is created in the sub class object. Therefore, using the sub class object you can access the members of the both classes.Multiple inheritanceThere are various types of inheritance available namely single, multilevel, hierarchical, multiple ...

Read More

How to make an object completely encapsulated in java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 390 Views

The process of wrapping up the data and, the code acting on the data together is known as encapsulation. This is a protective mechanism where we hide the data of one class from (an object of) another.Since, variables hold the data of a class to encapsulate a class you need to declare the required variables (that you want to hide) private and provide public methods to access (read/write) them.By doing so, you can access the variables only in the current class, they will be hidden from other classes, and can be accessed only through the provided methods. Therefore, it is ...

Read More
Showing 121–130 of 500 articles
« Prev 1 11 12 13 14 15 50 Next »
Advertisements