Showing posts with label Java string literals. Show all posts
Showing posts with label Java string literals. Show all posts

Java string literals

Strings
Java string literals look the same as those in C/C++, but Java strings are real objects, not pointers to memory. Java strings may or may not be null-terminated. Every string literal such as "a string literal" is interpreted by the Java compiler as new String("a string literal")
Java strings are constant in length and content.

For variable-length strings, use StringBuffer objects. Strings may be concatenated by using the plus operator:

String s = "one" + "two"; // s == "onetwo"

You may concatenate any object to a string. You use the toString() method to convert objects to a String, and primitive types are converted by the compiler.

For example,
String s = "1+1=" + 2; // s == "1+1=2"
The length of a string may be obtained with String method length(); e.g., "abc".length() has the value 3.
To convert an int to a String, use:

String s = String.valueOf(4);

To convert a String to an int, use:

int a = Integer.parseInt("4");

Read more...

  © Free Blogger Templates Modic Template by For more Information : click Here

Back to TOP