What is the difference between a = a.trim() and a.trim()? - java

I've ran into a bit of a confusion.
I know that String objects are immutable. This means that if I call a method from the String class, like replace() then the original contents of the String are not altered. Instead, a new String is returned based on the original. However the same variable can be assigned new values.
Based on this theory, I always write a = a.trim() where a is a String. Everything was fine until my teacher told me that simply a.trim() can also be used. This messed up my theory.
I tested my theory along with my teacher's. I used the following code:
String a = " example ";
System.out.println(a);
a.trim(); //my teacher's code.
System.out.println(a);
a = " example ";
a = a.trim(); //my code.
System.out.println(a);
I got the following output:
example
example
example
When I pointed it out to my teacher, she said,
it's because I'm using a newer version of Java (jdk1.7) and a.trim()
works in the previous versions of Java.
Please tell me who has the correct theory, because I've absolutely no idea!

String is immutable in java. And trim() returns a new string so you have to get it back by assigning it.
String a = " example ";
System.out.println(a);
a.trim(); // String trimmed.
System.out.println(a);// still old string as it is declared.
a = " example ";
a = a.trim(); //got the returned string, now a is new String returned ny trim()
System.out.println(a);// new string
Edit:
she said that it's because I'm using a newer version of java (jdk1.7) and a.trim() works in the previous versions of java.
Please find a new java teacher. That's completely a false statement with no evidence.

Simply using "a.trim()" might trim it in memory (or a smart compiler will toss the expression entirely), but the result isn't stored unless you precede with assigning it to a variable like your "a=a.trim();"

String are immutable and any change to it will create a new string. You need to use the assignment in case you want to update the reference with the string returned from trim method. So this should be used:
a = a.trim()

You have to store string value in same or different variable if you want some operation (e.g trim)on string.
String a = " example ";
System.out.println(a);
a.trim(); //output new String is not stored in any variable
System.out.println(a); //This is not trimmed
a = " example ";
a = a.trim(); //output new String is stored in a variable
System.out.println(a); //As trimmed value stored in same a variable it will print "example"

Related

java I thought strings were immutable

I was always told strings in java are immutable, unless your going to use the string builder class or string writter class.
Take a look at this practice question I found online,
Given a string and a non-negative int n, return a larger string that is n copies of the original string.
stringTimes("Hi", 2) → "HiHi"
stringTimes("Hi", 3) → "HiHiHi"
stringTimes("Hi", 1) → "Hi"
and the solution came out to be
Solution:
public String stringTimes(String str, int n) {
String result = "";
for (int i=0; i<n; i++) {
result = result + str; // could use += here
}
return result;
}
As you see in the solution our 3rd line assigns the string , then we change it in our for loop. This makes no sense to me! (I answered the question in another nooby way ) Once I saw this solution I knew I had to ask you guys.
Thoughts? I know im not that great at programming but I haven't seen this type of example here before, so I thought I'd share.
The trick to understanding what's going on is the line below:
result = result + str;
or its equivalent
result += str;
Java compiler performs a trick on this syntax - behind the scene, it generates the following code:
result = result.concat(str);
Variable result participates in this expression twice - once as the original string on which concat method is called, and once as the target of an assignment. The assignment does not mutate the original string, it replaces the entire String object with a new immutable one provided by concat.
Perhaps it would be easier to see if we introduce an additional String variable into the code:
String temp = result.concat(str);
result = temp;
Once the first line has executed, you have two String objects - temp, which is "HiHi", and result, which is still "Hi". When the second line is executed, result gets replaced with temp, acquiring a new value of "HiHi".
If you use Eclipse, you could make a breakpoint and run it step by step. You will find the id (find it in "Variables" View) of "result" changed every time after java did
result = result + str;
On the other hand, if you use StringBuffer like
StringBuffer result = new StringBuffer("");
for(int i = 0; i < n; i++){
result.append(str);
}
the id of result will not change.
String objects are indeed immutable. result is not a String, it is a reference to a String object. In each iteration, a new String object is created and assigned to the same reference. The old object with no reference is eventually destroyed by a garbage collector. For a simple example like this, it is a possible solution. However, creating a new String object in each iteration in a real-world application is not a smart idea.

