String 8 « string « Java Data Type Q&A





1. Strings in java.    coderanch.com

From the Java Virtual Machine Specification (2nd edition, paragraph 5.1): The Java virtual machine maintains a per-type constant pool (3.5.5), a runtime data structure that serves many of the purposes of the symbol table of a conventional programming language implementation. The constant_pool table (4.4) in the binary representation of a class or interface is used to construct the runtime constant pool ...

2. getBytes() in String    coderanch.com

The byte[] will contain character codes for the characters in the String, using some default encoding which will vary from country to country. For many installations, you will indeed effectively get the ASCII codes for the characters 1, 2, 3, 4, and 5: 49, 50, 51, 52, 53. If you want the characters themselves, use toCharArray().

3. Strings in Java    coderanch.com

In a standalone local application there will only be one Hello world in the VM (at least in a Sun VM, other implementations may vary). Both objects will be referencing the same memory space, as a way of work saving mechanism. To verify this you can run a If (one == two) Test. This will give you true because they ...

4. String problem    coderanch.com

5. getting last piece of string    coderanch.com

split() returns a String array. You want the last element of the array. Of course, if you just want to get the file name from a path, you should create a File object and then call its getName() method. This will be more o/s independent as you will not need to worry what the path separator is.

6. Switch-statement and Strings    coderanch.com

7. spliting a string    coderanch.com

8. a simple problem regarding strings    coderanch.com

9. working with Strings    coderanch.com

Java doesn't use ASCII, it uses Unicode; but for your purposes, Unicode is a superset of ASCII. In Java, as in C and many other similar language, a character is an integral type; it is it's own "ASCII code". You can just cast a char to an int, although even that isn't needed most of the time: int asciiCode = (int) ...





10. string swapping    coderanch.com

11. regarding strings    coderanch.com

12. strings    coderanch.com

13. contentEquals in java.lang.String    coderanch.com

14. about String class    coderanch.com

15. String limitations    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. - ...

16. question regarding strings    coderanch.com





17. String Problem    coderanch.com

The reason is that + has higher precedence than ==, so the String concatenation is performed first, and then the resulting String is compared with == to str2. Also I would expect that when you fix the String concatenation problem, you won't get a false in the second statement. [ August 13, 2006: Message edited by: Keith Lynn ]

18. String argument giving two different outputs ?    coderanch.com

