John Smithonian

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

Recent posts by John Smithonian

Thanks all, I also don't like the idea of catching, logging, and rethrowing.
8 years ago
Hello,

I am looking at this article:

http://www.theserverside.com/tutorial/OCPJP-Use-more-precise-rethrow-in-exceptions-Objective-Java-7

And I am confused. For example, they have this code, but it makes no sense to me, why would you want to throw OpenException and CloseException when you are catching them already? Surely either you catch an exception, or you throw it, why would you need to do both?

Thanks.
8 years ago
I think on Code Sample 2, he meant to comment out line 41, but forgot to.

So Code Sample 1 won't work as explained - before you can use a ResultSet, you have to do a next() or last() or first() etc. As you haven't done any of these before line 41, you get an error as you have started using it.

If you want to show all the rows first, do a (while rs.next()) { ... }
8 years ago
Great, thanks guys.
8 years ago
Hello,

In the line BufferedReader br = new BufferedReader(new Filereader("myFile.txt")), I see two resources, one is br, and one is anonymous.

Now try-with-resources closes br, but what about the anonymous resource? Do inner resources get closed when the outer resource gets closed?

Thanks.
8 years ago

Campbell Ritchie wrote:What happened when you tried to compile that code?



I didn't, there isn't any point in randonly compiling things without understanding the theory, but I found a tutorial that explains it well, so understand it now.

Thanks.
8 years ago
Oh. So if we have A <- B

Are you saying
MyClass<A> a = new MyClass<B>(); // is wrong, as MyClass<B> doesn't extend MyClass<A>

a.methodOfA(A myArg); // myArg can be an object of type B because B extends A
?

Is this correct?

Thanks.
8 years ago
Hello,

I am confused.

At https://docs.oracle.com/javase/tutorial/java/generics/inheritance.html, it states that if A <- B, then it doesn't mean that you can subsitute MyClass<B> in place of MyClass<A>. Fair enough, makes sense.

But then at https://docs.oracle.com/javase/tutorial/java/generics/unboundedWildcards.html it says that "You can insert an Object, or any subtype of Object, into a List<Object>". Why? Just because Object <- AnyClass, surely we cannot substitute MyClass<AnyClass> whenever we have MyClass<Object>.

I find the Java tutorial on Generics confusing, I really do, maybe it isn't and it is just me because I am just trying to rush it, but I find it confusing.
8 years ago

Christopher Schneider wrote:
You should remember that T can be anything. With that code, I could theoretically do this:



There is no method getWhatAmI() defined for an Integer class, however, so you'll receive a compile time error.



Of course - you are absolutely right. It makes sense now.

Thanks!
8 years ago
Thanks guys.

Actually, I had read that Box example from official documentation, but was still confused, but now read it again, and see where I am going wrong.

However, I still have an error in method getItemFromBag() - error: "The method getWhatAmI() is undefined for the class T".

Any ideas>

8 years ago
Hello,

I am getting confused by generics. Please ignore the really bad design of the code below, it's just for test purposes.

Basically, I am a bit confused here - I know you can specify more than one type in the class definition, but what if you want your variable to be able to be more than one type?

For example, what if I want to be able to put either a phone or a comb in my bag, how can I do this? I have tried the below, but I seem to be doing it all wrong.

Thanks.

8 years ago

Campbell Ritchie wrote:I think they were introduced in Java1.2 which was about 1998.



Ah, I must have forgotten about them then.

Thanks.
8 years ago
Thanks all, I now went and read about anonymous classes here ...

https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html#declaring-anonymous-classes

... and now it makes sense.

Just out of curiosity, have anonymous classes always been around? I did the SCJP in 2006, and I don't remember coming across them then.

Thanks.
8 years ago
Hello,

I am reading a tutorial on exceptions.

http://tutorials.jenkov.com/java-exception-handling/exception-handling-templates.html

I am rather confused what is going on here. Below is the code, can someone please explain that is happening? This is the bit I am confused about:



We seem to be creating an object without actually giving it a name, and then calling a method on that object, but then we are also defining the method too? This is rather confusing. Please help me understand this or direct me to some page where I can learn about this syntax.

Class InputStreamProcessingTemplate below:


8 years ago