Found 4338 Articles for Java 8

Call append() method to construct a StringBuffer object in Java

Nancy Den
Updated on 26-Jun-2020 15:29:55

185 Views

The StringBuffer append() method appends the String representation of the particular argument to the sequence. It is a method of the java.lang.StringBuffer class. This method returns a reference to the object.The method changes the value of the object called in the method. Each primitive data type will have a separate method −public StringBuffer append(double d) public StringBuffer append(float f) public StringBuffer append(int i) public StringBuffer append(String str) public StringBuffer append(long l) public StringBuffer append(char[] str) public StringBuffer append(char[] str, int offset, int len) public StringBuffer append(Object obj) public StringBuffer append(boolean b) public StringBuffer append(char c) public StringBuffer append(StringBuffer sb)ExampleA program ... Read More

What does the StringBuffer append() method do in Java?

Rishi Rathor
Updated on 26-Jun-2020 15:34:24

114 Views

The StringBuffer append() method appends the String representation of the particular argument to the sequence. It is a method of the java.lang.StringBuffer class. This method returns a reference to the object.The basic syntax for append() method is as follows −public StringBuffer append(data_type variable_name)A program to illustrate the use of append() method is given as follows −Example Live Demoimport java.lang.*; public class Example {    public static void main(String[] args) {       StringBuffer s1 = new StringBuffer("The sun rises in the east ");       System.out.println("Statement : " + s1);       s1.append(true);       System.out.println("Outcome : ... Read More

Set the capacity value for a StringBuffer object in Java

Nishtha Thakur
Updated on 26-Jun-2020 15:34:47

221 Views

The capacity() method returns the current capacity. It is a part of the StringBuffer. It is the quantity of storage present for recently inserted characters, after which an allocation occurs.Declaration - The capacity() method is declared in the java.lang.StringBuffer class as follows −public int capacity()The capacity of a StringBuffer object can be set by inserting the value to be set while instantiating the StringBuffer class.Let us see an example program showing how the capacity value can be set.Example Live Demoimport java.lang.*; public class Example {    public static void main(String[] args) {       StringBuffer b = new StringBuffer(100);   ... Read More

Java program to insert a String into a Substring of StringBuffer

Nitya Raut
Updated on 26-Jun-2020 15:35:39

175 Views

The insert() method of the StringBuffer class inserts the String starting from an index specified in the parameter along with the stringThe syntax of the insert method is −original_string.insert( index , var)where var is the variable to be inserted and index is the position where the variable var is to be inserted.Let us see an example program −Example Live Demopublic class Example {    public static void main(String[] args) {       StringBuffer str = new StringBuffer("Hello World");       String a = "Wonderful ";       str.insert(6, a);       System.out.println(str);    } }OutputHello Wonderful World

Java 8 Stream Terminal Operations

Nancy Den
Updated on 26-Jun-2020 15:38:20

8K+ Views

Streams in Java have a few terminal operations. They are as follows −collect − The collect method returns the outcome of the intermediate operationsList id = Arrays.asList(“Classes", "Methods", "Members"); List output = id.stream().filter(s -> s.startsWith("M")).collect(Collectors.toList());reduce − The reduce method is reduces the elements of a stream into a single element having a certain value after computation. The BinaryOperator is an argument of the reduce method.List list1 = Arrays.asList(11, 33, 44, 21); int even = list1.stream().filter(x -> x % == 0).reduce(0, (ans, i) -> ans+i);forEach − This method iterates through every element in the streamList list1= Arrays.asList(1, 3, 5, 7); List ... Read More

Java 8 Streams and its operations

Nishtha Thakur
Updated on 26-Jun-2020 15:41:18

861 Views

Streams are sequences of objects from a source, which support aggregate operations. These were introduced in Java 8.With Java 8, Collection interface has two methods to generate a Stream.stream() − Returns a sequential stream considering collection as its source.parallelStream() − Returns a parallel Stream considering collection as its source.Some operations on streams are −sorted − The sorted method is use to sort the stream lexicographically or in ascending orderList id = Arrays.asList("Objects", "Classes", "Interfaces"); List output = id.stream().sorted().collect(Collectors.toList());map − The map method maps the elements in the collection to other objects according to the Predicate passed as a parameterList list1= ... Read More

Can we override a private or static method in Java

Nancy Den
Updated on 27-Jun-2020 12:48:55

7K+ Views

No, we cannot override private or static methods in Java.Private methods in Java are not visible to any other class which limits their scope to the class in which they are declared.ExampleLet us see what happens when we try to override a private method − Live Democlass Parent {    private void display() {       System.out.println("Super class");        } } public class Example extends Parent {    void display() // trying to override display() {       System.out.println("Sub class");    }    public static void main(String[] args) {       Parent obj = new Example(); ... Read More

Declare static variables and methods in an abstract class in Java

Rishi Rathor
Updated on 27-Jun-2020 12:51:19

6K+ Views

If a method is declared as static, it is a member of a class rather than belonging to the object of the class. It can be called without creating an object of the class. A static method also has the power to access static data members of the class.A static variable is a class variable. A single copy of the static variable is created for all instances of the class. It can be directly accessed in a static method.An abstract class in Java is a class that cannot be instantiated. It is mostly used as the base for subclasses to ... Read More

Restrictions applied to Java static methods

Nishtha Thakur
Updated on 26-Jun-2020 15:50:46

3K+ Views

If the static keyword is applied to any method, it becomes a static method.If a method is declared as static, it is a member of a class rather than belonging to the object of the class. It can be called without creating an object of the class. A static method also has the power to access static data members of the class.There are a few restrictions imposed on a static methodThe static method cannot use non-static data member or invoke non-static method directly.The this and super cannot be used in static context.The static method can access only static type data ... Read More

Object Serialization with Inheritance in Java Programming

Nitya Raut
Updated on 26-Jun-2020 15:57:47

723 Views

Serialization is the process of changing the state of an object into the byte stream so that the byte stream can return back into a copy of the objectIn Java, an object is said to be serializable if its class or parent classes implement either the Serializable interface or the Externalizable interface.Deserialization is converting the serialized object back into a copy of the object.There are three cases of Object Serialization with inheritance.The child class is automatically serializable if the parent class is serializableA child class can still be serialized even if the parent class is not serializableIf we want the ... Read More

Advertisements