Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link

Ted Scofield

+ Follow
since Apr 04, 2009
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Ted Scofield

Probably because these questions are made up by people who learned their programming in the 70's-90's, who still haven't got their heads around the fact that, for many modern languages, these sorts of things simply aren't important.
If this was an interview for a perl job, a question like that would generate derisive laughter - and rightly so - so why do people think it's important for Java?

While I agree with you, I think it's still nice to know (at least in very broad terms) what happens under the hood.
Also, if you were hiring a Java developer, what would be the question that - if answered correctly - would make you think the candidate does know a thing or two about programming?
9 years ago
Hi Jeff,

a wonderful reply, thank you very much for your time/clear explanation, I really appreciate it.

And finally, while it's good to have a general idea of the heap/stack landscape, you'll almost never actually care about it when you're developing with Java. About the only time it would come into play is if you're actually writing your own JVM implementation, or if you're working on a specialized or limited-memory JVM, or if you're doing hardcore profiling for memory or performance problems.

To be honest, I'm preparing for an interview and it seems this question is pretty popular.

Again, thank you very much.
9 years ago
Hi all,

I'm trying to figure out if my understanding of stack/heap in Java is sound, I'd appreciate any feedback/correction/remark.

First, some theory: in general, JVM stores objects/arrays in a heap, although some implementations may use a stack (escape analysis). Local variables are put in a stack. When a method is invoked, its parameters and local variables are pushed in a stack.

Consider the following example:

What happens when we invoke the program? JVM starts a new thread and creates its heap and stack.

Main method is executed first; a new String array object is created and stored in the heap. 'args' parameter is pushed in the stack, pointing to the newly created array.

Then a new Greeter object is created and stored in the heap. If there's not enough memory, an OutOfMemoryError is thrown. 'elvis' reference is put in the stack, pointing to the newly created object. If there's not enough memory in the stack, a StackOverflowError is thrown.

sayHello is executed on the 'elvis'. A new String object is put in the String constant pool if it doesn't already exists and 'name' param points at it.

System.out is created or retrieved from heap and println method is triggered. A new String object is added to String constant pool in the heap - "Hello Elvis".

After the println method is done, "Hello Elvis" will no longer be referenced by any variable. This makes it eligible for garbage collection. When/if GC will be performed, this String object will be collected. The same goes for the Greeter object 'elvis' is pointing at.

I'm not sure for the last one though:

I assume the following: a new Greeter object is created on the heap and right away, the method sayHello is executed on it. The method does the same thing as I described above. When it finishes, the object becomes eligible for GC.

Would this be an okay explanation of Java memory management?
9 years ago
Rob, thanks for explaining it. Doing it the way it's been done a million times before, without unnecessary confusion (in this case, annotations) seems like a pretty cool idea.
11 years ago
Hi Hunter,

I've seen that, along with some more examples I found on Google, but I still don't get the idea of how to creates something as described above...
11 years ago
Hi all,

first off, I will try to demonstrate what I would like to achieve, since I think it will be much better than describing it. Consider the following interface:

Next, consider the following line of code

What I would like to achieve is that @Validate would check the String following the annotation by using the isValid method from the interface InputValidator and throw a compile time error if the result would be false. Since I'm pretty new to annotations, I'm not even sure if this is possible. So, is it possible and if so, could you please point me in the direction I could find more about how to create such an annotation?

Thank you.
11 years ago
Garrett, this is exactly what I was talking about. Now all I gotta do is figure out how to include Strings in the mappings, so that I can retrieve the Box I need. Thank you very much for showing me the example. Also, all other comments were very useful, so thanks to all of you.

If I get stuck again, should I continue the conversation in this thread, or make another one?

Thanks again guys.
11 years ago
What about some different approaches that would be remotely similar or analogue to this one? Since I've created a project with all the fancy generics and I'm almost done, I find it really difficult to accept the fact that I might be still at ground zero.
11 years ago
Hello all,

suppose I create the following method:

which I should invoke like so: if I want to get the Box holding Strings, or for the Box holding Integers. I have been playing around a bit, casting up and down, but so far, I have not found a solution, so I thought maybe you guys could help me out a bit?

Thank you.
11 years ago

Sebastian Janisch wrote:If the user input is 5.0, is it okay to display it as 5 ??

