This week's giveaway is in the Spring forum.
We're giving away four copies of Microservices Testing (Live Project) and have Chris Love & Andres Sacco on-line!
See this thread for details.
Win a copy of Microservices Testing (Live Project) this week in the Spring forum!

Krep Lock

Ranch Hand
+ Follow
since Sep 19, 2006
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 Krep Lock



I must confess I've never seen an array declaration without a set of square brackets on the left side... I went and compiled it to remove my doubt.

Are there any subtle differences between the following?

12 years ago
Thanks a lot, people. I got it to work with the regular test program and a custom Rocket object I made for testing. I also realized I was making way too big a production of comparing list objects and tightened that up.
12 years ago
Thanks for all the input, everyone.

Rob Prime wrote:Comparable is generic too. The declaration should be That "? super T" is to ensure that T can also be java.sql.Timestamp which is a Comparable<java.util.Date>.

That should solve the warning, but the ClassCastException will still exist. Can you show us the relevant parts of MyListADT? Especially the related fields and its constructor, since that's where the exception is coming from.



Here is the relevent portion of MyListADT:



...and TestProg's attempt to instantiate a list:



Generics is new to me and what Istvan says about

makes sense to me, but I'm surprised it compiles at all if that is the problem.
12 years ago

Rauhl Roy wrote:

kemp may i know the answer to my question?



Q. Which example is better?

A. Example 1, which uses only String a.

Q. Why o why?

A. Example 1 uses less heap memory. By the time you get to the final line of example 1 you have the following string persisting on the heap:

"howareyou"

By the last line of example 2 there are many more strings persisting on the heap:

"how"
"are"
"you"
"howareyou"

All other things being equal, in a "big application" you are likely to tax system resources much quicker using example 2.
12 years ago

Janeice DelVecchio wrote:Rob -

I like your example.

In the first example, how is the "old a" ever able to be GC?
I was under the impression that string literals hung out in the pool forever..... of course I'm incredibly dense when it comes to GC and strings.....



String literals safely remain in the pool as long as one or more reference is pointing at them. So when the reference to "how" is overwritten in a with a reference to "howare" the older String's milliseconds are numbered.
12 years ago
Well, I confess I am working on homework, so using the library is not an option for this feature. The test program for the assignment uses a collection of Integers, so I could avoid a lot of trouble by comparing primitive values and get an 'A' but I'd like to figure out how to do it the "right" way and compare actual classes with a compareTo() implementation.

The class declaration reads:

So I am not extending (or implementing) Comparable with my collection class, I am just telling it to accept any class, T, that implements Comparable and substitute that for T within the class code. I'm not sure why it's "extends Comparable" though I expect it's for a similar reason that interfaces do not implement each other, but rather extend each other.

So Comparable is to be implemented in any class that this container will hold. I feel pretty close on this but the project is a train wreck all the same.
12 years ago
I'm not quite "getting" generic usage yet.

I have a custom container class and I'd like to pull out the smallest element from the list. Seems pretty straightforward, but I've been working on it for hours now. Here's my best try so far:



It compiles with a warning, then fails to execute:


E:\practice>javac -Xlint MyArrayList.java
MyArrayList.java:154: warning: [unchecked] unchecked method invocation: <T>smaller(T,T) in MyArrayList is applied to (T,T)
if(list[i] == smaller(list[smallest], list[i]))
^
1 warning

E:\practice>javac TestProg.java

E:\practice>java TestProg
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Comparable;
at MyListADT.<init>(MyListADT.java:39)
at MyArrayList.<init>(MyArrayList.java:12)
at TestProg.main(TestProg.java:11)



I think the problem is in the constructor, line 39 in MyListADT.java:
list = (T[]) new Object[maxSize];

I'm not sure how the syntax would work there for limiting T[] to only those classes that implement Comparable. Otoh, am I going about this right with trying to exclude non-comparables from my container class?

Thanks for any tips on this.
12 years ago
Wow, I had no idea.

Thanks for the answers.
13 years ago
I'm missing something on the following typical implementation of Cloneable's clone method:



Line 17 is my problem. The confusion is: why does the clone method get to directly set the bell instance variable of the copy even while it is private? Shouldn't it be forced to use a mutator method like everyone else? Is this a Cloneable-breaks-the-rules thing or am I missing something in plain sight?
13 years ago


This compiles and runs fine; both classes belong to the default package because they're in the same file.

If I uncomment the package echo; line, the code compiles fine, but prints the first line then returns a runtime exception of the NoClassDefFoundError variety. I guess I have a dynamic binding problem but I'm not clear enough on access rules to figure why it's happening and especially why it doesn't happen when the classes belong to the "default" package.
13 years ago
Fixed!

Thanks very much. I'm glad I posted here instead of throwing my hands in the air and quitting.
13 years ago
I've tried to install the JDK on my laptop and it's devolved into a horriffic timesink. I cannot get the simplest program to run:



F:\Java\StartingOut\chapter2>javac test.java
F:\Java\StartingOut\chapter2>java test
Exception in thread "main" java.lang.UnsupportedClassVersionError: test (Unsuppo
rted major.minor version 50.0)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)

F:\Java\StartingOut\chapter2>SET CLASSPATH=

F:\Java\StartingOut\chapter2>javac test.java

F:\Java\StartingOut\chapter2>java test
Exception in thread "main" java.lang.UnsupportedClassVersionError: test (Unsuppo
rted major.minor version 50.0)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)

F:\Java\StartingOut\chapter2>javac -cp . test.java

F:\Java\StartingOut\chapter2>java -cp . test
Exception in thread "main" java.lang.UnsupportedClassVersionError: test (Unsuppo
rted major.minor version 50.0)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)

F:\Java\StartingOut\chapter2>


...so it's not likely to be CLASSPATH. I've tried installing that latest JDK/JRE, but nothing resolves this problem.

It's been a long time since I've done anything with Java; I've been working with other languages. Am I missing something simple here? Or am I going to have to resort to brain surgery on my laptop to fix some obscure setting or registry before this will work?
13 years ago
I got it working... it looks like just showing up at the java ranch gave me that couple extra IQ points to solve this riddle.

/usr/bin/java was the default jre installation, which apparently is not ompatible to the jdk that I installed. Easily fixed by:

#mv /usr/bin/java /usr/bin/java.old
#ln -s /jdk.../bin/java /usr/bin/java

14 years ago
I've unwillingly switched platforms to linux, so I'm a noob all over again.

Here's my test program:


Here's what happens when I try to compile and execute from the command line:



the jdk is located in /jdk1.6.0_05
I have a symlink /usr/bin/javac pointing to the /jdk.../bin/javac file. Yet I am denied.
14 years ago
if mainmenu.class is created, how are you sure the mainmenu.java is not being compiled? what do your javac and java commands look like, and what is the output?
14 years ago