elibraryportal Logo

What is String in Java

→ String is a sequence of characters, in double quotation mark.

→ String an object in java.

→ String is an array of characters .

Example

How to create string object:

→ There are 2 ways to create String object

  1. String Literal
  2. new keyword

1) String Literal

→ String Literal contain "double quotes" String Value.

Example

Output:

String Literal

2) new keyword

→ When we can create String object using new keyword for that allocates memory for the object. . String class has many constructors to create String objects using different sources, such as string literal, an array of characters etc.

Example

Output

java
strings
example

String Method in Java

There are some use full Method are used in this chapter that is.

  • String length()
  • String compareTo()
  • String concat()
  • String IsEmpty()
  • String Trim()
  • String toLowerCase()
  • String toUpper()
  • String ValueOf()

1) Java String length() :-

→ length() Method returns count of total number of characters present in the String.

Example:

Output:

Java String length()

2) Java String compareTo() :-

→ compareTo() method compares the given string with current string. It either returns positive number or negative number or 0.

Example:

Output:

Java String CompareToExample()

3) Java String concat() :-

→ concat() method is used to add two or more string into a single string object.or You can say append of another string.

Example:

Output:

Java String ConcatExample()

4) Java String IsEmpty() :-

→ IsEmpty() method checks string is empty or not. If the java String is Empty, it returns true otherwise else returns false.

Example:

Output:

Java String IsEmpty()

5) Java String Trim() :-

→ Trim() Method Remove whitespace from both sides of a string:

Example:

Output:

Java String Trim()

6) Java String toLowerCase() :-

→ toLowerCase() method converts all the characters of the String in lower case letter.

Example:

Output:

Java String toLowerCas()

7) Java String toUpper() :-

→ toUpper() converts all the characters of the String in Upper case letter.

Example:

Output:

Java String toUpper()

8) Java String ValueOf() :-

→ ValueOf() Method converts different types of values into string such as long to string, boolean to string, character to string, float to string, double to string, object to string and char array to string etc.

String valueOf() method in Java

  1. valueOf(boolean b) :- Returns the boolean argument.
  2. valueOf(char c) :- Returns the char argument
  3. valueOf(char[] data) :- Returns the char array argument.
  4. valueOf(char[] data, int offset, int count) :- Returns the string a specific sub array of the char array argument.
  5. valueOf(double d) :- Returns the of the double argument.
  6. valueOf(float f) :- Returns the float argument.
  7. valueOf(int i) :- Returns the int argument.
  8. valueOf(long l) :- Returns the long argument.
  9. valueOf(Object obj) :- Returns the Object argument.

Example:

Output:

Java String ValueOf()