In that case just convert everyting to double, sort it and when displaying it as text again check if the number is a whole number and if that is the case strip the .0 to convert 5.0 back to 5

No. Unfortunately, I must use every String representation as is, meaning "5.0" can be displayed only as "5.0" or as a double value 5.0.
12 years ago
Hi guys,

I'm having some doubts/problems on how to manipulate a String containing numbers. My task is to sort the numbers given from a standard input
and then print them. All the "types" should remain the same, meaning a String representation of an int must remain an int (in other words, "1" must
not become "1.0").

Here is an example of a correct input:

where "+" means ascending order. The syntax of an expression is defined as: [ORDER SIGN]+[SPACE] followed by [NUMBER]+[,]+[SPACE]

I've come up with a solution I'm not particulary happy with. This is what I've done:

I should use Arrays.sort() method to sort the array. However, this makes sense only if the values
in the array are a String representation of a number. Thus I should check the String[] array:

If this turns out to be OK, the following method is invoked:

I'm looking for comments, improvements or a bit more elegant approach. Could you guys help me?


12 years ago
Hi all,

I'm trying to create a power supply unit simulation. In order to do so, I must use the following interface:

Ramping should be implemented concurrently, so that other interface methods (except loadRamp) are available at all times.

This is my solution:

I'm a bit worried with the following:

1) Is the design OK? Could it be more elegant?
2) Is there an elegant way of enabling methods only when the unit is turned on (and not using ifs)?
3) Is the startRamp method designed OK, or could this be done better?
4) Are exceptions in set method necessary or should I just ignore the method if the device is ramping or off or value < 0? What would you do?

If you have any other comments or tips/advices, I'd be very thankful.

Thank you,
12 years ago
Hello all,

I'm somewhat confused with reference type casting. I want to clear things out, so I'll try to explain myself how to cast properly. I hope you'll point out the mistakes.
First, let's consider this two classes:

Let's start with the easy part;

Animal is a superclass of a Dog class, therefore Dog is a subclass of an Animal class. Every subclass is a superclass object, thus class Dog is an Animal. So assigning a subclass reference to a superclass, like so:

works perfectly. However, a can refer only to superclass members, so the only thing we can call is an Animal getName() method. But since the getName() method in Dog overrides the method in Animal, the method returns the value defined in a Dog class. Example:

The output is "Class Dog".
As already said, a can refer only to superclass members so calling any member from a subclass causes a compile-time error.
Conclusions of assigning a subclass reference to a superclass variable:
- can refer only to superclass members,
- overriden methods in a subclass are invoked instead of the ones in a superclass.

Now for the second part;

Every Dog is an Animal, but not every Animal is a Dog. That's why Java does not allow statements like

They cause a compile time error. To avoid this error, one must explicilty cast the superclass reference to a subclass type, like so:

Unfortunatlely, this causes a run-time error, and I'm presuming this is because a superclass does not have an is a relationship with a subclass. The way to check if such a relationship exits is to use an "instanceof" operator. Example follows:

Conclusions of assigning a superclass refrence to a subclass variable:
- superclasses must become subclasses in order to be able to assign them to subclass variables,
- instanceof operator helps to determine if a variable can be cast to some arbitrary type T.

Could you please point out the mistakes I made and mention other important facts I may have forgot? Also, if you don't mind, any example where the superclass must be cast to subclass in order to work properly will be greatly appreciated.

12 years ago
Hi guys.

I'm a beginner in Java but I happen to have a copy of Block's Effective Java (1st edition). I'm slowly grasping through
sections I feel somewhat comfortable with, along with my main source, Deitel's How to Program.

I know the concepts presented are not meant for beginners, so I'm wondering how can a novice like me
benefit from them.

I just read Chapter 7, General Programming. The first topic is about the scope of local variables, and one of the
examples includes a for loop where the number of repetitions is calculated besides the control variable and then used
in the loop-continuation condition like so:

My question is: should I use start using the tips from the book right away, or should I wait to get a little better at Java first?

12 years ago
Hi Joanne,

the determinant of a matrix is only defined when the matrix is square, so the number of rows must match the number of columns.
I use instead of because the length of a matrix is calculated only once whereas in your case, the matrix length will be calculated in every iteration.

Thank you for you comment. Do you have any idea on how to set the GUI?

13 years ago