TODAY JAVA SCHOOL

In java school, programming, design, computer general knowledge, web application, software, web services, social media, digital marketing, oops, concept of programming language, oops feature, console media, graphics medium, first programming, c, c ++ , Java, PHP, SQL, MySQL, HTML, HTML_5, J_query, JavaScript, Bootstrap, Framework, images with logos, examples, shared and explained.

https://www.amazon.in/b?node=26373545031&linkCode=ll2&tag=1234567801cdb-21&linkId=3b9882431b00409b44141e0344b35a15&language=en_IN&ref_=as_li_ss_tl

Breaking

Saturday 21 December 2019

String in java | String in java example syntax


String in Java

 


Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character ‘\0’. In Java, string is basically an object that represents sequence of char values. An array of characters works same as Java string
Syntax:
<String_Type> <string_variable> = “<sequence_of_string>”;
Example
String str=”Greeks”;

 

String in java
String in java


Memory allotment of String


Whenever a String Object is created, two objects will be created- one in the Heap Area and one in the String constant pool and the String object reference always points to heap area object.
For example:

                String str=”Greeks”;



 
string in java
java string

An Example that shows how to declare String

// Java code to illustrate String
  
import java.io.*;
import java.lang.*;
  
class Test {
    public static void main(String[] args)
    {
        // Declare String without using new operator
        String s = "GeeksforGeeks";
  
        // Prints the String.
        System.out.println("String s = " + s);
  
        // Declare String using new operator
        String s1 = new String("GeeksforGeeks");
  
        // Prints the String.
        System.out.println("String s1 = " + s1);
    }
}

Output:

String s =GeeksforGeeks
String s1 =GeeksforGeeks

Interfaces and Classes in Strings in Java

·         CharBuffer: This class implements the CharSequence interface. This class is used to allow character buffers to be used in place of CharSequences. An example of such usage is the regular-expression package java.util.regex.
·         String: String is a sequence of characters. In java, objects of String are immutable which means a constant and cannot be changed once created.

Creating a String
      There are two ways to create string in Java:

·       String literal
String s =”GeeksforGeeks”;
·       Using new keyword
String s = new String (“GeeksforGeeks”);
·       StringBuffer-StringBuffer is a peer class of String that provides much of the functionality of strings. String represents fixed-length, immutable character sequences while StringBuffer represents growable and writable character sequences.

Syntax:

StringBuffer s = new StringBuffer (“GeeksforGeeks”);

·       StringBuilder -The StringBuilder in Java represents a mutable sequence of characters. Since the String Class in Java creates and immutable sequence of characters, the StringBuilder class provides an alternate to String Class, as it creates a mutable sequence of characters.

Syntax:

String Builder str=new StringBuilder();
Str.append(“GFG”);

·       StringTokenizer -StringTokenizer class in Java is used to break a string into tokens.

                  

                 Example:

 
java string pdf
java string ()





A StringTokenizer object internally maintains a current position   within the string to be tokenized. Some operations advance this current position past the characters processed.
A token is returned by taking a substring of the string that was used to create the StringTokenizer object
.
·       StringJoiner-StringJoiner is a class in java.util package which is used to construct a sequence of characters(strings) separated by a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix. Though this can also be with the help of StringBuilder class to append delimiter after each string, StringJoiner provides an easy way to do that without much code to write.

       Syntax:

      public StringJoiner(CharSequence delimiter);

No comments:

Post a Comment