Browsing 7239 questions and answers with Jon Skeet

Formula of currentTimeMillis() in java?

To my knowledge, System.currentTimeMillis()/1000 can show the current time in seconds since 1970-1-1 00:00:00 (YY-MM-DD HH:mm:ss) For example 2013-10-12 21:30:00...
Jon Skeet
people
quotationmark

System.currentTimeMillis() just returns the number of milliseconds since the Unix epoch (January 1st 1970, midnight UTC), as a long. Converting that value into a string is normally the job of something like SimpleDateFormat, via Calendar... more 10/12/2013 1:43:13 PM

people

Close WebResponse, or Leave Open?

So I'm making a program which pretty much makes bulk HttpWebRequests. In this program, speed is main thing. If I can find ways to increase the HttpWebRequests by even a...
Jon Skeet
people
quotationmark

You should absolutely close the WebResponse - ideally with a using statement: using (WebResponse response = request.GetResponse()) { string x = ReadResponseBody(response); } If you don't close the response, the framework doesn't... more 10/12/2013 12:26:44 PM

people

Warn developer to call `super.foo()` in java

Lets say I have these two classes, one extending the other public class Bar{ public void foo(){ } } public class FooBar extends Bar { @Override public void...
Jon Skeet
people
quotationmark

EDIT: To answer the edited, question, which includes: I would even be happy with making the method "un-overrideable" ... just make the method final. That will prevent subclasses from overriding it. From section 8.4.3.3 of the JLS: ... more 10/12/2013 5:47:22 AM

people

How can I sort the data in a loop from least to greatest if the data gets replaced each time it goes back into the loop?

I have a program that should behave like this. You enter the number of students in a class, lets say 3, for example. So then you enter a student ID, followed by 4 numbers. The...
Jon Skeet
people
quotationmark

It sounds like really you should create a Student class, which contains their ID, their results and their desired final grade. You can then have a collection of students (e.g. a Student[] or a List<Student> and sort that by the... more 10/12/2013 5:32:02 AM

people

Java abstract inheritance in nested classes

Does Java allow to do something similar to this: Abstract.java: public abstract class Abstract { int state; abstract void changeState(int newState); public static...
Jon Skeet
people
quotationmark

Your problem has nothing to do with nested classes, and everything to do with the fact that the method in Map is called put, not insert. What you're trying to do is entirely valid. After changing insert to put and importing... more 10/11/2013 9:39:29 PM

people

Using generic linked list but forced to use (cast) when popping

I am deliriously overjoyed to say that I just implemented a generic linked list from a few weeks ago in a project and IT WORKS! One problem, though. I am forced to do a cast (and...
Jon Skeet
people
quotationmark

This is the problem: GenericStack stack = new GenericStack(); That's using the raw type of GenericStack. It's not clear why you've got this at all when you've already extended GenericStack<JTextField> to be honest. I'd expect you... more 10/11/2013 9:18:52 PM

people

String field with single quotation mark is causing an error when inserting record in table

I have below code: query = "insert into tblB2B_OrderStatusTopStillInRB (LSRNbr, ShipName, Units, DroppedInRB, EPT, Status, OnTimeStatus, ShipVia, DroppedInRB_Order, RealEPT)...
Jon Skeet
people
quotationmark

Is there any way to fix that, without removing the single quotation mark from the string? Yes - use parameterized SQL instead. You should never use variable values directly in your SQL like this. It can allow SQL injection attacks,... more 10/11/2013 8:21:31 PM

people

how to use primitives with iterator

How do I use an iterator to get back a stack of ints? My code works with for a statement if i use Objects but not int. If i use a for statement with Objects it work. Does it have...
Jon Skeet
people
quotationmark

The first problem is with your Stack class. It just implements the raw Iterable type. It should implement Iterable<Item> instead. Read more about raw types in the Java Generics FAQ. You still wouldn't be able to create a... more 10/11/2013 3:21:14 PM

people

Passing list of objects to create with arguments to constructor or method

Basically what I'm trying to do is to make a class that can mass-create objects using Activator.CreateInstance(Type type, params object[] args) I will need to pass all the...
Jon Skeet
people
quotationmark

I would suggest you encapsulate the "type and args" into a specific type... then you can use a params array of that. For example: // TODO: Find a better name :) public class TypeBlueprint { public Type Type { get; set; } public... more 10/10/2013 9:34:04 PM

people

Array Strange Postfix Evaluation

I have created an array of primitives and if i executes arr[index]++, then strange enough the content of my array is getting modified. Could someone explain why the content of...
Jon Skeet
people
quotationmark

Arrays are effectively collections of variables - so an array access expression refers to a variable, as per section 15.13 of the JLS: An array access expression refers to a variable that is a component of an array. ... The... more 10/10/2013 9:24:00 PM

people