tags:

views:

470

answers:

10

As a beginner in Java, I would like to know where I'm learning things (correctly), where I'm learning things incorrectly, and what I should learn next. I find I learn best by being given problems I can't immediately solve, and I need to do some research/learning to work out the best solution, then go onto the next problem. I then repeat the old questions once in a while until they have entered my long term memory.

Questions on Stack Overflow are useful in this respect, however they are not ordered in terms of difficulty and many of them are about a specific implementation of some feature.

Please reply with questions (and suitable answers) to this question that would be useful to any person trying to improve their knowledge and experience in Java. I'm sure that many Stack Overflow questions already posted would be suitable answers, in which case just reply with the question and the link to the original question. Also include the level of difficulty: low, medium, high (maybe include low-medium and medium-high in there as well).

+1  A: 

I like:

What does this return?

private int foo() {
  try {
    return 1;
  } catch (Exception e) {
    return 2;
  } finally {
    return 3;
  }
}

Answer: 3

cletus
Blimey, I'm a Java developer and I don't even know the answer to that. C'mon then, share!
Kezzer
It's interesting trivia but what specific does it tell us about Java ? And about the developper being tested and her practical knowledge of the language ? Can you elaborate on what you get out of this question ?
Yann Semet
If you're going for questions like that, you might as well try my old favourite: write an assignment operation for a Java variable that is *not* == to itself.Equally useless in answering the original question, though. ;)
Adrian
I agree with Yann Semet. Sure, it's an interesting piece of trivia, but it tells you nothing about the programmer. Here's a hint: if it appears in Java Puzzlers, it's probably not going to be helpful in the 80% case.
GaryF
I normally don't go for syntactic trickery (eg whether or not someone knows that "int []a[b]" is in fact a valid array declaration) but this question has value in that try-finally blocks are a commonly used and important construct and if you know that you can answer this.
cletus
Worth noting that you WILL get a compiler WARNING on this...
Yuval A
The finally part of the try/catch block is indeed a very important construct and well worth an interview question, in that I totally agree. I just think that the "puzzle" aspect induced by the use of return in that example drives the question away from its purpose.
Yann Semet
+6  A: 

Buy a copy of Effective Java and read it from cover to cover.

Read it regularly.

Concentrate on things such as equals, hashcode, compareto methods.

Learn the collections api thoroughly.

I realise that this doesn't answer your questions directly but (IMHO) it is sound advice!

Fortyrunner
+1 on Collections API
Mario Ortegón
I can't emphasize enough about Effective Java! That book has saved my ass at work and on every interview I've been on.
Tazzy531
+2  A: 

Oh I also like:

Imagine if you put this in a class:

public int hashCode() {
  return 1;
}

Questions:

  1. Is this legal?
  2. What is the effect of putting these objects in a Map? Will that Map work?
  3. How will it affect performance?

Answers:

  1. Yes, it doesn't violate the equals/hashCode contract, which states that if two objects are equal they must return the same hashCode();
  2. The Map will work;
  3. The performance will be O(n) and the Map will operate like an expensive List as everything will be stored in the same bucket.
cletus
It depends on your definition of "legal". If your definition of legal is "does it compile and run", then technically, since it is an integer return value it is "legal". Your question #1 is thus better phrased as "Does this method implementation follow the hashcode method contract?"
MetroidFan2002
His method does follow the hashCode contract. Returning the constant value 1 does not break the contract since two objects that are equal still have the same hash code.
Steve Kuo
+3  A: 

I don't know how much of a beginner you are and what other languages you know, but in my opinion if you don't really know the java.util package you are in trouble. So my questions would be things like:

  1. What are the major differences between LinkedList, ArrayList, Vector and arrays?
  2. When would you use an ArrayList and when a Set?
  3. What are the differences between HashMap and TreeMap and when would you use each of them?

