Casting variables in Java

Casting in Java isn’t magic, it’s you telling the compiler that an Object of type A is actually of more specific type B, and thus gaining access to all the methods on B that you wouldn’t have had otherwise. You’re not performing any kind of magic or conversion when performing casting, you’re essentially telling the … Read more

How do I make a delay in Java?

If you want to pause then use java.util.concurrent.TimeUnit: To sleep for one second or To sleep for a minute. As this is a loop, this presents an inherent problem – drift. Every time you run code and then sleep you will be drifting a little bit from running, say, every second. If this is an issue … Read more

javax vs java package

I think it’s a historical thing – if a package is introduced as an addition to an existing JRE, it comes in as javax. If it’s first introduced as part of a JRE (like NIO was, I believe) then it comes in as java. Not sure why the new date and time API will end up as javax following this logic … Read more

What’s the simplest way to print a Java array?

Since Java 5 you can use Arrays.toString(arr) or Arrays.deepToString(arr) for arrays within arrays. Note that the Object[] version calls .toString() on each object in the array. The output is even decorated in the exact way you’re asking. Examples: Simple Array:String[] array = new String[] {“John”, “Mary”, “Bob”}; System.out.println(Arrays.toString(array)); Output:[John, Mary, Bob] Nested Array:String[][] deepArray = new String[][] {{“John”, “Mary”}, {“Alice”, “Bob”}}; System.out.println(Arrays.toString(deepArray)); //output: … Read more

“Char cannot be dereferenced” error

The type char is a primitive — not an object — so it cannot be dereferenced Dereferencing is the process of accessing the value referred to by a reference. Since a char is already a value (not a reference), it can not be dereferenced. use Character class:

Can’t execute jar- file: “no main manifest attribute”

First, it’s kind of weird, to see you run java -jar “app” and not java -jar app.jar Second, to make a jar executable… you need to jar a file called META-INF/MANIFEST.MF the file itself should have (at least) this one liner: Where com.mypackage.MyClass is the class holding the public static void main(String[] args) entry point. Note that there are several ways to … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)