Difference Between "Text" and New String("Text")

What is the difference between text and new String(text)?

new String("text");
explicitly creates a new and referentially distinct instance of a String object; String s = "text"; may reuse an instance from the string constant pool if one is available.

You very rarely would ever want to use the new String(anotherString) constructor. From the API:

String(String original) : Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string. Unless an explicit copy of original is needed, use of this constructor is unnecessary since strings are immutable.

Related questions

  • Java Strings: “String s = new String(”silly“);”
  • Strings are objects in Java, so why don’t we use ‘new’ to create them?

What referential distinction means

Examine the following snippet:

    String s1 = "foobar";
String s2 = "foobar";

System.out.println(s1 == s2); // true

s2 = new String("foobar");
System.out.println(s1 == s2); // false
System.out.println(s1.equals(s2)); // true

== on two reference types is a reference identity comparison. Two objects that are equals are not necessarily ==. It is usually wrong to use == on reference types; most of the time equals need to be used instead.

Nonetheless, if for whatever reason you need to create two equals but not == string, you can use the new String(anotherString) constructor. It needs to be said again, however, that this is very peculiar, and is rarely the intention.

References

  • JLS 15.21.3 Reference Equality Operators == and !=
  • class Object - boolean Object(equals)

Related issues

  • Java String.equals versus ==
  • How do I compare strings in Java?

What is the difference between Text and String?

In the context of JavaFX, Text is a class in the javafx.scene.text package. The Text class defines a node that displays a text. Paragraphs are separated by '\n' and the text is wrapped on paragraph boundaries. In other words, Text is a component of a scene graph.

String, while also a class, is a fundamental data type in the Java language. String is in the java.lang package which is implicitly imported by the Java Compiler. The String class represents character strings. All string literals in Java, such as "abc", are implemented as instances of this class.

Similarities

  • Both Text and String instances can contain "text" as a sequence of characters.

Differences

  • String values are immutable in that you cannot change the value once you assign it but Text instances are mutable and can change by calling setText() method.

Methods that take a Text or String argument cannot be used interchangeably without converting to the appropriate class.

Text text = new Text("foo");
// convert Text to String
String s = text.getText(); // value = "foo"
// convert String to Text
text.setText(s); // or text = new Text(s) // value = "foo"

What is the difference between ABC and new String(ABC)?

String

In Java String is a special object and allows you to create a new String without necessarily doing new String("ABC"). However String s = "ABC" and String s = new String("ABC") is not the same operation.

From the javadoc for new String(String original):

Initializes a newly created String object so that it represents the
same sequence of characters as the argument; [...]

Unless an explicit copy of original is needed, use of this constructor
is unnecessary since Strings are immutable.

In other words doing String s = new String("ABC") creates a new instance of String, while String s = "ABC" reuse, if available, an instance of the String Constant Pool.

String Constant Pool

The String Constant Pool is where the collection of references to String objects are placed.

String s = "prasad" creates a new reference only if there isn't another one available. You can easily see that by using the == operator.

String s = "prasad";
String s2 = "prasad";

System.out.println(s == s2); // true

Sample Image

Image taken from thejavageek.com.


new String("prasad") always create a new reference, in other words s and s2 from the example below will have the same value but won't be the same object.

String s = "prasad";
String s2 = new String("prasad");

System.out.println(s == s2); // false

Sample Image

Image taken from thejavageek.com.

What's the difference between return (string expr) and return New String(string expr)?

There is no difference in real.

as both of your function has return type String, creating a new String() is just a overhead. it like wrapping a string to again to a string and create a new string in pool which in real has no advantage.

But one major difference in primitive type and String object is that class String always create new string.

String str = "my string";

if "my string" already exists in String pool. then it will use the same string instead of creating new.

This is the reason why when,

String str1= "my string";
String str2 ="my string";

str1==str2? --> will return true

The result of above will be true, because same String object from pool is used.

but when you do,

String str = new String("new string");

Always, a new String object is created, irrespective of a same one already exists in pool or not.

so comparing:

String str1 = new String("new string");
String str2 = new String("new string");

str1==str2 --> will return false

What is the difference between types String and string?

Here is an example that shows the differences, which will help with the explanation.

var s1 = new String("Avoid newing things where possible");
var s2 = "A string, in TypeScript of type 'string'";
var s3: string;

String is the JavaScript String type, which you could use to create new strings. Nobody does this as in JavaScript the literals are considered better, so s2 in the example above creates a new string without the use of the new keyword and without explicitly using the String object.

string is the TypeScript string type, which you can use to type variables, parameters and return values.

Additional notes...

Currently (Feb 2013) Both s1 and s2 are valid JavaScript. s3 is valid TypeScript.

Use of String. You probably never need to use it, string literals are universally accepted as being the correct way to initialise a string. In JavaScript, it is also considered better to use object literals and array literals too:

var arr = []; // not var arr = new Array();
var obj = {}; // not var obj = new Object();

If you really had a penchant for the string, you could use it in TypeScript in one of two ways...

var str: String = new String("Hello world"); // Uses the JavaScript String object
var str: string = String("Hello World"); // Uses the TypeScript string type

Difference between string and text in rails?

The difference relies in how the symbol is converted into its respective column type in query language.

with MySQL :string is mapped to VARCHAR(255)

  • https://edgeguides.rubyonrails.org/active_record_migrations.html
:string |                   VARCHAR                | :limit => 1 to 255 (default = 255)  
:text | TINYTEXT, TEXT, MEDIUMTEXT, or LONGTEXT2 | :limit => 1 to 4294967296 (default = 65536)

Reference:

https://hub.packtpub.com/working-rails-activerecord-migrations-models-scaffolding-and-database-completion/

When should each be used?

As a general rule of thumb, use :string for short text input (username, email, password, titles, etc.) and use :text for longer expected input such as descriptions, comment content, etc.



Related Topics



Leave a reply



Submit