Answers:

  1. Arrays can't grow, so are fundamental building blocks. If you are writing your own data structures and you really need constant factor memory or CPU performance increases they could be useful. ArrayList and Vector are essentially the same except Vector is synchronized. You will nearly always be better using ArrayList, and if you want synchronisation think about it at a higher level. ArrayList is like an array except that it has the benefits of being able to grow, and it can be made Generic for extra type safety. LinkedList is a specialised data structure you won't use as often but can be useful for Queues and so on.
  2. You would use a Set when you don't want duplicate elements but ordering is unimportant. You would use an ArrayList when ordering is important and you want duplicate elements. If you care about Ordering and duplicates then LinkedHashSet is an option.
  3. TreeMap is Ordered, but has slightly slower O(ln n) access. HashMap is unordered but is usually slightly quicker. TreeMap requires a comparison or for elements to be Comparable. HashMap is probably your default choice.
Nick Fortescue
+2  A: 

What does the following program print?

public class ReferencesArePassByValue
{
    public static void doSomething(Integer i)
    {
     i = new Integer(5);
    }

    public static void main(String[] args)
    {
     Integer value = new Integer(42);
     doSomething(value);
     System.out.println(value);  
    }
}

Answer: 42.

Reason: In Java, references are passed-by-value.

A copy of the reference to the new Integer(42) stored in value is passed into the doSomething method. In the doSomething method, the copy of the reference stored in i is tossed out, and the new object reference to new Integer(5) is assigned to i. Note, only the copy of the reference was thrown out here. The method only received a copy to the reference, so it does not affect the original reference in the main method.

Therefore, there is no effect to the value variable in the main method. The value variable keeps the reference to the new Integer(42), so the output of the program is 42.

There seems to be quite a bit of misunderstanding in this topic. This is not pass by reference. In fact, everything in Java, be it primitives or references, are passed to method by-value, not by-reference.

Related questions and links:

coobird
+2  A: 

Have a look at this one: JavaBlackBelt.

JBB gets a lot of love from me, it's the perfect site for me to remind myself how crappy I am and how I should improve my skills! :)
Esko
A: 

Comparsion of strings, taken from javabeginner.com:

public class StringComparsion {

        public static void main(String[] args) {
         String name1 = "Bob";
         String name2 = new String("Bob");
         String name3 = "Bob";
         // 1st case
         if (name1 == name2) {
          System.out.println("The strings are equal.");
         } else {
          System.out.println("The strings are unequal.");
         }
         // 2nd case
         if (name1 == name3) {
          System.out.println("The strings are equal.");
         } else {
          System.out.println("The strings are unequal.");
         }
        }
    }

Answer: First compares to unequal, the second compares to equal (there's only one static string "Bob" created and both name1 and name3 point to it).

david a.
A: 

What is your opinion on checked exceptions in Java? What are the advantages and disadvantages?

I think being able to answer questions like this indicates that you are not just a code monkey - you have the ability to see the bigger picture as well.

DrJokepu
+1  A: 

Via: Ethan Nicholas's Blog http://weblogs.java.net/blog/enicholas/archive/2006/05/understanding_w.html

Some time ago I was interviewing candidates for a Senior Java Engineer position. Among the many questions I asked was "What can you tell me about weak references?" I wasn't expecting a detailed technical treatise on the subject. I would probably have been satisfied with "Umm... don't they have something to do with garbage collection?" I was instead surprised to find that out of twenty-odd engineers, all of whom had at least five years of Java experience and good qualifications, only two of them even knew that weak references existed, and only one of those two had actual useful knowledge about them.

Pierre
A: 

To learn a programming language properly you need to write code. LOTS of code.

I'd suggest that you solve as many problems as you can from Project Euler (http://projecteuler.net/index.php?section=problems) in Java - there are problems of every difficulty requiring only the Java knowledge available from http://java.sun.com/docs/books/tutorial/.

This gives you a strong knowledge of the core language. Then you can start digging into what else you need - database access (JDBC), GUIs (Swing), web stuff (servlets/jsp) or what have you, plus all the "hey this is strange"-stuff.

Have fun - there is always more to learn :)

Thorbjørn Ravn Andersen