Narasimha Murthi has Published 29 Articles

How can I search a character or substring in java?

Narasimha Murthi

Narasimha Murthi

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

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 reverse a string in Java?

Narasimha Murthi

Narasimha Murthi

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

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

What is instanceof operator in Java? Explain.

Narasimha Murthi

Narasimha Murthi

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

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

What is System.exit() in Java?

Narasimha Murthi

Narasimha Murthi

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

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

Can I overload private methods in Java?

Narasimha Murthi

Narasimha Murthi

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

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

Can I overload static methods in Java?

Narasimha Murthi

Narasimha Murthi

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

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

What is the difference between String, StringBuffer and StringBuilder classes in Java explain briefly?

Narasimha Murthi

Narasimha Murthi

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

The String class of the java.lang package represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. Strings are constant, their values cannot be changed after they are created.The StringBuffer and StringBuilder classes are used when there is a necessity to ... Read More

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

Narasimha Murthi

Narasimha Murthi

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

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

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

Narasimha Murthi

Narasimha Murthi

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

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

Advertisements