Browsing 7239 questions and answers with Jon Skeet

How is 'instanceof' implemented in the JVM?

Does it use reflectione, and if so, what's going on behind the scenes?
Jon Skeet
people
quotationmark

It's part of the JVM instruction set, basically - there's a specific instanceof instruction. So for example, a method like this: public static void checkString(Object x) { if (x instanceof String) { System.out.println("Foo"); ... more 10/5/2013 3:01:10 PM

people

LINQ Join List of List

Using LINQ, how can I join (Inner Join) list of list datas Example datas: List<List<string>> datas1 = new List<List<string>>(); datas1.add(new List() {...
Jon Skeet
people
quotationmark

You're not using the parameters within your lambda expressions in the joins - you've used datas1[0] and datas2[0] instead of d1[0] and d2[0]. Additionally, your projection to the result doesn't give you want you've said you want. Your... more 10/5/2013 12:33:51 PM

people

Error show java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0)

I have three table 1.User 2.Branch 3 userbranch. I have trying to solve login form. but when login button is click it's show this error java.sql.SQLException: Parameter index out...
Jon Skeet
people
quotationmark

Your SQL doesn't have any parameters: Select u.username,u.password,b.branchname from user u, branch b, userbranch ubwhere u.userid = ub.userid and b.branchid=ub.branchid So it's failing when you try to set parameters. You probably... more 10/5/2013 11:28:22 AM

people

Type mismatch: cannot convert from long to int

I had the following lines of code long longnum = 555L; int intnum = 5; intnum+=longnum; intnum= intnum+longnum; //Type mismatch: cannot convert from long to...
Jon Skeet
people
quotationmark

I think line-3 and line-4 do same task, then why compiler showing error on line-4 "Type mismatch: cannot convert from long to int" Because they don't do the same thing. Compound assignment operators have an implicit cast in... more 10/5/2013 11:10:57 AM

people

Calling ILAsm global function from C# code

Suppose I have an ILAsm library with the following code: .assembly extern mscorlib{} .assembly TestMe{} .module TestMe.dll .method public static void PrintMe() cil managed { ...
Jon Skeet
people
quotationmark

You can't, as far as I'm aware. C# only "knows" about methods declared in types - not at the top level. Move your method into a type. more 10/5/2013 11:02:34 AM

people

Is console text still displayed if no console is present?

When starting a "Runnable jar" it doesn't by default start a console. If you would like to invoke a terminal you can run in command prompt (for windows users) "java -jar...
Jon Skeet
people
quotationmark

Naturally with a GUI you wont have a console loaded unless you run the command I put above, so if you are having a lot of print statements, does it still affect the speed of your program, and are they still printed? The best answer... more 10/5/2013 7:18:55 AM

people

How to read a line which doesn't end with new line or carriage return character in java?

I am using BufferedReader as follows, BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); for (int i = 0; i < 4; i++) { ...
Jon Skeet
people
quotationmark

If the input is terminated - which would be the case if standard input is actually being redirected from a file, for example - then it's fine. BufferedReader.readLine will still return the last line from its input, when it detects the end... more 10/5/2013 6:48:42 AM

people

Is it more efficient to use a flag or an if clause?

In a Java loop, is it more efficient to use a boolean flag instead of an if statement? Take a look at these two bits of code. Using a flag: public boolean...
Jon Skeet
people
quotationmark

The two pieces of code aren't quite equivalent. Even though you only call item.isSomething() as many times as you need to (contrary to my original answer), the first version still keeps trying to iterate over the rest of the... more 10/4/2013 4:53:11 PM

people

Passing objects to methods java

I'm using three objects: StringBuilder, Integer, and testobject - that are passed to a method to change its state. As expected, the StringBuilder and testobject points to the...
Jon Skeet
people
quotationmark

Look at your three methods: static void go(StringBuilder s1){ s1.append("Builder"); } static void go1(Integer s2){ s2 = 1; } static void go2(testobject s3){ s3.x = 5; } In go and go2, you're making a modification to the... more 10/4/2013 4:46:26 PM

people

Delete those elements from the list that are indexed in another list

List<Double> elements = new ArrayList<Double>(); List<Integer> usedElements = new ArrayList<Integer>(); for (int i=0; i<usedElements.size(); i++) { ...
Jon Skeet
people
quotationmark

If you know your usedElements list is in ascending order, the simplest approach would be to remove the elements in reverse order - that way the "shuffling up" effect won't affect any of the later operations: List<Double> elements =... more 10/4/2013 4:32:52 PM

people