Found 7442 Articles for Java

Java.util.StringJoiner in Java8

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

152 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

668 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

214 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

167 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

Explain the architecture of Java Swing in Java?

raja
Updated on 24-Feb-2020 11:08:53

3K+ Views

Java Swing is a set of APIs that provides a graphical user interface (GUI) for the java programs. The Java Swing was developed based on earlier APIs called Abstract Windows Toolkit (AWT). The Java Swing provides richer and more sophisticated GUI components than AWT. The GUI components are ranging from a simple level to complex tree and table. The Java Swing provides the pluggable look and feels to allow look and feel of Java programs independent from the underlying platform.Features of Java SwingThe Java Swing is platform independent and follows the MVC (Model View and Controller) framework.Pluggable look and feel − The Java ... 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

123 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

147 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

What is ‘this’ reference in Java?

Narasimha Murthi
Updated on 29-Jun-2020 14:52:30

3K+ Views

The this is a keyword in Java which is used as a reference to the object of the current class, with in an instance method or a constructor. Using this you can refer the members of a class such as constructors, variables and methods.Using “this” you can −Differentiate the instance variables from local variables if they have same names, within a constructor or a method.class Student {    int age;    Student(int age) {       this.age = age;    } }Call one type of constructor (parametrized constructor or default) from other in a class. It is known as ... Read More

When will be an object eligible for garbage collection?

Narasimha Murthi
Updated on 29-Jun-2020 14:55:12

2K+ Views

Java Garbage collector tracks the live object and objects which are no more need are marked for garbage collection. It relieves developers to think of memory allocation/deallocation issues.When an object created in Java program is no longer reachable or used it is eligible for garbage collection.Following are some scenarios where a Java object could be unreachable/unused.Object inside a method − In Java a method is stored in the stack memory. When you call a method, JVM fetches it into the stack and executes it. After the execution it is popped out of the stack then, all its variables will be ... Read More

Can we override the equals() method in Java?

Narasimha Murthi
Updated on 29-Jun-2020 14:56:35

3K+ Views

To compare two objects the object class provides a method with name equals(), this method accepts an object and compares it with the current object. If the references of these two objects are equal, then it returns true else this method returns false.ExampleIn the following example we have a class Employee with two variables name, age and a parameterized constructor.From the main method we are creating two objects by passing same values and, comparing both values using the equals() method.Since the equals() method of the Object class returns true only if the references of the two objects are equal, this ... Read More

Advertisements