This forum is in archive mode. You will not be able to post new content.

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - red.evil

Pages: [1]
1
Java / Re: Java quiz series
« on: March 16, 2014, 12:31:09 AM »
It can't compile, because at the beginning of the compiling/parsing progress every unicode sign will be read and replaced. In this case, \u000d, it will be replaced with a newline. This leads to a source code like:
Code: [Select]
public class MonkeyDLuffy {
  public static void main(String... string) {
    System.out.println("Kaizoku-oni, orewa naru!");
         //Character c = new Character('
');
   }
}
and some of the comment is now in a new line and not a comment anymore.

If my answer is correct someone else can ask the next question.

2
Java / Re: Java quiz series
« on: January 21, 2014, 12:44:32 PM »
I think I inartfully expressed what I mean.
I meant it like, the JVM maybe already recognises that there's a variable used in a multithread system. So it's updating it in a way you won't get problems because of the caching (like MESI does on a physical multicore system). But yep, the reordering-problem still exists.

3
Java / Re: Java quiz series
« on: January 20, 2014, 10:05:12 PM »
Yep, thats right!

But, I talked to my lecturer today if I have to use it always or not, because of the leaking/ caching problem . He said, he never used it and also didn't have problems, so maybe it's already fixed by the JVM. If someone does have more information about it, it would be nice if he/she would share it with me.

4
Java / Re: Java quiz series
« on: January 06, 2014, 10:57:53 AM »
Yep, I have one:

Code: (Java) [Select]
class Clazz extends Runnable
{
   private volatile boolean stop = false;
   
    public void stop()
    {
        stop = true;
    }
 
    @Override
    public void run()
    {
        while( !stop )
        {
            // do Something
        }
    }
}
Why does the variable stop have to be volatile?

5
Java / Re: Java quiz series
« on: January 05, 2014, 05:49:57 PM »
The result is 394, because 0610  ( = 392|10 ) is a number written in octal number system, which you can see on the leading 0 and 0b10 ( = 2|10 ) is one in binary system, because of the leading 0b. This binary notation works since SE 7.

6
Java / Re: Question: byte array - to - hex - algorithm
« on: January 05, 2014, 01:56:16 PM »
Oh, sorry I've misread your code snippet..
Initially I wanted BigIntegers also to save the value, but I think with your method I will just use the given byte-array as it is.

Thank you all for you help!

7
Java / Re: Question: byte array - to - hex - algorithm
« on: January 04, 2014, 09:32:03 PM »
I've tried Integer and Long already, but they're too small, because I need those calculations for Hash- and Checksums  (e.g. SHA-512).

8
Java / Re: Question: byte array - to - hex - algorithm
« on: January 04, 2014, 06:38:43 PM »
Thank you, but that wasn't my problem. My problem was to understand why I need to do this logic operationm, but I have a solution to this question now.
It is because normaly byte is signed in Java. If we do such an operation it will be casted to an also signed int and will be interpreted as two's complement. But what we want in this case is an unsigned byte and don't want to lose our byte pattern. To save the byte pattern, we can make the logic operation somebyte & 0xff.
So my mistake was to not consider that we have a signed variable here.

9
Java / Question: byte array - to - hex - algorithm
« on: January 04, 2014, 04:55:13 PM »
Hey guys,
for my current project I need to convert a byte array to a hex value, since I had problems with solving it I googled and found this algorithm:
Code: (Java) [Select]
    public static BigInteger convertToBigInteger( byte[] toConvert )
    {
   BigInteger converted = BigInteger.valueOf( 0 );
   for( int i = 0; i < toConvert.length; i++ )
   {
      converted = converted.shiftLeft( 8 );
      converted = converted.add( BigInteger.valueOf( toConvert[i] & 0xff ) );
   }
   return( converted );
    }
So I will get a BigInteger which I can simply convert to a Hex-String.
But I don't get why I have to do this operation: toConvert & 0xFF
Does somebody of you know why I have to do it?

Regards,
red.evil


*staff note: Do not double post!

10
Java / Re: Java quiz series
« on: January 02, 2014, 11:57:12 PM »
It's correct.

11
Java / Re: Java quiz series
« on: January 02, 2014, 03:18:46 PM »
Code: (java) [Select]
class Quiz
{
   public static void main( String args[] )
   {
      int x = 0;
      int y = 55;
 
     try
     {
  System.out.println( y/x );
     }
     catch( Exception e )
     {
         e.printStackTrace();
     }
  }
}

Will there be an exception thrown?
If yes:
  when will it be thrown?
     At compiletime? or
     Runtime?
  what Exceptiontype is it?
  Why is it thrown at this specific time ( Runtime / Compiletime ) ?
 

12
Java / Re: Java quiz series
« on: January 02, 2014, 02:43:34 AM »
It returns false.
Because we're using an instance of HashSet<> as Set; and this HashSet uses the methods hashCode() and equals() to check if two items given to the Set are equal or not.
Since we haven't implemented an extra hashCode method, it uses the standart hashCode() from Object and there we will get two different codes for those two objects.

// Edit: added the explanation

Example to get true:
Code: (java) [Select]
   
import java.util.HashSet;
import java.util.Set;
     
public class Name
{
      private final String first, last;
   
      public Name( String first, String last )
      {
            this.first = first;
            this.last = last;
       }
     
        @Override
        public boolean equals( Object o )
        {
              if ( !( o instanceof Name ) )
                      return false;

              Name n = (Name) o;
              return n.first.equals( first ) && n.last.equals( last );
        }
       
        @Override
        public int hashCode()
        {
             return( first.hashCode() + last.hashCode() ); // this isn't a good hashCode method! Don't do it in real but here it works
        }
     
        public static void main( String[] args )
       {
              Set<Name> s = new HashSet<Name>();
               s.add( new Name( "Mickey", "Mouse" ) );
              System.out.println( s.contains( new Name( "Mickey", "Mouse" ) ) );
        }
}

13
General discussion / Re: Wishing You All A Happy New Year
« on: January 01, 2014, 05:29:56 PM »
Happy new year to all of you!

Pages: [1]


Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.