Found 4338 Articles for Java 8

Can I overload private methods in Java?

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

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.Example Live Demopublic 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 = ... Read More

What is System.exit() in Java?

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

248 Views

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 termination.ExampleFollowing program accepts an array of elements from the user and prints the it. While printing if any of the given elements is greater or equal to 20 the program exits. Live Demoimport java.util.Scanner; public class System_Exit_Example {    public static void main(String args[]){       Scanner sc = new ... Read More

How can I search a character or substring in java?

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

114 Views

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[]){       String str = new String("hi welcome to Tutorialspoint");       int index = str.indexOf('w');       System.out.println("Index of the letter w :: "+index);    } }OutputIndex of the letter w :: 3

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

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

1K+ 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

Java.util.StringJoiner in Java8

Narasimha Murthi
Updated on 29-Jun-2020 15:00:44

104 Views

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");       System.out.println(sj);    } }OutputKrishna, Raju, Satish, Pruthvi

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

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

503 Views

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 make a lot of modifications to Strings of characters.Unlike Strings, objects of type StringBuffer and String builder can be modified over and over again without leaving behind a lot of new unused objects.The StringBuilder class was introduced as of Java 5 and the main difference between the StringBuffer and StringBuilder ... Read More

What is the significance of the StringTokenizer class of the Java? Explain with an example?

Narasimha Murthi
Updated on 29-Jun-2020 15:01:27

161 Views

The java.util.StringTokenizer class allows an application to break a string into tokens.This class is a legacy class that is retained for compatibility reasons although its use is discouraged in new code.Its methods do not distinguish among identifiers, numbers, and quoted strings.This class methods do not even recognize and skip comments.Example Live Demoimport java.util.*; public class StringTokenizerDemo {    public static void main(String[] args) {       // creating string tokenizer       StringTokenizer st = new StringTokenizer("Tutorialspoint is the best site");       // counting tokens       System.out.println("Total tokens : " + st.countTokens());    } }OutputTotal tokens : 5

In how many ways can I compare Strings in Java explain with example?

Narasimha Murthi
Updated on 29-Jun-2020 15:01:58

116 Views

We can compare Strings in Java using the compareTo() method and the == operator.comapareTo() method − The compareTo() method compares two strings lexicographically.The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string.The == operator − You can compare two strings using == operator. But, it compares references of the given variables not values.The equals() method of the String class accepts a String as parameter and it compares the current string to the specified object. The result ... Read More

What is the use of StringBuffer class can anyone explain with an example?

Narasimha Murthi
Updated on 29-Jun-2020 15:02:48

73 Views

The java.lang.StringBuffer class is a thread-safe, mutable sequence of characters. Following are the important points about StringBuffer −A string buffer is like a String, but can be modified.It contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.They are safe for use by multiple threads.Every string buffer has a capacity.Example Live Demoimport java.lang.*; public class StringBufferDemo {    public static void main(String[] args) {       StringBuffer buff = new StringBuffer("tutorials ");       System.out.println("buffer = " + buff);       // appends the string argument to ... Read More

What are the main shift operators provided by Java? Explain with an example?

Narasimha Murthi
Updated on 29-Jun-2020 15:05:55

86 Views

Java provides three shift operators namely −Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.Shift right zero fill operator. The left operands value is moved right by the number of bits specified by the right operand and shifted values are filled up with zeros.Example Live Demopublic class Test {    public static void main(String args[]) {       int a = 60;/* 60 = 0011 1100 */   ... Read More

Advertisements