Browsing 7239 questions and answers with Jon Skeet

JDK Hashmap Source Code Anonymous Inner Classes and Abstract Instantiation?

Problem I'm trying to understand how Sun implemented the entrySet, keySet and values methods of the HashMap class but I'm coming across code that doesn't make sense to me. I...
Jon Skeet
people
quotationmark

On line 599 this code is instantiating the AbstractSet class. How is this possible? Via an anonymous inner class. It's creating a concrete (but unnamed) subclass of AbstractSet. You can do the same for interfaces. For... more 11/30/2013 10:34:00 PM

people

Send file length with outputstream and receive length and byte[] with inputstream for streaming frames from one device to the other Android/Java

I have searched and searched and everything I have found has been helpful but I keep getting an out of memory error. The images I send are .06 MB so I know the problem isn't from...
Jon Skeet
people
quotationmark

This is the problem: int count = inputStream.read(); while (count != -1) { You're consuming a byte and then ignoring it. That means the next value you read (the size) will be incorrect. You need a different way of telling whether you're... more 11/30/2013 2:46:26 PM

people

Canonical equals() method for Java as on Android Developers site

Here's how the site suggests you to write an equals() method. @Override public boolean equals(Object o) { if (this == o) { return true; } if (!(o...
Jon Skeet
people
quotationmark

I believe the point is that this would be a type where referenceField was guaranteed to be non-null, e.g. it's checked in the constructor. Compare this with nullableField, where the nullity is checked as part of equals. more 11/30/2013 2:38:31 PM

people

What am I doing wrong in inserting data to mysql table?

After executing code I get the Data saved message but no data is recorded in my clients table? I'm new to databases with Java, What am I doing wrong or how can I fix my code? ...
Jon Skeet
people
quotationmark

What am I doing wrong Well, you're building your SQL statement by concatenating values. That leads to SQL injection attacks - amongst other issues. Fortunately, that hasn't actually created a problem just yet - because you're never... more 11/29/2013 10:06:01 PM

people

how pass array to constructor java?

How pass array to Constructor with one parameter integer I want to save all the value of array in a1 when use this code : //Constructor,,int a1 welcome(int…a) { ...
Jon Skeet
people
quotationmark

This loop: for(int b:a) { a1=b; } just repeatedly assigns the value of b to a1 - so yes, when the loop has finished, a1 will have the value of the last value in a. If you want to print out the values, you need to put the output in... more 11/29/2013 10:01:53 PM

people

Displaying a date Short Format

I am Trying to show a date in a textbox with short format, i get the data from the database. At presentation Time I want it to be shown with another Format Than this ...
Jon Skeet
people
quotationmark

Inside the database it has rhis format: No, inside the database it will be stored in some binary format. Fundamentally, it's a date - it's not in any particular format, any more than a number is stored in decimal or hex... it's just a... more 11/29/2013 10:00:20 PM

people

Sql Syntax error unknown field list but looking for wrong field?

So I created the following function to write to a database: public static void updateBuyer(String ID, String name, Location latlong) { float lat = latlong.latitude; float...
Jon Skeet
people
quotationmark

The problem is that you've got SQL like this: UDPATE Buyer SET Name = craigs WHERE idBuer in (Whatever) That's trying to copy the value from a column named "craigs". Now you could just add apostrophes - but don't. Instead, use... more 11/29/2013 7:13:51 PM

people

Calling Method of class within it

// What is the technical reason behind this scenarios..?
Jon Skeet
people
quotationmark

You're trying to use a statement other than a declaration directly inside the class - rather than within a method. When did you expect the method to get called? Basically all you can have directly within a type is a bunch of declarations... more 11/29/2013 5:10:35 PM

people

Reading a specific line with StreamReader

I have a problem with the stream reader. i want to read from a text file just one line. I want a specific line, like the seventh line. and i don't know how to. it's a function...
Jon Skeet
people
quotationmark

The simplest approach would probably be to use LINQ combined with File.ReadLines: string line = File.ReadLines("foo.txt").ElementAt(6); // 0-based You could use File.ReadAllLines instead, but that would read the whole file even if you... more 11/29/2013 2:03:00 PM

people

Binary compatibility issue an example?

As far as I understand the source compatibility and how you can easily show an example that would break source compatibility (change name of the method, remove method etc.), I am...
Jon Skeet
people
quotationmark

One example (and this is by no means the only one) would be if the signature of a method in a library changes, in a compatible way. For example, consider: // Library.java v1 public class Library { public static void print(String foo)... more 11/29/2013 1:54:39 PM

people