Another string pool clarification
public static void main(String[] args) {
char[] diff = new char[]{'D','i','f','f','e','r'};
String s = new String(diff);
String t = new String(diff);
System.out.println(System.identityHashCode(s));
System.out.println(System.identityHashCode(s.intern()));
}Both the sysouts print different values. Doesnt this mean there is an already existing reference in the pool? Any idea how that came?
Yes you are right. But this was not for application coding purposes. So clarifying the intent.
public static void main(String[] args) {
String a = "hi";
String b = "How";
String c = "r";
String d = "u";
String abcd = a + b + c + d;
System.out.println(abcd);
char[] aA = new char[]{'h','i'};
char[] aB = new char[]{'H','o','w'};
char[] aC = new char[]{'r'};
char[] aD = new char[]{'u'};
char[] aABCD = new char[]{'h','i','H','o','w','r','u'};
System.out.println(a == new String(aA).intern()); //prints true
System.out.println(b == new String(aB).intern()); //prints true
System.out.println(c == new String(aC).intern()); //prints true
System.out.println(d == new String(aD).intern()); //prints true
System.out.println(abcd == new String(aABCD).intern()); //prints false
}Just wanted to be sure programatically how appending works exactly. Different forums and books are not so clear about it. Well the fact that string reference 'abcd' is not in the pool was something new to me (based on the assumption that at least that part of the javadoc for intern method is correct). But because of the change in spec for the intern method there is no way to check whether string "hiHow" is in the pool because we need a reference to it. But if the string abcd is not in the pool i guess "hiHow" will also not be.
Thats the whole thing. And a conclusion, intern method will not let you help check the pool whether a string is in it, if you already dont have the reference to the literal and this is because of the change in the spec while implementation. If the javadoc were correct you could have done like below to see if the string is not in the pool.
public static void main(String[] args) {
char[] aAB = new char[]{'h','i','H','o','w'};
String ab = new String(aAB);
if(ab != ab.intern()){
System.out.println("alerady in pool");
}else{
System.out.println("Only now in pool");
}But now that the method puts a copy in the pool, we never know.
Similar Messages
-
How to use one module pool program in another module pool programming?
Hi
I have one moodule pool program.
In one of my screen i want to use some code which already developed in another module pool program.
Is there any direct way for using one module pool programcode in another module pool program.
If there any approaches for this plz help me in this issue.Dear,
If you want to use the code.
first you can write your code in a subroutine in module pool program 1
and then you can use it from module pool program 2 by
perform subroutine_name(program_name)
using P_1
changing C_1
if you wan to use one module pool data into other module pool.
so that is another requirement. -
I would like to do a program that have one string control and one string indicator, any character that I type in the string control in the same time it will be appear in another string (indicator). How can help me?
Why not use an event
Add a While Loop, inside the loop add the Event Sructure.
Now in the event structure selecd the String Controls.value change event to
react
and the new value inside the event that you get,( connect to the String
indicator box.
On Sun, 10 Aug 2003 15:58:47 -0500 (CDT), WiltonFilho wrote:
> I would like to do a program that have one string control and one
> string indicator, any character that I type in the string control in
> the same time it will be appear in another string (indicator). How can
> help me?
>
> I would like to do a program that have one string control and one
> string indicator, any character that I type in the string control in
> the same time it will be appear in another string (indicator). How can
> help me?
>
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/ -
How to replace a particular striing with another string in jtextpane
how to get replace a particular string with another string of a jtextpane without taking the text in a variable using getText() method .
Thanks for your answer,
"relevant object" means for you datasources, transfer rules and transformations ?
With the grouping option "Save for system copy" I can't take easily all datasources, and others objects
Will I have to pick all object one by one ?
What would be the adjustement in table RSLOGSYSMAP ? For the moment it's empty.
Regards
Guillaume P. -
Using EJB from another connection pool in WLPI
Hi All,
I've deployed an entity bean (CMP) which used the connection pool other than the
one used by WLPI. When I execute the business operation I've defined for this
bean, the following exception is encountered:
"Couldn't get connection: java.sql.SQLException: Connection has already been created
in this tx context for pool named wlpiPool. Illegal attempt to create connection
from another pool: oraclePool"
Is there any other alternative for me to use the entity bean in WLPI?
Thanks,
FredInstead of using mulitiple connection pool,we are using single connection pool.
Our one application, accessing remote db also, for that we are using simple jdbc
connection. No pool.
We also tried OracleXAdriver. In that case, at the time of startup we are getting
weird error.
Thanks
JIgnesh Patel
"Fred" <[email protected]> wrote:
>
I simply turned all my CMP to BMP and get the connection from another
connection
pool. But that was not what I want. Is there any method that allows a
CMP to use
connection pool other than the wlpiPool pool in WLPI?
Thanks
"jignesh" <[email protected]> wrote:
Hi Fred,
We are also getting same error. How did you resolve this error ?
I'd appreciate,if you can post resolution of this error.
Thanks
Jignesh Patel
"Fred" <[email protected]> wrote:
Hi All,
I've deployed an entity bean (CMP) which used the connection pool other
than the
one used by WLPI. When I execute the business operation I've defined
for this
bean, the following exception is encountered:
"Couldn't get connection: java.sql.SQLException: Connection has already
been created
in this tx context for pool named wlpiPool. Illegal attempt to create
connection
from another pool: oraclePool"
Is there any other alternative for me to use the entity bean in WLPI?
Thanks,
Fred -
HI All,
When following assingments are done, then why two objects are created. According to string pool, only distinct objects exists for string in JVM. So == operator should return true. Till I was unaware of String Pool, it appeared right. But now I am a bit confused.
String s1="ab";
String s2="ab";
Kindly guide me on this. And point me to some good links about JVM, classloaders and String Pool. In books i am not able to find good coverage on these topics.J2EE_Life wrote:
Kindly guide me on this. And point me to some good links about JVM, classloaders and String Pool. In books i am not able to find good coverage on these topics.That is because you shouldn't really care, except maybe for classloaders especially when you want to get into web development. Worrying about JVM internals only leads to bad behavior, like premature optimization.
For information about classloaders, there are numerous decent articles that Google can lead you to. the javadocs are a good place to start:
http://docs.oracle.com/javase/6/docs/api/java/lang/ClassLoader.html -
Replacing a special character in a string with another string
Hi
I need to replace a special character in a string with another string.
Say there is a string - "abc's def's are alphabets"
and i need to replace all the ' (apostrophe) with &apos& ..which should look like as below
"abc&apos&s def&apos&s are alphabets" .
Kindly let me know how this requirement can be met.
Regards
SukumariREPLACE
Syntax Forms
Pattern-based replacement
1. REPLACE [{FIRST OCCURRENCE}|{ALL OCCURRENCES} OF]
pattern
IN [section_of] dobj WITH new
[IN {BYTE|CHARACTER} MODE]
[{RESPECTING|IGNORING} CASE]
[REPLACEMENT COUNT rcnt]
{ {[REPLACEMENT OFFSET roff]
[REPLACEMENT LENGTH rlen]}
| [RESULTS result_tab|result_wa] }.
Position-based replacement
2. REPLACE SECTION [OFFSET off] [LENGTH len] OF dobj WITH new
[IN {BYTE|CHARACTER} MODE].
Effect
This statement replaces characters or bytes of the variable dobj by characters or bytes of the data object new. Here, position-based and pattern-based replacement are possible.
When the replacement is executed, an interim result without a length limit is implicitly generated and the interim result is transferred to the data object dobj. If the length of the interim result is longer than the length of dobj, the data is cut off on the right in the case of data objects of fixed length. If the length of the interim result is shorter than the length of dobj, data objects of fixed length are filled to the right with blanks or hexadecimal zeroes. Data objects of variable length are adjusted. If data is cut off to the right when the interim result is assigned, sy-subrc is set to 2.
In the case of character string processing, the closing spaces are taken into account for data objects dobj of fixed length; they are not taken into account in the case of new.
System fields
sy-subrc Meaning
0 The specified section or subsequence was replaced by the content of new and the result is available in full in dobj.
2 The specified section or subsequence was replaced in dobj by the contents of new and the result of the replacement was cut off to the right.
4 The subsequence in sub_string was not found in dobj in the pattern-based search.
8 The data objects sub_string and new contain double-byte characters that cannot be interpreted.
Note
These forms of the statement REPLACE replace the following obsolete form:
REPLACE sub_string WITH
Syntax
REPLACE sub_string WITH new INTO dobj
[IN {BYTE|CHARACTER} MODE]
[LENGTH len].
Extras:
1. ... IN {BYTE|CHARACTER} MODE
2. ... LENGTH len
Effect
This statement searches through a byte string or character string dobj for the subsequence specified in sub_string and replaces the first byte or character string in dobj that matches sub_string with the contents of the data object new.
The memory areas of sub_string and new must not overlap, otherwise the result is undefined. If sub_string is an empty string, the point before the first character or byte of the search area is found and the content of new is inserted before the first character.
During character string processing, the closing blank is considered for data objects dobj, sub_string and new of type c, d, n or t.
System Fields
sy-subrc Meaning
0 The subsequence in sub_string was replaced in the target field dobj with the content of new.
4 The subsequence in sub_string could not be replaced in the target field dobj with the contents of new.
Note
This variant of the statement REPLACE will be replaced, beginning with Release 6.10, with a new variant.
Addition 1
... IN {BYTE|CHARACTER} MODE
Effect
The optional addition IN {BYTE|CHARACTER} MODE determines whether byte or character string processing will be executed. If the addition is not specified, character string processing is executed. Depending on the processing type, the data objects sub_string, new, and dobj must be byte or character type.
Addition 2
... LENGTH len
Effect
If the addition LENGTH is not specified, all the data objects involved are evaluated in their entire length. If the addition LENGTH is specified, only the first len bytes or characters of sub_string are used for the search. For len, a data object of the type i is expected.
If the length of the interim result is longer than the length of dobj, data objects of fixed length will be cut off to the right. If the length of the interim result is shorter than the length of dobj, data objects of fixed length are filled to the right with blanks or with hexadecimal 0. Data objects of variable length are adapted.
Example
After the replacements, text1 contains the complete content "I should know that you know", while text2 has the cut-off content "I should know that".
DATA: text1 TYPE string VALUE 'I know you know',
text2(18) TYPE c LENGTH 18 VALUE 'I know you know',
sub_string TYPE string VALUE 'know',
new TYPE string VALUE 'should know that'.
REPLACE sub_string WITH new INTO text1.
REPLACE sub_string WITH new INTO text2. -
I need to append a string to another string
I'm working with some inherited code, I'm a Colf Fusion
novice myself, and I'm trying to make this order form display the
correct data. The problem is a lot of data in the database is
missing. Description in the QStockDB query can contain a lot of
stuff. For our full color work the text "4/0", "4/BLACK", and "4/4"
are consistent so I'm changing the newitem (I know, not very
descriptive but it's not my code) to CMYK Printing.
<cfif #QStockDB.description# contains "4/0"><cfset
newitem = "CMYK Printing"></cfif>
<cfif #QStockDB.description# contains
"4/4/BLACK"><cfset newitem = "CMYK Printing"></cfif>
<cfif #QStockDB.description# contains "4/4"><cfset
newitem = "CMYK Printing"></cfif>
I want to then go back through description and compare it
more to add more description. For instance with this:
<cfif #QStockDB.description# contains "12 pt"><cfset
newitem = newitem + " - BC"></cfif>
I know that the job is a business card. So I want to append
the newitem variable with " - BC". Likewise:
<cfif #QStockDB.description# contains
"catalog"><cfset newitem = newitem + " - Catalog
Sheets"></cfif>
displays CMYK Printing - Catalog Sheets. Or, it should... or,
more precisely, I want it to. :)
How do I append a string to another string?+ is the addiion operator and works with numbers. Because
your string is a ...well, string... you need to use an ampersand.
Thus, instead of:
<cfif #QStockDB.description# contains
"catalog"><cfset newitem = newitem + " - Catalog
Sheets"></cfif>
Use...
<cfif #QStockDB.description# contains
"catalog"><cfset newitem = newitem & " - Catalog
Sheets"></cfif>
<cfoutput>#newitem#</cfoutput>
At least I think that will work - haven't tested it
though. -
Execute method to a string written in another string
Hi all,
I need help...!!
I must call a function/method on a string written in another string like this:
String s1 = "GR0000";
String s2 = "substring(0,s1.length()-2)";
I want execute
s1.substring(0,s1.length()-2)
from s1,s2, how can i do it?
Thank you in advice!!!Hi all,
I need help...!!
I must call a function/method on a string written in
another string like this:
String s1 = "GR0000";
String s2 = "substring(0,s1.length()-2)";
I want execute
s1.substring(0,s1.length()-2)
from s1,s2, how can i do it?
Thank you in advice!!!Advance.
I assume that you want the equivalent of a javascript eval() function ?
Seek for it not in java! What you want is a script interpreter. What you could do is wrap the string in a string representing a java class, write that string to disk as a java file, compile it, have the ClassLoader suck it up and execute it. Viola. The jsp-way; the hard way, but the only way.
If I'm wrong about your intentions, then tell me. I can't seem to figure it out totally. But good luck anyway. -
Hi there.
With regard to the string pool,
my book states that whenever the compiler
encounters a string literal that it will check the pool to see if
there is a matching string...
This doesn't seem right to me.
I would have imagined that the JVM would perform this operation at runtime.
Maybe I'm wrong, but I googled it and I came across some sites that indicate that
the JVM checks the string pool.
Im just wondering which is it, the JVM or the compiler.
Thanks & regards.jverd wrote:
Both.
String s1 = "abc";
String s2 = "abc";Compiler sees the first line, looks for "abc" in the class' constant pool, doesn't find it, adds it, places reference to its position for the assignment to s1.
Compiler sees the second line, looks for "abc", finds it, places reference to its position for the assignment to s2.
At runtime, when class is loaded, VM loads class' String constant pool. When time comes to execute the above lines, the bytecode tells it which constant pool entry to reference.Great answer (No, I'm not sarcastic) -
Why jvm maintains string pool only for string objects why not for other objects?
why jvm maintains string pool only for string objects why not for other objects? why there is no pool for other objects? what is the specialty of string?
rp0428 wrote:
You might be aware of the fact that String is an immutable object, which means string object once created cannot be manipulated or modified. If we are going for such operation then we will be creating a new string out of that operation.
It's a JVM design-time decision or rather better memory management. In programming it's quite a common case that we will define string with same values multiple times and having a pool to hold these data will be much efficient. Multiple references from program point/ refer to same object/ value.
Please refer these links
What is Java String Pool? | JournalDev
Why String is Immutable in Java ? | Javalobby
Sorry but you are spreading FALSE information. Also, that first article is WRONG - just as OP was wrong.
This is NO SUCH THING as a 'string pool' in Java. There is a CONSTANT pool and that pool can include STRING CONSTANTS.
It has NOTHING to do with immutability - it has to do with CONSTANTS.
Just because a string is immutable does NOT mean it is a CONSTANT. And just because two strings have the exact same sequence of characters does NOT mean they use values from the constant pool.
On the other hand class String offers the .intern() method to ensure that there is only one instance of class String for a certain sequence of characters, and the JVM calls it implicitly for literal strings and compile time string concatination results.
Chapter 3. Lexical Structure
In that sense the OPs question is valid, although the OP uses wrong wording.
And the question is: what makes class Strings special so that it offers interning while other basic types don't.
I don't know the answer.
But in my opinion this is because of the hybrid nature of strings.
In Java we have primitive types (int, float, double...) and Object types (Integer, Float, Double).
The primitive types are consessons to C developers. Without primitive types you could not write simple equiations or comparisons (a = 2+3; if (a==5) ...). [autoboxing has not been there from the beginning...]
The String class is different, almost something of both. You create String literals as you do with primitives (String a = "aString") and you can concatinate strings with the '+' operator. Nevertheless each string is an object.
It should be common knowledge that strings should not be compared with '==' but because of the interning functionality this works surprisingly often.
Since strings are so easy to create and each string is an object the lack ot the interning functionality would cause heavy memory consumption. Just look at your code how often you use the same string literal within your program.
The memory problem is less important for other object types. Either because you create less equal objects of them or the benefit of pointing to the same object is less (eg. because the memory foot print of the individual objects is almost the same as the memory footpint of the references to it needed anyway).
These are my personal thoughts.
Hope this helps.
bye
TPD -
Search given string array and replace with another string array using Regex
Hi All,
I want to search the given string array and replace with another string array using regex in java
for example,
String news = "If you wish to search for any of these characters, they must be preceded by the character to be interpreted"
String fromValue[] = {"you", "search", "for", "any"}
String toValue[] = {"me", "dont search", "never", "trip"}
so the string "you" needs to be converted to "me" i.e you --> me. Similarly
you --> me
search --> don't search
for --> never
any --> trip
I want a SINGLE Regular Expression with search and replaces and returns a SINGLE String after replacing all.
I don't like to iterate one by one and applying regex for each from and to value. Instead i want to iterate the array and form a SINGLE Regulare expression and use to replace the contents of the Entire String.
One Single regular expression which matches the pattern and solve the issue.
the output should be as:
If me wish to don't search never trip etc...,
Please help me to resolve this.
Thanks In Advance,
KathirAs stated, no, it can't be done. But that doesn't mean you have to make a separate pass over the input for each word you want to replace. You can employ a regex that matches any word, then use the lower-level Matcher methods to replace the word or not depending on what was matched. Here's an example: import java.util.*;
import java.util.regex.*;
public class Test
static final List<String> oldWords =
Arrays.asList("you", "search", "for", "any");
static final List<String> newWords =
Arrays.asList("me", "dont search", "never", "trip");
public static void main(String[] args) throws Exception
String str = "If you wish to search for any of these characters, "
+ "they must be preceded by the character to be interpreted";
System.out.println(doReplace(str));
public static String doReplace(String str)
Pattern p = Pattern.compile("\\b\\w+\\b");
Matcher m = p.matcher(str);
StringBuffer sb = new StringBuffer();
while (m.find())
int pos = oldWords.indexOf(m.group());
if (pos > -1)
m.appendReplacement(sb, "");
sb.append(newWords.get(pos));
m.appendTail(sb);
return sb.toString();
} This is just a demonstration of the technique; a real-world solution would require a more complicated regex, and I would probably use a Map instead of the two Lists (or arrays). -
How can I substitute a string with another string in a file
I have a file. I have a substitute a keyword with another string in all the occurences of a file. How can I do this?
I'm gonna give you the benifit of the doubt and assume you didn't mean to double post your question.
As to substitute a keyword with a string one way is to read in the file and run it through a StreamTokenizer class to break it into words. Pull one word at a time, check it, and stick it into a StringBuffer. Once your done with the file overwrite it with what is in the StringBuffer.
Another way might be to use the RandomAccessFile class, but I'm not really familiar with that because I hardly ever use it. -
Replace a string with another string in a file
I have to replace a string with another string. Say for example in a html file it got a line like,
<a href="##"></a>
now i want to replace this ## with the full derived path like
"c;\\sample folder\\sample subfolder\\samplefile.html"
can someone help me to do this?
pls tell me from the file opening till closing the file.I have to replace a string with another string. Say
for example in a html file it got a line like,
<a href="##"></a>
now i want to replace this ## with the full derived
path like
"c;\\sample folder\\samplesubfolder\\samplefile.html"
can someone help me to do this?
pls tell me from the file opening till closing the
file.
public class Buckel {
final static String CONST = "c:\\sample folder\\samplesubfolder\\samplefile.html";
public Buckel() {
public static void main ( String[] argv ) throws Exception {
int idx = 0;
String in = "<a href=\"##\"></a>", // We'll imagine this just gets here somehow. If it's on the i/p file then take the '\' out B4 and aft the '##'.
out = "";
idx = in.indexOf( "##" );
if ( idx > 0 ) {
out = in.substring( 0, idx ) + CONST + in.substring( idx+=2, in.length() );
System.out.println( out );
} -
Check if a string contains another string
can anybody help?
String st="DC";
how can i check if another string contains "DC" or not?How bout
if(someString.indexOf(st) != -1) {
//someString contains st at some point
}
Maybe you are looking for
-
How can i import .pmd files into Indesign CC, which where created with pagemaker?
Hello! i have a problem with a new Installation of indesign cc... i can*t import old .pmd Pagemaker files into it!! it was possible with Indesign cs6, but apparently no longer..!? ..is there an extension or anything else i could use? thanks in advanc
-
Asset Transfer Report with matching new asset number
Hi, During the past week, our company transferred over 2000 assets from several companies to one company. As a result of the transfer, the tax depreciation keys and tax depreciation life weren't carried over to the new assets. I am wondering if anyon
-
Static Identity Group Assignment
Does anyone know a way to bring in an endpoint with the following attributes? Endpoint Policy Name Static = True Static Group Assignment Static = True The 1.2 manual says; If the file used for import contains endpoints that
-
Problem in Views!!!!!
hi all of you...... i have a problem regarding views, if any one can help........Thanks. I have 2 view name Emp1 and Emp2 respectively....... I want to know, if any one performs DML operations (insert, update, delete) on Emp1 Emp2 should automaticall
-
When I right click on the source system FLATFILE, I don't see the 'Replicate datasources' option. Would this be a problem because I am getting an error on the transports: <i>No mapping defined for source system FLATFILE When transporting InfoPackages