java interview questions
Top java frequently asked interview questions
Use of java.net.URLConnection
is asked about pretty often here, and the Oracle tutorial is too concise about it.
That tutorial basically only shows how to fire a GET request and read the response. It doesn't explain anywhere how to use it to among others perform a POST request, set request headers, read response headers, deal with cookies, submit a HTML form, upload a file, etc.
So, how can I use java.net.URLConnection
to fire and handle "advanced" HTTP requests?
Source: (StackOverflow)
Until today I thought that for example:
i += j;
is just a shortcut for:
i = i + j;
But what if we try this:
int i = 5;
long j = 8;
Then i = i + j;
will not compile but i += j;
will compile fine.
Does it mean that in fact i += j;
is a shortcut for something like this
i = (type of i) (i + j)
?
Source: (StackOverflow)
I've always been one to simply use:
List<String> names = new ArrayList<String>();
I use the interface as the type name for portability, so that when I ask questions such as these I can rework my code.
When should LinkedList
be used over ArrayList
and vice-versa?
Source: (StackOverflow)
Are there clear rules on when to use each of these when making classes and interfaces and dealing with inheritance?
Source: (StackOverflow)
If I run the following program, which parses two date strings referencing times one second apart and compares them:
public static void main(String[] args) throws ParseException {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str3 = "1927-12-31 23:54:07";
String str4 = "1927-12-31 23:54:08";
Date sDt3 = sf.parse(str3);
Date sDt4 = sf.parse(str4);
long ld3 = sDt3.getTime() /1000;
long ld4 = sDt4.getTime() /1000;
System.out.println(ld4-ld3);
}
The output is:
353
Why is ld4-ld3
not 1
(as I would expect from the one-second difference in the times), but 353
?
If I change the dates to times one second later:
String str3 = "1927-12-31 23:54:08";
String str4 = "1927-12-31 23:54:09";
Then ld4-ld3
will be 1
.
Java version:
java version "1.6.0_22"
Java(TM) SE Runtime Environment (build 1.6.0_22-b04)
Dynamic Code Evolution Client VM (build 0.2-b02-internal, 19.0-b04-internal, mixed mode)
Timezone(TimeZone.getDefault()
):
sun.util.calendar.ZoneInfo[id="Asia/Shanghai",
offset=28800000,dstSavings=0,
useDaylight=false,
transitions=19,
lastRule=null]
Locale(Locale.getDefault()): zh_CN
Source: (StackOverflow)
If you have java.io.InputStream
object, how should you process that object and produce a String
?
Suppose I have an InputStream
that contains text data, and I want to convert this to a String
(for example, so I can write the contents of the stream to a log file).
What is the easiest way to take the InputStream
and convert it to a String
?
public String convertStreamToString(InputStream is) {
// ???
}
Source: (StackOverflow)
I always thought Java was pass-by-reference; however I've seen a couple of blog posts (for example, this blog) that claim it's not. I don't think I understand the distinction they're making.
What is the explanation?
Source: (StackOverflow)
From what time I've spent with threads in Java, I've found these two ways to write threads:
With implements Runnable
:
public class MyRunnable implements Runnable {
public void run() {
//Code
}
}
//Started with a "new Thread(new MyRunnable()).start()" call
Or, with extends Thread
:
public class MyThread extends Thread {
public MyThread() {
super("MyThread");
}
public void run() {
//Code
}
}
//Started with a "new MyThread().start()" call
Is there any significant difference in these two blocks of code ?
Source: (StackOverflow)
I have an array that is initialized like:
Element[] array = {new Element(1), new Element(2), new Element(3)};
I would like to convert this array into an object of the ArrayList class.
ArrayList<Element> arraylist = ???;
Source: (StackOverflow)
The following print statement would print "hello world".
Could anyone explain this?
System.out.println(randomString(-229985452) + " " + randomString(-147909649));
And randomString()
looks like this:
public static String randomString(int i)
{
Random ran = new Random(i);
StringBuilder sb = new StringBuilder();
while (true)
{
int k = ran.nextInt(27);
if (k == 0)
break;
sb.append((char)('`' + k));
}
return sb.toString();
}
Source: (StackOverflow)
I was looking at the new APIs introduced in Android 4.2.
While looking at the UserManager
class I came across the following method:
public boolean isUserAGoat()
Used to determine whether the user making this call is subject to teleportations.
Returns whether the user making this call is a goat.
How and when should this be used?
Source: (StackOverflow)
I want to package my project in a single executable JAR for distribution.
How can I make Maven package all dependency JARs into my JAR?
Source: (StackOverflow)
If I have an object implementing the Map
interface in Java and I wish to iterate over every pair contained within it, what is the most efficient way of going through the map?
Will the ordering of elements depend on the specific map implementation that I have for the interface?
Source: (StackOverflow)
Do Android devices have a unique ID, and if so, what is a simple way to access it using Java?
Source: (StackOverflow)