Dear all, I have written a program which gives two different ouputs for two different String Arguments. When String... is passed value is different as compared to String args why ? Please explain me. Beta.java class Alpha { public void foo(String... args) { System.out.print("Alpha:foo "); } public void bar(String a) { System.out.print("Alpha:bar "); } } public class Beta extends Alpha { ...

19. string help stuff...    coderanch.com

http://www.programmersheaven.com/c/MsgBoard/grouplist.asp?Setting=A0001F2001 has a bunch of great C boards, along with lots of other subjects. I think it is the best all around programming board. The Java boards are pretty dead, but the boards labeled C/C++ are quite active and the regulars are not only nice, but extremely knowledgable in CS topics. It sounds like you are in need of a good ...

20. Initcap of a string    coderanch.com

21. Understanding String as an Object    coderanch.com

It's not true to say that a String is like a primitive in any way, shape or form. Saying String aString; gives you an uninitialized variable that points to nothing, just as Object anObject; does. Until you assign an object to the variable, it can't be used for anything. Now, there is one single thing that's special about the String class: ...

22. Getting string from a string    coderanch.com

Hi Cosmos, are you sure you want to get a String from a String? Your example looks more like you want to get an int from a String. In this case, you can also use a scanner. Scanners can get all number formats (ints, doubles, ...) from a String that's gonna be tokenized. Default delimiter is a whitespace. See example: [B]import ...

23. getting an object from a given string    coderanch.com

24. String - IsNumeric()?    coderanch.com

Note that the original question allowed for floating-point as well. Ignoring the possibility of exponential notation, this suggests a more elaborate regex like: public boolean isNumeric(String s) { return s.matches("[+-]?(?:\\d+(?:\\.\\d*)?|\\.\\d+)"); } Unfortunately that's a bit involved for the beginner forum. So, Lambert: if you are familiar with regular expressions, or are sufficiently motiviated to learn more about them, I recommend something ...

25. String Clustering ??    coderanch.com

"String clustering"? I've never heard of that. It is not a common Java term. Searching on Google for "java string clustering" also doesn't find anything. Java does something called string pooling, which is mainly meant as a memory optimization. Is that maybe what you mean? It works like this: When you use the same string literal more than once in your ...

26. Using Strings we can't Modify the content,Explain it    coderanch.com

Hi Sirisha, try this: String s = "Hello"; s.substring (0,1); System.out.println(s); It will print "Hello", not only "H". Because the String object made in the first line and reference by s is "Hello" and will never change. If the second line was s = s.substring (0,1); the object from the first line also would not be changed. Instead a new string ...

27. How can I make this string ok?    coderanch.com

28. string manipuation    coderanch.com

Hi All I am receiving a String content from a method which is a chunk of xml. The xml is something like this blahblah other tags Or i can recive some thing like this blahblah other tags The input xml i recieve will vary dynamically. What i want to do is interspere a fe xml tags just ...

29. breaking the string    coderanch.com

30. string problem    coderanch.com

But " String ".trim() is not a String literal. You call a method on a String literal and it returns a new String object, of which the value is "String" (without the leading and trailing white space). As to why == normally does work with string literals, that's a more complicated story. It is because of an optimization that the Java ...

31. Control for String    coderanch.com

The first thing to consider is what constitutes valid input for your field? Would any of these be invalid? Garrett Garrett M Rowe Garrett M. Rowe Mr. Rowe Garrett "the Animal" Rowe gmr1960@myplace.org GMR Once you have settled on a set of rules, you need to code a method or a class that can take an input String and send you ...

32. Initializing method local String object.    coderanch.com

Remember, variables are REFERENCES to objects. Variables are not objects themselves. Setting secondName to null means that it is a reference to no object. No methods may be executed using that reference (you'll get NullPointerException if you try). Setting secondName to "" means that it is a reference to a zero-length String object. Any method of the String class may be ...

33. String preservation    coderanch.com

To be precise, the String itself is not bold or underlined or anything, thats just how it is currently displayed in whatever editor you're using. A String at it heart is an array of chars, it has no concept of being bold or underlined, its just text. If you want bold or underlined text, you'll have to markup your String using ...

34. Strings are poor substitutes for capabilities    coderanch.com

I am reading the book Effective Java, written by Joshua Bloch. Now, I'm with Item 32: Avoid strings where other types are more appropriate. I can't understand the last point: Strings are poor substitutes for capabilities. I copied the part that I dun understand: ---------------------------------------------------------------------- Occasionally, strings are used to grant access to some functionality. For example, consider the design of ...

35. string regular    coderanch.com

As the principal engineer of an HTTP web server, you are responsible for implementing the request processing subsystem of the server. An incoming request for a specific resource, identified by an URI, must be dispatched to the appropriate handler according to the server configuration which maps URIs to request handlers. 'HandlerFactory.getHandler' must be implemented: public class HandlerFactory { public String getHandler(String[] ...

37. string find/search code in java    coderanch.com

38. String object creation    coderanch.com

39. No. of string instances    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. - ...

40. string stuff    coderanch.com

Originally posted by Ruchee Goyal: Hi, I've a string which is being populated at runtime from database with OS (operating system) values say AIX 5.2, Linux 2.4 etc. Now i need to take the version thing(5.2, 2.4 etc) out and just return the version free OS string to the user. what can be the best way to do that?

41. leading zero in String    coderanch.com

42. question about strings    coderanch.com

In the general case, it's simply no longer referenced by the variable, and may be eligible for garbage collection if nothing else references it. In the specific case you've shown, though, it's a String literal, which the JVM keeps in a special "String pool", so that it won't be collected. If the String was created in some other way, though, it ...

43. Strings    coderanch.com

Hi When I use the following code, String s="cow"; String c="cow"; String s1=new String("cow"); String s2=new String("cow"); System.out.println(s1==s2);//false System.out.println(s==s1);//false System.out.println(s==c);//true System.out.println(s.hashCode());//1000911 System.out.println(s1.hashCode());//1000911 System.out.println(s2.hashCode());//1000911 System.out.println(c.hashCode());//1000911 As per my understanding, if we use ==, the control will check for references but not for objects. But why is that in the above example, s1==s2 is false, eventhough they have the same hashCode? Strings are ...

44. Strings doubt    coderanch.com

I think it probably prints what you first said, though I am not sure and not interested enough to work it out. What we can say for sure is that it will always produce the same result on every Java compiler and JVM. The language spec carefully defines operator precedence, expression evaluation etc. Although the answer to the question will be ...

45. string processing    coderanch.com

One alternative along the same lines as what you're thinking is to reverse the String and read up to the first "/". This will separate out the file name and the path to the file. Then just reverse the two resulting Strings to get them back into usable form. The best way is probably to just use Java's File object to ...

47. String    coderanch.com

well!! about the difference i can say is in the first statement String s="hell"; s is a variable of type string which is given value "hell". But in the second statement String s=new String("hello"); here s is pointing to the reference of class String and it is instantiated using the keyword "new" (on the right hand side). now If you use ...

48. Externalizable Strings    coderanch.com

I think Anupam Sinha is referring to the java.io.Externalizable interface which is not implemented by the String class. The Externalizable interface does extend Serializable which is implemented by String. See the Javadoc for these two interfaces to understand the difference. However, I believe your problem is specific to your IDE (WSAD). I use RAD 6.0 which is essentially WSAD. If you ...

49. string class    coderanch.com

The preceding construct is equivalent to, but more efficient than, this one, which ends up creating two identical strings: String s = new String("Hola Mundo"); //don't do this this is one of line which i have read from sun.java material here two identical strings means it will create two string objects and also they have commented like dont do this .... ...

50. Strings    coderanch.com

I had to read twice for understanding your problem The == checks if the reference variable refers to the same object and not to the same objecttype. For that you can use the instanceof operator. The following code has the behavior that you want import static java.lang.System.out; public class Test { public static void main(String... args){ String s1 = new String("abc"); ...

51. Simpe question on string    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. - ...

52. Related to String    coderanch.com

Originally posted by manishkumarlal cs: whats the different between String s = "abc"; and String s = new String("abc"); String is a very special class in JAVA. String s=new String("abc"): jvm create a String Object contained "abc" then return a refrence pointed to s. SO s a refrence not a real object. String s = "abc": jvm first use String.equals to ...

53. string editor    coderanch.com

i've just skimmed the requirements, but i don't think you're actually building a GUI anything - the GUI stuff is already in the class file you are given. You will implement the MyList class as a singly linked list. Then, you will write your StringEditor class, that will have two of your MyList objects as members. Each of those lists will ...

54. methods in string objects    coderanch.com

55. < and > in java string    coderanch.com

56. Replicating a string?    coderanch.com

Is it possible to replicate a string by itself (or any other integer)? In Perl you can use the x operator to multiply the left handstring by the right hadn one, eg $a = b x c;. I'm still working on some code for which I'm trying to do this but I'm really unsure as to whether I've set it up ...

57. 6GB data as String    coderanch.com

Many 32-bit Java implementations limit the Java heap to under 2GB, and so to even get that much data in memory, you'd need a 64-bit machine and a 64-bit JVM. Unfortunately, even then, because String is implemented using a char[], and Java array indices are ints, I believe Integer.MAX_VALUE is the greatest number of characters a String can store -- i.e., ...

58. Help with returning one string from three methods of type string?    coderanch.com

"ideal" was probably not the best choice of words by me. One thing you'll learn is that there is never and ideal solution that applies to all possible scenarios. Every solution has tradeoffs that have to be weighed and balanced. For example, there are some situations where a bubble sort, which can be horribly slow, is actually the best solution, where ...

59. Object and String    coderanch.com

I presume there was some sort of agreement made when Java was new that String objects would be treated differently. String objects are immutable. Once a String is set up it cannot be changed, only replaced with a different String. The compiler and JVM reuse Strings; if you write "hello" twice, they will recognise a previously-used String and reuse it. So, ...

60. Question on String    coderanch.com

1) Question; 11. public String makinStrings() { 12. String s = Fred; 13. s = s + 47; 14. s = s.substring(2, 5); 15. s = s.toUpperCase(); 16. return s.toString(); 17. } How many String objects will be created when this method is invoked? A. 1 B. 2 C. 3 D. 4 E. 5 F. 6 When we invoke substring() method ...

61. why string    coderanch.com

Howdy Manazirrrrrhasan Khanrrrr , Welcome to JavaRanch Great question you have started off with. My guess goes this way. As such Strings are the basic and main way the inputs are dealt across and considering in terms of the total number of objects being created in heap, they might have done that way. When the strings are immutable, only the references ...

62. String problem    coderanch.com

In your code example, there are two words are found: " String " & "String" found in the string pool because you are using the "==" you are comparing the two locations, thus the output is "not equal." declaring two variable with the same value. Only one "String" is found in the string pool, thus only one location/reference. public class Test1 ...

63. String Doubt    coderanch.com

64. string question    coderanch.com

65. simple String question    coderanch.com

66. Books, the main(String[] method), non-object oriented programming, and beginners.    coderanch.com

First, I must apologise. About two weeks ago I commented about code which seemed to have everything in the main method, and suggested that was typical of the examples in Head First Java (HFJ), and offended Bert Bates, which I hadn't intended to do. I am very sorry. What is more, I was mistaken about the book; the sort of example ...

67. how totake user input for the string for this program ?    coderanch.com

import java.util.*; public class StringReverseWord { private static void doStringReverseWord() { String a = " Rohit Khariwal Mohit Parnami"; Stack stack = new Stack(); // this statement will break the string into the words which are separated by space. StringTokenizer tempStringTokenizer = new StringTokenizer(a); // push all the words to the stack one by one while (tempStringTokenizer.hasMoreTokens()) { stack.push(tempStringTokenizer.nextElement()); } System.out.println("\nOriginal ...

68. How String tokeinzer is advantegous then Strings utility method    coderanch.com

If you have never used either, you should do it both ways. Just to learn how to do it. Normally you should judge this task on time it takes to implement it and time it may take to maintain the code you've created. Only in rare cases will the execution time (or execution size) of the code you create actually become ...

69. String Separation    coderanch.com

70. Strings contained in strings    coderanch.com

What's the best way to go about checking if a String being searched for is contained within another String (ignoring case). ex) Cat is contained in Catastraphy I searched the string class online but found no functions that might help me do this. Maybe just using indexOf() with a string parameter, because that way if I don't get back an index ...

71. String reconstruction    coderanch.com

[Nick]: StringBuffer class has ways of doing what you want. String class is resistant to chop it into 3 pieces Not really. It's pretty easy to create three pieces using the substring() method, with no StringBuffers anywhere. The real problem here is the stuff Jess has been asking about: how can we reconstruct the original string using only 2 of the ...

72. ADDING TWO STRING    coderanch.com

73. Returning a string from a method    coderanch.com

Hi, I keep getting an error (cannot resolve symbol) when compiling my code. How can I get my method to return the concatonated string strAbsentData ? import java.io.PrintStream; import java.sql.*; import java.lang.*; public class Absense { public Absense() { } public String GetAbsense(String dbname, String user, String password, String telno) { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch(ClassNotFoundException _ex) { System.out.println("Unable to load ...

74. Sending any string via cmd. code given    coderanch.com

public class argss { public static void main(String[] args) { for (int i=0; i < args.length; i++) { System.out.println("Hello " + args[i]); } } } I tried running the application but I didn't get any output ?? How to send any string via Command Prompt ?? Also how to send input in a program. Like "system.out.print" outputs on the console , ...

75. modify the arrray to string    coderanch.com

76. all combination of string    coderanch.com

That won't work. that will give you every permutation of every letter, but only ones that are two characters long. The original post at LEAST also needed one-character strings. Also, if the input string had "abcdeabc", generally with combinations/permutations, you don't want duplicates. Finally, please remember we try to not just post a solution. That doesn't help the original person figure ...

77. String Query    coderanch.com

78. string    coderanch.com

79. String Tweaking    coderanch.com

80. generating string    coderanch.com

81. String correction    coderanch.com

Welcome to the Ranch, both of you. Drop Bear? Is that a real name? But whatever his name, he is probably correct. If you really have a String of concatenated numbers like that, then a linear search is probably the easiest way to insert it. You will probably have to use the methods of the Integer class like toString(int i) to ...

82. Question on String objects    coderanch.com

There are 5 String objects in that method. "Fred" and "47" are created when the class is loaded. When you run the class you create 3: "Fred47" substring(2,5) "ed4" and that to upper case, "ED4". String s = "Fred"; does not create a new object; it simply adds a name (s) to it.

83. need to delimit string to get into parts    coderanch.com

I've currently setup the code try { File file = new File("c:/file.csv"); FileReader reader = new FileReader(file); BufferedReader in = new BufferedReader(reader); String string; while ((string = in.readLine()) != null) { System.out.println(string); } in.close(); } catch (IOException e) { e.printStackTrace(); } and it reads in the text file fine, but I need to be able to delimit my input into two ...

84. About String usage    coderanch.com

Parsing XML files can be slow. Its unlikely it is your use of these String variables that is the cause is though. It is also unlikely that memory is the constraint. If it were, you might start to see OutOfMemoryErrors. My guess is the bottleneck is the CPU. There is a general rule for Swing applications, do not do lengthy non-GUI ...

85. getting string    coderanch.com

Hey guys, I've ran into a point where I can't seem to get it work correctly and was hoping you guys could point me in the right direction. I want to get the String delay from the run() function. I've declared it in the checklocation class earlier but then it won't get changed. public class checklocation extends Thread { // Passes ...

86. The well known String str = "abc" and String str1 = new String("abc");    coderanch.com

String str = "abc" and String str1 = new String("abc"); What happens in the first case > String str = "abc" ? Is that getting allocated to the String constant pool ! What happens in the Second case > String str1 = new String("abc"); The String str1 = abc is allocated in the String constant pool. Then str1 is copied to ...

88. String s1="abc" & String s2= new String("abc")    coderanch.com

Having read this question in one of the post here,i tried looking for the solution myself and referred K&B & Effective Java & came to a conclusion but i m still not sure. What i understood is : class A { . . String s1="abc"; String s2=new String "abc"; . . } In the first statement one object & one reference(s1) ...

89. please provide the explanation regarding string objects............    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. - ...

90. How do I covert on object into string    coderanch.com

The main intention behind overriding Object#toString() , method in your class is to print some meaningful data , explaining your class , eg, This may be the implementation of your Ann class class Ann { int a; . // other instance varible definations, . String strEnplaningAnnClass; // this string objects holds the data //that you want to display , when objects ...

91. doubt in String    coderanch.com

92. String operations    coderanch.com

I have this condition statement. if(firstval != secondval ) then(result1) else if(secondValue == null) then(result2) else(result3) I want to store the sub parts of each condition in separate strings. For eg. in the above string, For the first if Condition, I need the sub parts to be saved in Strings as follows -> leftCondition = "firstval" operator = "!=" rightCondition = ...

93. String    coderanch.com

For numerous reasons. Some of those: - it's about the only thing that can be printed to the console or most user interface controls. Even primitive types such as int are converted to Strings - because they are immutable they are perfect as keys for Maps - it just makes sense to store (fixed) alphanumeric information in Strings Now about the ...

94. string handling    coderanch.com

95. Creating our own String class?    coderanch.com

It might be possible to create a class with immutable objects but in java strings are stored in String constant pool if you may have heard of it how are you going to create such a thing A static map. The only thing you wouldn't be able to implement by creating a new class is the standard syntax for the assignment ...

96. String    coderanch.com

int i1 = 10; int i2 = i1; i2 = 20; //i2 changes but i1 doesn't change here String s1 = new String("Original"); String s2 = s1; s2 = s2.concat("1"); //s2 changes but s1 doesn't change here StringBuffer sB1 = new StringBuffer("Original"); StringBuffer sB2 = sB1; sB2.delete(2,5); //sB1 and sB2 both change here

97. String objects    coderanch.com

it is said that if we declare a atring object like String ab="abc"; then the jvm will first check the String constant pool for the existence of some object with value "abc",if it exists it will assign it to ab else a new object is created. but when we use new keyword to make a new object like String ab=new String("abc"); ...

98. String enhancements    coderanch.com

99. How does Stack and Heap work for Strings    coderanch.com

It doesn't. It effects how much optimization the compiler is able to do. When a and b are final, the compiler recognizes that a + b is a compile time constant, calculates its value and puts the result into the byte code. So, the difference between your two code examples is that the first executes the concatenation at runtime, whereas the ...

100. conditional String query    coderanch.com

is that REALLY your code? why do you use "equals" for the first condition, and "==" for the second? also, what is 'N'? is that a variable? if so, what is the value of 'N'? Even if it is a reference to a string, odds are you don't want "==" there... also, and I'm not sure of this, but if you ...