Narasimha Murthi has Published 29 Answers

Can we override final methods in Java?

Narasimha Murthi
Published on 05-Jul-2019 11:04:51
Overriding is a one of the mechanisms to achieve polymorphism. This is the case when we have two classes where, one inherits the properties of another using the extends keyword and, these two classes same method including parameters and return type (say, sample).Since it is inheritance. If we instantiate the ... Read More

Can I overload static methods in Java?

Narasimha Murthi
Published on 05-Jul-2019 11:01:20
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.Example Live Demopublic class Calculator {    public int addition(int a , ... Read More

Can I overload private methods in Java?

Narasimha Murthi
Published on 05-Jul-2019 10:58:52
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, ... Read More

What is System.exit() in Java?

Narasimha Murthi
Published on 05-Jul-2019 10:56:20
This method belongs to the System class of the java.lang package. It terminates the current JVM (Java Virtual Machine).This method accepts an integer value representing the status code, it accepts two values 0 or, 1 or, -1. Where, 0 indicates a successful termination and 1 or, -1 indicates an unsuccessful ... Read More

What is instanceof operator in Java? Explain.

Narasimha Murthi
Published on 05-Jul-2019 10:53:39
The instanceof operator in Java is used to find whether a reference is an instance of a Type i.e. class or an interface.Example Live Demopublic class InstanceOfExample {    public static void main(String args[]) {       String str = "hello";       boolean bool = str instanceof String;   ... Read More

How can I reverse a string in Java?

Narasimha Murthi
Published on 05-Jul-2019 10:46:06
The StringBuffer class has a method named reverse() which will reverse the contents of the current buffer. The program buffers the input String using StringBuffer (String string) method, reverse the buffer and then converts the buffer into a String with the help of toString() method.Example Live Demopublic class Sample {   ... Read More

How can I search a character or substring in java?

Narasimha Murthi
Published on 05-Jul-2019 10:43:51
You can search for a particular letter in a string using the indexOf() method of the String class. This method which returns a position index of a word within the string if found. Otherwise it returns -1.Example Live Demopublic class Test {    public static void main(String args[]){       ... Read More

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

Narasimha Murthi
Published on 05-Jul-2019 10:41:53
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 ... Read More

Explain the usage of the split() method of the String class in Java.

Narasimha Murthi
Published on 05-Jul-2019 10:39:38
The split(String regex, int limit) method of the String class. splits the current string around matches of the given regular expression.The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of ... Read More

Java.util.StringJoiner in Java8

Narasimha Murthi
Published on 05-Jul-2019 10:37:29
This class is used Join a sequence of characters separating using the delimiter.Examplepublic class StringJoinerSample {    public static void main(String[] args){       StringJoiner sj = new StringJoiner(", ");       sj.add("Krishna");       sj.add("Raju");       sj.add("Satish");       sj.add("Pruthvi");       ... Read More
Advertisements