Garrett OBrien

Greenhorn
+ Follow
since Oct 25, 2005
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Garrett OBrien

Originally posted by Henry Wong:
http://faq.javaranch.com/java/CallByReferenceVsCallByValue



When I first saw this link, I got very nervous, till I read it. This whole thing of "pass by reference" vs "pass by value" is annoying because people don't understand what the terms mean. As the link states, Java is only pass by value, there is no pass by reference, although, you can simulate it by putting your variables in a class.

Garrett
15 years ago
I'll through my thoughts and what I've done before into the mix here as well...


As far as static initializers, if I'm not mistaken, JDBC drivers use them to register with the DriverManager. This is where I've used them as well. Instead of having to explicitly call a method on a specific class to register with something, I can make a String[] holding the names of the classes I want to register (could be read in from a file), and do a Class.forName() on each one. It makes your code more dynamic, and somewhat easier because the user doesn't have to worry about calling the register method.

Non-static initializers are actually quite useful partly for one of the reasons mentioned above. If you put shared constructor code into a non-static initalizer, it will automatically be called right after the super() is called in the constructor. Yes, you can make a method, and call that in the constructor, but are you or anyone who will be modifying the code later going to remember to do that when you add a constructor? I find that forcing something is a lot of times better than trying to remember to do something. Also, setting up member variables is very nice in the non-static initializers (if the variable needs several lines to do this, or a try-catcy). Declare your variable, then right after, your non-static initializer to set it up. I find that it makes for cleaner code than putting the initializers in a seperate method, or in the constructor. Anyone coming back to the code can see exactly what is done to initialize the variable instead of going searching the code for it.

Garrett
15 years ago

Originally posted by Ilja Preuss:


Well, it's actually not that simple, as far as I know. If I'm not ways off, some constant expressions, such as

"This " + "text"
42*7

actually get "executed" at compile time - the byte code will only contain the value of the expression, not the expression itself.



You aren't way off, this is correct. When I first saw the post, this is what I was thinking of. To test it, you can modify the underlying fields of "This " + "text", and that would actually modify "This Text" as well. The compiler is smart enough to figure this type of thing out.

Garrett
15 years ago
I haven't done any speed tests, but when I need to do something like this, I assume it's much faster to do the conversion of case than regex's. I use regex's for pattern match, not case match.

Garrett
15 years ago
I read the article that Campbell Ritchie posted, and I'm in agreement, for the most part, however, I'm torn because at the end it said

"If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception"

I'm not sure if I totally agree. Most of the built-in unchecked exceptions are checkable by code, and the client can recover, so that doesn't agree with the Java core language. When I teach this to people, I usually say if an exception can be caught through logic (i.e. preventable), it should be an unchecked exception, if it can't (i.e. unpreventable), it should be a checked exception (e.g. opening a file for reading, since the program is generally on a multi-user platform, it's impossible to determine if the file is there to read, someone may have moved it while your program is running, however, a nullpointer, or divisionbyzero, both of them can be prevented through logic). This is in agreement with the Java core.

If there is a completely unrecoverable problem, it should be an Error, not an Exception.

Just my 2 cents

Garrett
15 years ago
Yes, that is possible (as you found out), and you would use an inner-inner class for the same reasons you would use just an inner class, however, I don't think I would ever do this because it looks fairly ugly, and anyone coming back to it would probably be confused.

Garrett
15 years ago
I don't think that Arrays are really that much faster than ArrayLists. If you look at the implementation of ArrayList, the underlying data is an array, so the only speed difference is a few minor method calls vs. direct access to the array, and if you size the ArrayList at the beginning, adding to the ArrayList won't really take any more time either, if you go over the initial capacity of the array, then it will need to create a new internal array and copy the first array values to the second array.

Just my 2 cents.

Garrett
18 years ago
I have written a completely useless program that changes a String, and I was wondering if it will work on other JVMs. I know that by accessing private variables, I'm asking for problems and I really shouldn't be doing it, but that's never stopped me before. I was just curious if the implementation of String is the same (or similar) across platforms/JVMs.

Thanks

Garrett


import java.lang.reflect.Field;

public class Empty
{
public static void main(String[] args)
{
System.out.println("");
}
static
{
long l = 0x20AC63C177C984l;
String s = "!";
while(l!=0)
{
s=(char)(l%32+((l%32!=0)?64:32))+s;
l/=32;
}
try
{
Field value = String.class.getDeclaredField("value");
Field count = String.class.getDeclaredField("count");
value.setAccessible(true);
count.setAccessible(true);
count.set("", new Integer(s.length()));
value.set("", s.toCharArray());
} catch (Exception e)
{
// don't care if there is an error
}
}
}
18 years ago