Browsing 7239 questions and answers with Jon Skeet

Remove BOM from string in Java

I have string in file, that contains BOM (from UTF-8). I want to convert this string to win-1251 and put it in file. I trying to remove BOM from string in this...
Jon Skeet
people
quotationmark

You're replacing the BOM with U+0000, rather than with an empty string. You should replace the BOM with the empty string, e.g. out.write(l.replace("\uFEFF", "") + "\n"); more 11/10/2014 3:52:22 PM

people

Convert UInt32 to float without rounding?

I need to convert a UInt32 type to a float without having it rounded. Say I do float num = 4278190335; uint num1 = num; The value instantly gets changed to 4278190336. Is...
Jon Skeet
people
quotationmark

I need to convert a UInt32 type to a float without having it rounded. That can't be done. There are 232 possible uint values. There are fewer than 232 float values (there are 232 bit patterns, but that includes various NaN values).... more 11/10/2014 3:16:08 PM

people

Encoding.UTF7.GetBytes does not reverse Encoding.UTF7.GetString()

I guess I'm missing something fundamental but I'm really confused by this one and searching has failed to find me anything. I have the following... byte[] bytes1; string...
Jon Skeet
people
quotationmark

What you're seeing is two different ways of encoding the same text in UTF-7. Your original text is: @7y6$Hev&(dr!nU^pPĀ£$Tg:~ The ASCII version of bytes2 is +AEA-7y6+ACQ-Hev+ACY-(dr+ACE-nU+AF4-pP+AKMAJA-Tg:+AH4- In other words,... more 11/10/2014 12:47:46 PM

people

SHA algorithm generates each time unique hash string for a same key

I know there are lots lots of articles available about hashing and encryption algorithm. I have figure it out from them that use hashing function instead of encryption to store...
Jon Skeet
people
quotationmark

This is your most immediate problem: byte pass[] = md.digest(); System.out.println(pass.toString()); You're not returning the hash of the string. You're returning the result of calling toString() on a byte[]. Arrays in Java don't... more 11/10/2014 6:59:51 AM

people

Possible to create a array of referencetype?

For example I have a arraylist of objects and I would like to test what kind of object it is. eg. ArrayList<Object> list = new...
Jon Skeet
people
quotationmark

You can use Class<?> as the type argument for the second list - and then use Class.isInstance to perform the check for "instancehood": List<Class<?>> classes = new... more 11/9/2014 9:05:44 AM

people

Why Observable is still a class in Java 8?

Related Question: Why is java.util.Observable not an abstract class? Since we have interfaces which can contains default methods, isn't it a better idea to change Observable to...
Jon Skeet
people
quotationmark

It should be changed to an interface in Java 8 correct? Not unless you want to break all backwards compatibility, no. If you changed it to an interface, then anything written as: public class Foo extends Observable would be broken.... more 11/9/2014 8:53:43 AM

people

Java: How do I make a loop which creates arrays?

I need a large series of arrays, right now I am creating them manually: int[] row1 = new int[3]; int[] row2 = new int[3]; int[] row3 = new int[3]; I would like to create these...
Jon Skeet
people
quotationmark

Specifically, how do I generate dynamic identifiers on each iteration? You don't. Instead, you realize that you've actually got a collection of arrays. For example, you could have: int[][] rows = new... more 11/7/2014 9:10:32 PM

people

Assigning a value to final instance variable using this keyword

I wanted to understand what am missing in the usage of final variable. I tried googling a lot about this strange (to me at least) behavior, and would love to know what happens...
Jon Skeet
people
quotationmark

In line 2 public final int a the JVM will assign a default value of 0 to a That's what the JVM does. That's not what the compiler does - it's not an assignment within the source code. It's just the value the variable has before it's... more 11/7/2014 6:53:31 PM

people

why do some lines not have semicolon in C#?

I am just trying to figure out the technical reason why in the below some lines do not end with a semicolon but other lines do - what is it about a semicolon that C# expects in...
Jon Skeet
people
quotationmark

The semi-colon isn't a line terminator... it's a statement terminator. There's a semi-colon at the end of every expression statement and declaration statement. (if, for etc statements aren't expression or declaration statements.) So for... more 11/7/2014 2:31:31 PM

people

Initialize my class with array syntax

Is there anyway to initialize my class like an array or a dictionary, for example private class A { private List<int> _evenList; private...
Jon Skeet
people
quotationmark

To use a collection initializer, your class has to: Implement IEnumerable Implement appropriate Add methods For example: class A : IEnumerable { private List<int> _evenList = new List<int>(); private... more 11/7/2014 1:06:54 PM

people