Question:
how to write string in java program?
Punitha J
2012-02-07 06:09:15 UTC
Is it is possible to write two string in one class file in java
Three answers:
anonymous
2012-02-07 06:10:35 UTC
Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared. For example:





String str = "abc";



is equivalent to:





char data[] = {'a', 'b', 'c'};

String str = new String(data);



Here are some more examples of how strings can be used:





System.out.println("abc");

String cde = "cde";

System.out.println("abc" + cde);

String c = "abc".substring(2,3);

String d = cde.substring(1, 2);
llaffer
2012-02-07 06:11:11 UTC
If the class allows for two string attributes, yes.



If you're referring to a String object, then no. Each String object can only have one value.
deonejuan
2012-02-07 06:21:08 UTC
As of Java6 the new StringBuilder API is faster and offers the best methods of char[] and String. Java uses StringBuilder internally because it is faster. When you could write:



System.out.println( "The date is " + myDate + " in Los Angeles, CA.");



StringBuilder sb = new StringBuilder();

sb.append( "The date is ");

sb.append(myDate);

sb.append(" in Los Angeles, CA.");



System.out.print( sb.toString() );



this way, you avoid the 'Strings are immutable' feature in Java String.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...