Found 10102 Articles for Object Oriented Programming

What is the purpose of toString() method? Or What does the toString() method do in java?

George John
Updated on 19-Feb-2020 12:49:21


The toString() method returns the current object in String format.ExampleLive Demopublic class Test { public static void main(String args[]) { Test obj = new Test(); System.out.println(obj.toString()); System.out.println("Hello"); } }OutputTest@2a139a55 Hello

How do I create a Java string from the contents of a file?

Updated on 19-Feb-2020 12:48:47


You can read contents of a file using the read() method of the class FileInputStream. To this method you need to pass a byte array as a parameter to which the contents of the file are to be read.To convert a byte array to Sting simply pass the byte array as a parameter to the constructor of the String class.Exampleimport; import; public class Demo {    public static void main(String args[]) throws Exception {             File file = new File("data.txt");       FileInputStream fis = new FileInputStream(file);       byte[] bytesArray ... Read More

What is the difference between a String object and a String literal in Java?

Paul Richard
Updated on 30-Jul-2019 22:30:20


When the String literal used to create String, JVM initially checks weather String with the same value in the String constant pool, if available it creates another reference to it else it creates a new object and stores it in the String constant pool.In case of an object, each time you instantiate the class a new object is created given value irrespective of the contents of the String constant pool.

What is the difference between a StringBuffer and StringBuilder in java?

Vikyath Ram
Updated on 25-Feb-2020 12:25:18


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 are 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

What is the difference between String s1 = "Hello" and String s1= new String("Hello") in java?

Rishi Raj
Updated on 19-Feb-2020 12:48:04

2K+ Views

When you store a String asString str1 = "Hello";directly, then JVM creates a String object with the given value in a separate block of memory known as String constant pool.And whenever we try to create another String asString str2 = "Hello";JVM verifies whether any String object with the same value exists in the String constant pool, if so, instead of creating a new object JVM assigns the reference of the existing object to the new variable.And when we store String asString str = new String("Hello");using the new keyword, a new object with the given value is created irrespective of the ... Read More

How many ways a String object can be created in java?

George John
Updated on 30-Jul-2019 22:30:20


You can create a String by − Step 1 − Assigning a string value wrapped in " " to a String type variable. String message = "Hello Welcome to Tutorialspoint"; Step 2 − Creating an object of the String class using the new keyword by passing the string value as a parameter of its constructor. String message = new String ("Hello Welcome to Tutorialspoint"); Step 3 − Passing a character array to the String constructor. char arr[] = {'H','e','l','l','o'}; String message = new String(arr);

What is the meaning of immutable in term of String in java?

Updated on 30-Jul-2019 22:30:20


In Java, immutable objects are those whose data can’t be changed or modified (once modified). String class is immutable i.e. once we create a String object its data cannot be modified.

How to convert a String into int in Java?

Updated on 19-Feb-2020 12:45:00


The Integer wrapper class of the java.lang package provides a method named parseInt() using this method you can convert a String variable to an integer variable.ExampleLive Demopublic class Test {    public static void main(String args[]) {       String data = "123";       int num = Integer.parseInt(data);       System.out.println(data);    } }Output123

Which package is used for pattern matching with regular expressions in java?

Paul Richard
Updated on 30-Jul-2019 22:30:20


Java provides the java.util.regex package for pattern matching with regular expressions.

How to split a string in Java?

Vikyath Ram
Updated on 16-Jun-2020 11:58:57


Java provides a split() methodology you'll be able to split the string around matches of the given regular expression.The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string. If the expression doesn't match any a part of the input then the ensuing array has the only 1part, specifically this string.ExampleLive Demoimport java.lang.*; public class StringDemo {    public static void main(String[] args) {       String str = "a d, m, i.n";       String ... Read More