Found 9326 Articles for Object Oriented Programming

Constructors of StringBuilder class in Java.

Manikanth Mani
Updated on 30-Jul-2019 22:30:21

237 Views

The StringBuilder class of the java.lang package is a mutable sequence of characters. This provides an API compatible with StringBuffer, but with no guarantee of synchronization. Following are the list of constructors provided by the StringBuilder class.S.N.Constructor & Description1StringBuilder()This constructs a string builder with no characters in it and an initial capacity of 16 characters.2StringBuilder(CharSequence seq)This constructs a string builder that contains the same characters as the specified CharSequence.3StringBuilder(int capacity)This constructs a string builder with no characters in it and an initial capacity specified by the capacity argument.4StringBuilder(String str)This constructs a string builder initialized to the contents of the specified string. ... Read More

Java String intern() method example.

Arjun Thakur
Updated on 26-Feb-2020 08:08:51

118 Views

The intern() method of the String class returns a canonical representation for the string object. It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.ExampleLive Demoimport java.io.*; public class Test {    public static void main(String args[]) {       String Str1 = new String("Welcome to Tutorialspoint.com");       String Str2 = new String("WELCOME TO SUTORIALSPOINT.COM");       System.out.print("Canonical representation:" );       System.out.println(Str1.intern());       System.out.print("Canonical representation:" );       System.out.println(Str2.intern());    } }OutputCanonical representation: Welcome to Tutorialspoint.com Canonical representation: WELCOME TO ... Read More

How to convert int to String in java?

Manikanth Mani
Updated on 30-Jul-2019 22:30:21

261 Views

You can convert a String value to an integer either using the valueOf() method of the String class or using the toString() method of the Integer class.

What does intern() method in java?

Ayyan
Updated on 30-Jul-2019 22:30:21

215 Views

The intern() method of the String method returns a canonical representation for the string object. A pool of strings, initially empty, is maintained privately by the class String.For any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.All literal strings and string-valued constant expressions are interned.Example Live Demoimport java.lang.*; public class StringDemo {    public static void main(String[] args) {       String str1 = "This is TutorialsPoint";       // returns canonical representation for the string object       String str2 = str1.intern();             // prints ... Read More

Methods of StringBuffer class in Java.

Manikanth Mani
Updated on 30-Jul-2019 22:30:21

255 Views

Following are the various constructors provided by the StringBuffer class.S.N.Constructor & Description1StringBuffer()This constructs a string buffer with no characters in it and an initial capacity of 16 characters.2StringBuffer(CharSequence seq)This constructs a string buffer that contains the same characters as the specified CharSequence.3StringBuffer(int capacity)This constructs a string buffer with no characters in it and the specified initial capacity.4StringBuffer(String str)This constructs a string buffer initialized to the contents of the specified string.

What is the difference between StringBuffer and StringBuilder in java?

Manikanth Mani
Updated on 30-Jul-2019 22:30:21

257 Views

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 is that StringBuilder’s methods do not thread safe (not synchronized).It is recommended to use StringBuilder whenever possible because it is faster than StringBuffer. However, if the thread safety is necessary, the best option is StringBuffer objects. Read More

Write a java program reverse tOGGLE each word in the string?

Manikanth Mani
Updated on 30-Jul-2019 22:30:21

536 Views

To perform a reverse toggle split the words of a string, reverse each word using the split() method, change the first letter of each word to lower case and remaining letters to upper case.Example Live Demoimport java.lang.StringBuffer; public class ToggleReverse {    public static void main(String args[]){       String sample = "Hello How are you";       String[] words = sample.split(" ");       String result = "";       for(String word:words){          StringBuffer s = new StringBuffer(word);          word = s.reverse().toString();          String firstSub = word.substring(0, 1);          String secondSub = word.substring(1);          result = result+firstSub.toLowerCase()+secondSub.toUpperCase()+" ";       }       System.out.println(result);    } }OutputoLLEH wOH eRA uOY

Constructors of StringBuffer class in Java.

Akshaya Akki
Updated on 30-Jul-2019 22:30:21

953 Views

Following are the various constructors provided by the StringBuffer class.S.N.Constructor & Description1StringBuffer()This constructs a string buffer with no characters in it and an initial capacity of 16 characters.2StringBuffer(CharSequence seq)This constructs a string buffer that contains the same characters as the specified CharSequence.3StringBuffer(int capacity)This constructs a string buffer with no characters in it and the specified initial capacity.4StringBuffer(String str)This constructs a string buffer initialized to the contents of the specified string.

Check if a string contains a number using Java.

Anjana
Updated on 06-Sep-2023 14:07:32

38K+ Views

To find whether a given string contains a number, convert it to a character array and find whether each character in the array is a digit using the isDigit() method of the Character class.ExampleLive Demopublic class ContainsExample {    public static void main(String args[]){       String sample = "krishna64";       char[] chars = sample.toCharArray();       StringBuilder sb = new StringBuilder();       for(char c : chars){          if(Character.isDigit(c)){             sb.append(c);          }       }       System.out.println(sb);    } }Output64

Write a java program to tOGGLE each word in string?

Alankritha Ammu
Updated on 26-Feb-2020 07:07:50

655 Views

You can change the cases of the letters of a word using toUpperCase() and toLowerCase() methods.Split each word in the string using the split() method, change the first letter of each word to lower case and remaining letters to upper case.ExampleLive Demopublic class Sample{    public static void main(String args[]){       String sample = "Hello How are you";       String[] words = sample.split(" ");       String result = "";       for(String word:words){          String firstSub = word.substring(0, 1);          String secondSub = word.substring(1);          result = result+firstSub.toLowerCase()+secondSub.toUpperCase()+" ";       }       System.out.println(result);    } }OutputhELLO hOW aRE yOU

Advertisements