Browsing 7239 questions and answers with Jon Skeet

Compression and Decompression of String using GZIP in java

I would like a simple code to compress and decompress a String in Java. Using GZIP. ex; String input = "Hello world"; and where the output would be a compressed string. ex;...
Jon Skeet
people
quotationmark

Yes, it's possible as a sequence of steps: Convert the string to binary form, e.g. using UTF-8 Compress the binary data Encode the binary data back as text, e.g. using base64. Do not try to "decode" it using a text encoding like base64;... more 9/11/2014 1:25:44 AM

people

Java : Type of Ref or new () decides which methods are visible

Say Person is base class. Doctor class extends Person. Now suppose diagnose() is method which belongs to Doctor class. Person p = New Person(); Doctor q =...
Jon Skeet
people
quotationmark

Why would this throw error? Because you're casting an object of type Person to Doctor. The value of p is a reference to an instance of Person, not to an instance of Doctor, therefore the cast fails. If you had: Person p = new... more 9/11/2014 1:20:52 AM

people

Java. Why does it work differently with english and slavic characters?

I've found a rather strange thing for me while working with Java. Maybe it's an ordinary thing, but i don't understand why it works this way. I have a code like this: Character...
Jon Skeet
people
quotationmark

Without boxing - using just char - you'd be fine. Likewise if you use equals instead of ==, you'd be fine. The problem is that you're comparing references for boxed values using ==, which just checks for reference identity. You're seeing a... more 9/10/2014 4:49:33 PM

people

recursion in java with unexpected output

public class testing { public static void printnum(int a) { System.out.println( a); if(a <= 3) { System.out.println("recursed"); ...
Jon Skeet
people
quotationmark

You've got two calls to System.out.println(a). You'll find it much easier to understand if you differentiate between them: public static void printNum(int a) { System.out.println("Before recursion: " + a); if(a <= 3) { ... more 9/10/2014 3:52:41 PM

people

Little Confusion Over Hashing

Consider ther following code : public class CarEqualsTestAgain { String model; public CarEqualsTestAgain(String x) { this.model=x; } @Override public...
Jon Skeet
people
quotationmark

When each Object is created JVM calculate its hashcode and put it in bucket or When Hashmap put() method is called then only JVM uses key Object to calculate hashcode ? It only calls hashCode() when it needs to - in this case when you... more 9/10/2014 1:55:23 PM

people

Usage of extension methods for framework types

In a new line of work I have been told to avoid using Extension Methods for Types that you (or your organization) have no control over, meaning external libraries, framework types...
Jon Skeet
people
quotationmark

It's an argument which has some merit, but in lots of cases the problems can be avoided: If you control your code and can easily update it if necessary, then you can easily write unit tests to detect the problem and correct it if it... more 9/10/2014 1:12:52 PM

people

System.currentTimeMillis measuring time

When I use System.currentTimeMillis() to measure the time, is it safe? E.g. When a time-shift happens (summer-winter time) will this produce an error?
Jon Skeet
people
quotationmark

No, System.currentTimeMillis() returns the number of elapsed milliseconds since the Unix epoch (midnight at the start of January 1st 1970, UTC). This does not depend on your local time zone at all. In other words, if you call it once per... more 9/10/2014 1:00:38 PM

people

Why doesn't this below code move file from one location to other

string source = textbox1.text; string destination = textBox2.Text; bool exists = System.IO.Directory.Exists(source); if (exists) { // its create directory to destination ...
Jon Skeet
people
quotationmark

The documentation is fairly clear on this: This method creates a new directory with the name specified by destDirName and moves the contents of sourceDirName to the newly created destination directory. If you try to move a directory to... more 9/10/2014 12:16:14 PM

people

C# How to set PropertyInfo value when its type is a List<T> and I have a List<object>

I have an object with a generic List property where T is a primitive value, string, or enum. The generic argument of this list will never be a reference type (except for string)....
Jon Skeet
people
quotationmark

You should create an instance of the property's real type, if you know it really will be a List<T> for some T (rather than an interface, for example). You can then cast to IList and add the values to that, without knowing the actual... more 9/10/2014 4:09:30 AM

people

Why is it showing Element type "bean" must be followed by either attribute specifications, ">" or "/ >".at <property name="list">

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" ...
Jon Skeet
people
quotationmark

You have to finish the opening tag of the element before you have any nested content (other elements, or text). In XML, this is fine: <x> <y /> </x> But this isn't: <x <y /> </x> This isn't... more 9/10/2014 2:54:39 AM

people