Literal « string « Java Data Type Q&A





1. Why is String the only class for which literals exist in Java?    stackoverflow.com

Why is java.lang.String the only class for which two ways of creation exist: 1) With normal way with "new" keyword.

String s = new String("abc");
2) With String literal (which is only available ...

2. Is there a jflex specification of java string literals somewhere?    stackoverflow.com

And by string literals I mean those containing \123-like characters too. I've written something but I don't know if it's perfect:

<STRING> {
  \"         ...

3. How would you convert a String to a Java string literal?    stackoverflow.com

This is sort of the Java analogue of this question about C#. Suppose I have a String object which I want to represent in code and I want to produce a ...

4. What is the right position of literals in String Comparison?    stackoverflow.com

I have

if (localName.equals("TaxName")) {
but PMD says
Position literals first in String comparisons

5. difference between string object and string literal    stackoverflow.com

what is difference between

String str = new String("abc");
and
String str = "abc";

6. what is the advantage of string object as compared to string literal    stackoverflow.com

i want to know where to use string object(in which scenario in my java code). ok i understood the diff btwn string literal and string object, but i want to know ...

7. Howto unescape a Java string literal in Java    stackoverflow.com

I'm processing some Java source code using Java. I'm extracting the string literals and feed them to a function taking a String. The problem is that I need to pass the ...

8. How do I concatenate a String literal and a String variable in Java?    stackoverflow.com

String date = new java.text.SimpleDateFormat("MM-dd-yyyy").format(new java.util.date());
upload.uploadfile("192.168.0.210", "muruganp", "vm4snk", "/home/media/Desktop/FTP Upload/+date+"_RB.zip"", "/fileserver/filesbackup/Emac/+date+"_RB.zip"");
uploadfile is a function which uploads the file 10-20-2010_RB.zip to the server location. But since I do have the string "date" in ...

9. Understading an example of Strings Literal Pool    stackoverflow.com

According to this article ScjpTipLine-StringsLiterally I was coding some examples like :

public static void main(String[] args) {
   String literalString1 = "someString";
   String literalString2 = "someString";
 ...





10. Java: Using identical String literals instead of a final variable?    stackoverflow.com

I've come across a class that includes multiple uses of a string literal, "foo". What I'd like to know, is what are the benefits and impact (in terms of object creation, memory ...

11. Java string literal automatically interpreted as String object, but how?    stackoverflow.com

What are the mechanics behind Java automatically interpreting string literals as String objects, since there are no overloaded operators and there is no default support for low-level string buffers (not including ...

12. diffrence between addition of String Literals and String objects    stackoverflow.com

What is the difference between an addition of String Literal and String Object? jdk = 1.6.22 For example

    String s1 ="hello";
    String s2 ="hello1";
    ...

13. Where to get "UTF-8" string literal in Java?    stackoverflow.com

I'm trying to use a constant instead of a string literal in this piece of code:

new InputStreamReader(new FileInputStream(file), "UTF-8")
"UTF-8" appears in the code rather often, and would be much better to ...

14. converting string to string literal    stackoverflow.com

Possible Duplicate:
How would you convert a String to a Java string literal?
Is there a library function that takes a string and converts is into ...

15. What's the difference between a string and a literal?    stackoverflow.com

What is the type of "I like Comp Sci!"? I'm pretty sure its either a string or a literal, can anyone point out the difference between the two and help me ...

16. Java String literal pool and string object    stackoverflow.com

I have known that JVM maintains a string literal pool to increase performance and maintain JVM memory and learned that string literal is maintained in the string pool. But I want ...





17. Literal string creation vs String object creation    stackoverflow.com

How many String object are created I am studying for the SCJP I cant seem to get my head round this String problem. I seem to see several possible answers depending on ...

18. New to java; String variable as a literal argument.    bytes.com

So basically the user picks a category and your program picks a word from that category. Suppose those categories are numbered 0, 1, 2, ... n-1 where you have 'n' categories. ...

20. A strange unicode String literal problem    coderanch.com

1. If I print out the following unicode in a servlet-- String s = "\u1f26\u0323\u1f82"; out.println(s); The unicode Greek characters are printed perfectly in HTML. 2. But when I get the unicode String (u1f26\u0323\u1f82) from elsewhere, that is, I DO NOT initialize String s with the literal string as above, out.println statement produces, on the HTML page, the literal string -- ...

21. String literal objects in String pool    coderanch.com

Hi, public class Test{ public static void main(String args){ String str ="abc"; String str1 = "ab"; str1 = str1.concat("c"); if(str.equals(str1){ System.out.println("In Equals method"); } if(str == str1){ System.out.println("In == method"); } } } I couldn't understand why it's printing only "In Equals method". "As per java API if compiler encounters a String literal , it checks the pool to see if ...

22. String & Literal Pool Doubt    coderanch.com

I have few basic doubts in String & its memory pool. With String s=new String("abc"); Two objects with value abc ,are created.One in prgram space referenced by s.& One goes in literal pool. 1. Is this String in literal pool remains lost or unreferenced. 2. And if program furthue uses s ,value abc comes from program space? 3. Is this literal ...

23. Bubble Sorting - String Literal    coderanch.com

Thanks David for your suggestion but i feel some difficulty to implement your suggestion. In the meanwhile, I made the above program using the compareTo method using String type Arrays. But the same thing i want to do with String literal without arrays means a para of text which sort out the text in ascending order. I think this would required ...

24. String literals    coderanch.com

25. String literals are String objects!    coderanch.com

26. String literals    coderanch.com

Ernest, thanks for pointing me to the resources. But this brings another question in my mind. If for example I have a class A that invokes some methods in class B. class A{ public boolean insertDataInTable(String s){ B b = new B(); boolean result = b.insertData(s); return result; } class B{ public boolean insertData(String s){ String sql = "insert in table"; ...

27. String literal vs String object    coderanch.com

28. what is string an object or literal ?    coderanch.com

hi.. can any body tell me what is String an object or literal ? i mean what is the difference when we declare string as String a = new String("abc text"); String b= "xyz text"; ??? and are call to functions in java are "call by refernce" or "call by value" i mean if i pass say someobject.someMethod("abc_text"); someobject.someMethod(a); // a ...

29. String Literals    coderanch.com

class Q15 { public static void main(String[] args) { System.out.print("\nab");// due to '\n' character it goes to new line and on new line prints 'ab' System.out.print("\bsi");// due to '\b' character, character 'b' will be deleted and 'si' will be appened so output become 'asi' System.out.print("\rha");// due to '\r' character, it starts printing from first character and overwrites previous output so 'ha' ...

30. String Literals    coderanch.com

31. unclosed string literal error    coderanch.com

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - ...

32. Difference between String Literal and String Object    coderanch.com

Hi Can any body give me the difference between the String Literals and String Objects and how the JVM implements both. I came across the following piece of code. public class Stringcomparison { public static void main(String args[]) { String ss1=new String("Rajarsi"); String ss2=new String("Rajarsi"); String s1="Rajarsi"; String s2="Rajarsi"; System.out.println(" == comparison for StringObjects: "+(ss1==ss2)); System.out.println(" == comparison for StringLiterals: "+(s1==s2)); ...

33. how compiler determines that String literal's length ??    coderanch.com

Java or any compiler scans each character in the source file. When it encounters a double quote character it flags the the start of a string and when it is another double quote character it encounters the end of the string. Carefully read and run the following code for an example: public class ParseStrings { public static void main(String args[]) { ...

34. Length of String literal    coderanch.com

35. Singleton class, class loader, string literals    coderanch.com

A singleton is a single instance of a class per JVM per class loader. Please google for Java singleton design pattern for example. Classes are introduced into the Java environment when they are referenced by name in a class that is already running. There is a bit of magic that goes on to get the first class running (which is why ...

36. Strings and literal pool    coderanch.com

Ernest, I don't think Strings are that simple of a subject depending on how deep you want to get. When does a String come from the constant pool? Does the JVM use a StringBuilder when concatanating two Strings? Does calling the new method create a new String or not if it's already in the pool? etc... there are a lot of ...

37. Position literals first in String comparisons-    coderanch.com

Position literals first in String comparisons - that way if the String is null you won't get a NullPointerException, it'll just return false. As it is a very good approach- Position literals first in String comparisons - that way if the String is null you won't get a NullPointerException, it'll just return false. public class NullCheckEqual { public static void main(String[] ...

38. String literal and String object comparison    coderanch.com

Hi, Can i know the reason for the String literal assignment . String str1 = new String("abc"); String str="abc"; System.out.println(str==str1); System.out.println("Str memory-->"+str.hashCode()); System.out.println("Str1 memeory-->"+str1.hashCode()); output: false Str memory-->96354 Str1 memeory-->96354 Even though a new String object is created both of them have same address still str1=str gives false. This is confusing .

39. Regarding the functioning of string literals...    coderanch.com

Hi, I happened to read the journal by Corey written in 2004 - http://www.javaranch.com/journal/200409/Journal200409.jsp#a1 Was happy to know few things... though... Why is it required for String objects to have references from two locations, when they are created as string literals? For eg... String a = "Ram"; "Ram" object is referred from the local variable table and from the string literal ...

40. String literals not eligible for GC?    coderanch.com

The big problem with intern is once you intern a String, you are stuck with it in RAM until the program ends. It is no longer eligible for garbage collection, even if there are no more references to it. If you want a temporary interned String, you might consider interning manually. However, in the most recent JVMs, the interned string cache ...

42. Will the string literal be garbage collected before JVM close?    coderanch.com

Hi All, I understand string concepts to some extent. But, now I have a different question. When we create a String like String a = "Sample"; The String object "Sample" is available in the heap and a reference is created in the string pool as well apart from the variable reference. Now when a code like this follows the above snippet... ...

43. When to use new String("") against string literal    coderanch.com

1. When you need to create string as a ThreadLocal. 2. When a string is doesnt have to be living throughout the life of JVM. If you have a code that creates too many strings (in a loop for eg) and this is code is run highly infrequently in application life time, it is perhaps preferable to create strings on heap ...

44. string literal is not properly closed by a double quote?    coderanch.com

Hello all, I'm trying to write a program that will allow users to type in a file path and return just the name of the file that they specified. I have the program written out, but every time I try to run it in Eclipse, it gives me the message, "string literal is not properly closed by a double quote." I ...

45. when to use string literal and String New Object ?    coderanch.com

Both have their own trade-offs: If you create a string using String literals , the string stays in the string-pool forever. It is never garbage collected. The down side of this is that the string-pool has a fixed size and it will get full at sometime. With 'new' operator new strings objects are created out of the string-pool, and they are ...

46. String Literal Pool internals    coderanch.com

47. String literal pool    coderanch.com

48. String literal in println    coderanch.com

Sudhanshu Mishra wrote:Thanks for the suggestion,but i would appreciate if you could be more direct. Do we get a new object or not? Thanks... While the direct answer is quite simple to give, here at javaranch, every1 believes in pointing to the resource than reading it out as much as you can. It serves two purposes 1) It saves ...

49. Instantiating a String literal even before it's really needed.    coderanch.com

Hello: I'm facing the following situation: 1. I've a method which, from an user input, receives many String parameters which need to be parsed to numbers. 2. Inside of it, the method which I use to parse them throws a specific kind of exception if it fails. 3. If such exception is thrown, I need to catch it and set it ...

50. String object and string literal    coderanch.com

The JVM maintains a pool of Strings, so that frequently used values can be reused instead of being instantiated all over again. In the following code, the two string variables refer to the same String instance: String a = "music"; String b = "music"; Strings a and b are both literals. Their values are assigned explicitly rather than through the use ...

51. I can only print string literals    java-forums.org

I'm writing a class to make a random password, but for some reason I can only print string literals in the main method. Can anyone tell me why? Java Code: public class RandomPassword { /* main method, only used for testing methods */ public static void main(String[] args) { System.out.print(RandomPassword.fixLast("this_")); } /* returns random char alpha (caps or no caps) or ...

52. Regd the String literal    forums.oracle.com

53. Strange String literal issue - Newbie alert !!    forums.oracle.com

Hello all, Please forgive if this is dumb but I have a deadline to meet. I have a function func_name(int num, String ip_s1, String ip_s2) It works just fine if I do somaclass.func_name(123,"string1","string2"); but not for someclass.func_name(123,string_var,string_var2); even though the function "receives" the input correctly. I have no clue as to what to do. Please help !

54. String literal concatenation with +=    forums.oracle.com

Thanks for your answer. By knowing this, I have other questions regarding this issue. Would you be kind to answer it, thanks again! ProjectMoon wrote: When javac reads string concatenation like this, it replaces that code with calls that create a StringBuilder and use it to do the actual string concatenation. so... you meant just like what I assumed in the ...

55. match literal string    forums.oracle.com

I've got a little program that reads the input of a user, the input should look like "draw tree" for example. Now if the user gives "draw" as a command (the complete input is splitted on whitespace, the first wordt being the first element of an array), I'd like the program to execute a certain part of the code. However, the ...

56. How many java String objects are created in string literal pool by executin    forums.oracle.com

Thank you for your reply. 1. String str = "Java"; 2. str = str.concat(" Beans "); 3. str = str.trim(); 4. String str1 = "abc"; 5. String str2 = new String("abc").intern(); But one of my colleague suggested that 5 java String objected will be created in the String Literal Pool "Java" // line1 " Beans " // line2 "Java Beans " ...

57. Unterminated string literal    forums.oracle.com

58. Literal strings in regexps    forums.oracle.com

59. String object vs String literal    forums.oracle.com

I understand how the string objects are created using new operator and using literal.I also understand how they are stored in memory.All I want to know is that when and why should I create a String object using new and when I should create an object using literal.Is there any specific reason for creating an object using new operator when objects ...

60. small clarification in String literals    forums.oracle.com

61. Literal Strings - same value, same object?    forums.oracle.com

I've read that if two literal Strings contain the same value, they reference the same object to save memory. Is this guaranteed? And is the same thing true of primitive wrappers with literal values like Integer(42)? The reason I ask is because I'm thinking of using a Map as a sparse array indexed by Integers... or, if not that, then Strings ...

62. numeric literal from string    forums.oracle.com

Hi, I have a string which contains the name of the customer, followed by phone number. There is no specific length for the name part and the tel number( I cannot use the substring method directly). The tel number is sometimes followed by address( cannot take the last 10 chars as the tel num either). I need to parse the string ...

63. Problem specifying \ in a string literal Using Matcher/Patteren class's    forums.oracle.com

import java.util.regex.Pattern; import java.util.regex.Matcher; public class regexTest { public static void main (String []args) { Pattern p = Pattern.compile("[a-zA-Z0-9- /]+"); Matcher m = p.matcher("123456-07//"); boolean b = m.matches(); String s1 = m.toString(); System.out.println(m.matches()); System.out.print(m.toString()); } } The program works fine and returns a Boolean true to the console indicating the match was successful for the following