Removing/ignoring all the punctuation in a given string

I am trying to remove every punctuation from my string: So far, I've done the following :
String line = "there's, isn't";
line.replaceAll("\\p{Punct}", "")
yet, this can't change "there's" into "theres" or "isn't" into "isnt".
I thought \p{Punct} includes every punctuation, including " ' " (as it's shown in api) yet this doesn't work. Can someone pls help?
Thanks in advance.
PS: expected outcome is: theres isnt
Strings are immutable in Java. String methods don't change the string in place, but instead return a modified copy of the string, so you need to assign the result back to the variable:
String line = "there's, isn't";
line = line.replaceAll("\\p{Punct}", "");
System.out.println(line); // theres isnt
As the previous answer said, the strings are immutable and once created you can't really change it. So once you code like String line = "there's, isn't";the string object "there's, isn't" will be created and line variable will be assigned to it. Java doesn't have the concept of pointers, but you can think of it as line variable pointing to the given string "there's, isn't". Now your next line line.replaceAll("\\p{Punct}", "") returns a newly created string object. You can assign it to another variable, say line_cleared=line.replaceAll("\\p{Punct}", "" and print that out: So here is the fully updated code:
String line = "there's, isn't";
line_cleared= line.replaceAll("\\p{Punct}", "");
System.out.println(line_cleared);

How can I efficiently use StringBuilder?

In the past, I've always used printf to format printing to the console but the assignment I currently have (creating an invoice report) wants us to use StringBuilder, but I have no idea how to do so without simply using " " for every gap needed. For example... I'm supposed to print this out
Invoice Customer Salesperson Subtotal Fees Taxes Discount Total
INV001 Company Eccleston, Chris $ 2357.60 $ 40.00 $ 190.19 $ -282.91 $ 2304.88
But I don't know how to get everything to line up using the StringBuilder. Any advice?
StringBuilder aims to reduce the overhead associated with creating strings.
As you may or may not know, strings are immutable. What this means that something like
String a = "foo";
String b = "bar";
String c = a + b;
String d = c + c;
creates a new string for each line. If all we are concerned about is the final string d, the line with string c is wasting space because it creates a new String object when we don't need it.
String builder simply delays actually building the String object until you call .toString(). At that point, it converts an internal char[] to an actual string.
Let's take another example.
String foo() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 100; i++)
sb.append(i);
return sb.toString();
}
Here, we only create one string. StringBuilder will keep track of the chars you have added to your string in its internal char[] value. Note that value.length will generally be larger than the total chars you have added to your StringBuilder, but value might run out of room for what you're appending if the string you are building gets too big. When that happens, it'll resize, which just means replacing value with a larger char[], and copying over the old values to the new array, along with the chars of whatever you appended.
Finally, when you call sb.toString(), the StringBuilder will call a String constructor that takes an argument of a char[].
That means only one String object was created, and we only needed enough memory for our char[] and to resize it.
Compare with the following:
String foo() {
String toReturn = "";
for (int i = 0; i < 100; i++)
toReturn += "" + i;
toReturn;
}
Here, we have 101 string objects created (maybe more, I'm unsure). We only needed one though! This means that at every call, we're disposing the original string toReturn represented, and creating another string.
With a large string, especially, this is very expensive, because at every call you need to first acquire as much memory as the new string needs, and dispose of as much memory as the old string had. It's not a big deal when things are kept short, but when you're working with entire files this can easily become a problem.
In a nutshell: if you're working appending / removing information before finalizing an output: use a StringBuilder. If your strings are very short, I think it is OK to just concatenate normally for convenience, but this is up to you to define what "short" is.

Putting the return of String.split(delimiter) into a String[]

I did read the documentaion on String.split(delimiter) and it says that the return type is String[]. But I don't know how to take that return of String.split() and assign its elements to my receiving String[]. The simple-minded
String[] z = stuff.split(" ");
(where stuff is a String) does not work. It compiles fine, but the z ends up being some mumbo-jumbo with ampersands. Looks like toString of something, not like a String array. Please enlighten me. Thanks.
UPADTE: Sorry for asking this. Of course it works. I knew too little at the time and was overwhelmed.
Of course a String array contains a toString method witch is what was probably called ...
From the comments by the OP:
Sorry, I made a mistake in the original question. The corrected question is shown now. No, String[] z = stuff.split(" "); does not work. System.out.println(" z = "+z); gives z = [Ljava.lang.String;#18e2b22
You should try this to print out the String array correctly:
String[] z = stuff.split(" ");
System.out.println("z = " + Arrays.toString(z));
First, the length of the of the returned array cannot be stuff.length() but count of the stuff portions delimited by given delimiter. It doesn't matter anyway because your initialized array will be discarded after assignment of new value returned by the split function. Find your string portions in z[i] strings where i < z.length ( or z.size() ).

Weird Java String comparison

I'm having a minor issue with Java String comparisons.
I've written a class which takes in a String and parses it into a custom tree type. I've written a toString class which then converts this tree back to a String again. As part of my unit tests I'm just checking that the String generated by the toString method is the same as the String that was parsed in the first place.
Here is my simple test with a few printouts so that we can see whats going on.
final String exp1 = "(a|b)";
final String exp2 = "((a|b)|c)";
final Node tree1 = Reader.parseExpression2(exp1);
final Node tree2 = Reader.parseExpression2(exp2);
final String t1 = tree1.toString();
final String t2 = tree2.toString();
System.out.println(":" + exp1 + ":" + t1 + ":");
System.out.println(":" + exp2 + ":" + t2 + ":");
System.out.println(exp1.compareToIgnoreCase(t1));
System.out.println(exp2.compareToIgnoreCase(t2));
System.out.println(exp1.equals(t1));
System.out.println(exp2.equals(t2));
Has the following output; (NB ":" - are used as delineators so I can ensure theres no extra whitespace)
:(a|b):(a|b):
:((a|b)|c):((a|b)|c):
-1
-1
false
false
Based on manually comparing the strings exp1 and exp2 to t1 and t2 respectively, they are exactly the same. But for some reason Java is insisting they are different.
This isn't the obvious mistake of using == instead of .equals() but I'm stumped as to why two seemingly identical strings are different. Any help would be much appreciated :)
Does one of your strings have a null character within it? These might not be visible when you use System.out.println(...).
For example, consider this class:
public class StringComparison {
public static void main(String[] args) {
String s = "a|b";
String t = "a|b\0";
System.out.println(":" + s + ":" + t + ":");
System.out.println(s.equals(t));
}
}
When I ran this on Linux it gave me the following output:
:a|b:a|b:
false
(I also ran it on Windows, but the null character showed up as a space.)
Well, it certainly looks okay. What I would do would be to iterate over both strings using charAt to compare every single character with the equivalent in the other string. This will, at a minimum, hopefully tell you the offending character.
Also output everything else you can find out about both strings, such as the length.
It could be that one of the characters, while looking the same, may be some other Unicode doppelganger :-)
You may also want to capture that output and do a detailed binary dump on it, such as loading it up into gvim and using the hex conversion tool, or executing od -xcb (if available) on the captured output. There may be an obvious difference when you get down to the binary examination level.
I have some suggestions
Copy each output and paste in Notepad (or any similar editor), then
copy them again and do something like this
System.out.println("(a|b)".compareToIgnoreCase("(a|b)"));
Print out the integer representation of each character. If it is a weird unicode, the int representation will be different.
Also what version of JDK are you using?

Categories