Java Articles

Page 73 of 450

How can I swap two strings without using a third variable in Java?

Narasimha Murthi
Narasimha Murthi
Updated on 11-Mar-2026 2K+ Views

To swap the contents of two strings (say s1 and s2) without the third.First of all concatenate the given two strings using the concatenation operator “+” and store in s1 (first string).s1 = s1+s2;The substring method of the String class is used to this method has two variants and returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string or up to endIndex – 1, if the second argument is given.Now using the substring() method of the String class store the ...

Read More

Can I overload private methods in Java?

Narasimha Murthi
Narasimha Murthi
Updated on 11-Mar-2026 2K+ Views

Overloading is a one of the mechanisms to achieve polymorphism where, a class contains two methods with same name and different parameters.Whenever you call this method the method body will be bound with the method call based on the parameters.Overloading private methodsYes, we can overload private methods in Java but, you can access these from the same class.Examplepublic class Calculator {    private int addition(int a , int b){       int result = a+b;       return result;    }    private int addition(int a , int b, int c){       int result = a+b+c; ...

Read More

What are the modifiers allowed for methods in an Interface in java?

Venkata Sai
Venkata Sai
Updated on 11-Mar-2026 4K+ 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.In Java 7As of Java7 you can have only public, abstract as modifiers for the methods of an interface.interface MyInterface{ public abstract void display(); public abstract void setName(String name); public abstract void setAge(int age); }Using any other modifier with the methods of an interface would lead to a compile time error.From Java8From Java8 onwards interfaces ...

Read More

Does Java support multiple inheritance? Why? How can we resolve this?

Venkata Sai
Venkata Sai
Updated on 11-Mar-2026 5K+ Views

Whenever, you extend a class a copy of superclass’s members is available to the subclass object and, when you can call the method of the superclass using the object of the subclass.ExampleIn the following example, we have a class named SuperClass with a method with name demo(). We are extending this class with another class (SubClass).Now, you create an object of the subclass and call the method demo().class SuperClass{ public void demo() { System.out.println("demo method"); } } public class SubClass extends SuperClass { public static ...

Read More

Can we cast an object reference to an interface reference in java? If so when?

Venkata Sai
Venkata Sai
Updated on 11-Mar-2026 8K+ Views

Yes, you can.If you implement an interface and provide body to its methods from a class. You can hold object of the that class using the reference variable of the interface i.e. cast an object reference to an interface reference.But, using this you can access the methods of the interface only, if you try to access the methods of the class a compile time error is generated.ExampleIn the following Java example, we have an interface named MyInterface with an abstract method display().We have a class with name InterfaceExample with a method (show()). In addition to it, we are implementing the ...

Read More

Explain restrictions on using enum in java?

Venkata Sai
Venkata Sai
Updated on 11-Mar-2026 875 Views

Enumeration (enum) in Java is a datatype which stores a set of constant values. You can use enumerations to store fixed values such as days in a week, months in a year etc.You can define an enumeration using the keyword enum followed by the name of the enumeration as −enum Days {    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }Points to keep in mindYou need to keep the following points in mind while declaring an enum −It is recommended to write the name of the constants in all capital letters as −public class EnumerationExample {    enum Days { ...

Read More

Can we create an enum with custom values in java?

Venkata Sai
Venkata Sai
Updated on 11-Mar-2026 8K+ Views

Enumeration (enum) in Java is a datatype which stores a set of constant values (Strings in general). You can use enumerations to store fixed values such as days in a week, months in a year etc.enum Days { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }Custom values to the constantsInstead of declaring just string constants in an enum, you can also have values to these constants as −enum Vehicles { ACTIVA125(80000), ACTIVA5G(70000), ACCESS125(75000), VESPA(90000), TVSJUPITER(75000); }Whenever, you need to assign custom values to the constants of an enum −To hold the value of each ...

Read More

Can we have variables and methods in an enum in Java?

Venkata Sai
Venkata Sai
Updated on 11-Mar-2026 4K+ Views

Enumeration (enum) in Java is a datatype which stores a set of constant values. You can use enumerations to store fixed values such as days in a week, months in a year etc.You can define an enumeration using the keyword enum followed by the name of the enumeration as −enum Days { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }Methods and variables in an enumerationEnumerations are similar to classes and, you can have variables, methods, and constructors within them. Only concrete methods are allowed in an enumeration.ExampleIn the following example, we are defining an enumeration with name ...

Read More

Explain about field hiding in java?

Venkata Sai
Venkata Sai
Updated on 11-Mar-2026 1K+ Views

Whenever you inherit a superclass a copy of superclass’s members is created at the subclass and you using its object you can access the superclass members.If the superclass and the subclass have instance variable of same name, if you access it using the subclass object, the subclass field hides the superclass’s field irrespective of the types. This mechanism is known as field hiding.But, since it makes code complicated field hiding is not recommended.ExampleIn the following example we have two classes Super and Sub one extending the other. They both have two fields with same names (name and age).When we print ...

Read More

What is variable shadowing in java?

Venkata Sai
Venkata Sai
Updated on 11-Mar-2026 6K+ Views

In Java you can declare three types of variables namely, instance variables, static variables and, local variables.Local variables − Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.Instance variables − Instance variables are variables within a class but outside any method. These variables are initialized when the class is instantiated. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.Class (static) variables − Class variables are variables declared within a class, ...

Read More
Showing 721–730 of 4,498 articles
« Prev 1 71 72 73 74 75 450 Next »
Advertisements