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 ... |
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> {
\" ...
|
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 ... |
I have
if (localName.equals("TaxName")) {
but PMD says
Position literals first in String comparisons
|
what is difference between
String str = new String("abc");
and
String str = "abc";
|
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 ... |
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 ... |
|
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 ... |
According to this article ScjpTipLine-StringsLiterally I was coding some examples like :
public static void main(String[] args) {
String literalString1 = "someString";
String literalString2 = "someString";
...
|
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 ... |
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 ... |
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";
...
|
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 ... |
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 ... |
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 ... |
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 ... |
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 ... |
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. ... |
|
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 -- ... |
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 ... |
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 ... |
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 ... |
|
|
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"; ... |
|
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 ... |
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' ... |
|
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. - ... |
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)); ... |
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[]) { ... |
|
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 ... |
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 ... |
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[] ... |
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 . |
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 ... |
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 ... |
|
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... ... |
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 ... |
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 ... |
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 ... |
|
|
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 ... |
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 ... |
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 ... |
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 ... |
|
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 ! |
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 ... |
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 ... |
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 " ... |
|
|
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 ... |
|
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 ... |
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 ... |
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 |