How would you accept input from the user from console?

The easiest way of accepting user input is using the `java.util.Scanner` class. `Scanner` was introduced in Java 1.5 for the exact purpose of reading character streams. Before Java 1.5 programmers had to make do reading byte streams and converting bytes to characters.

We use `Scanner`s to parse character data: strings to numbers, scanning lines, bytes, doubles […]

What are enums? Can you write the code to create an enum?

An enum also called an enumeration (not to be confused with `java.util.Enumeration`) or an enumerated type, is a type that can take any of the predefined values (and only them, and well `null`). Commonly this is used to represent some discrete data type like suits of cards, directions on the compass, days of week etc.

Sometimes […]

Why have stop and suspend method been deprecated in java?

While browsing through the javadoc of the `java.lang.Thread` class you may have seen the `stop` and `suspend` methods. Maybe you have been tempted to use them even though they’re marked as deprecated. Hopefully this article will convince you that this is a bad idea and you’re better of staying away from those methods!

Thread.stop

The stop method […]

How to check if a particular lock has been acquired by a Thread in Java?

The `java.util.concurrent.locks.Lock` provides one method that can be used to find whether this thread owns this lock and that method is `tryLock` in particular the timed variant. Let’s see this in an example.

Running the program yields:

main: Acquired lock
Lock not available… waiting
Lock not available… waiting
Lock not available… waiting
Lock not available… waiting
Lock not available… waiting
main: Releasing lock
Do […]

How does polymorphism work with arrays?

Arrays in Java are covariant. We say that some type is covariant when it is generic and if some other type let’s say `Dog` is of subtype `Animal` then is a subtype of . In general we talk about variance when we want to talk about complex relationships between types.

Reiteraiting what we said above […]

What is the difference between Hashtable and Properties class?

The Java classes `java.util.Hashtable` and `java.util.Properties` share a lot in common. In fact `Properties` inherit from `Hashtable` class. So what are the differences and how do we process property files in Java?

Hashtable

Tha `Hashtable` class provides the following methods:

* `void clear()` removes all the elements of the Hashtable
* `Object clone()` – shallow copy.
* `boolean contains(Object key)` […]

What is the purpose of Closeable, Flushable, Readable and Appendable interfaces?

Closable

`Closable` is the interface for a source of destination data that can be closed.

Because the programmer cannot count on the finalize() method of an object to work in a timely fashion the programmer wanting to use a resource that has to be additionally managed (for example a file or socket connection which has to be […]

How does Java handle overflow and underflow of values for primitive data types?

Paraphrasing Richard O’Keefe there are three ways of dealing with numerical data:

Having the number dynamically grow (this is how `BigDecimal` and `BigInteger`operate)

Having the number throw an exception when it gets incremented or decremented over its range

The broken way where incrementing or decrementing an number silently causes an overflow or underflow. This behiaviour is unfortunately […]

Features of java as a programming language

Java as a language is only one drop in a vast array of other languages – so what makes it unique? Java has a couple of unique features that don’t exist in any other language but in my opinion no one feature that makes Java… well Java.

A combination of all of features makes Java stand […]

Pre increment and post increment operators in Java

Java comes with two types of special operators on numbers the pre-(increment/decrement) operator and the post-(increment/decrement) operator.

The differences between the four of them (syntax-wise and semantics-wise) should become apparent looking at the examples:

Pre-increment

Example code:

Result:

Before: 0
During: 1
After: 1

Post-increment

Example code:

Result:

Before: 0
During: 1
After: 1

Pre-decrement

Example code:

Result:

Before: 0
During: -1
After: -1

Post-decrement

Before: 0
During: 0
After: -1

Summary

The `??x` operators (i.e `–x` and `++x`) operators […]

x

Enter your email address:

Delivered by FeedBurner

Java ExperienceA place for all type of Java web tutorials2014