'Java'에 해당되는 글 18건

  1. 2010.04.08 C# CurrentTimeMills
  2. 2010.04.08 Java Variable Arguments (Varargs)
  3. 2010.03.24 Java Anonymous Class
  4. 2008.09.01 Java 프레임 타이틀바 없애기
2010.04.08 13:50

C# CurrentTimeMills

Java의 System.currentTimeMillis() 와 동일한 효과를 내는 C# code

public long CurrentTimeMillis()
{
    DateTime Epoch = new DateTime(1970, 1, 1);
    return (long)(DateTime.UtcNow - Epoch).TotalMilliseconds;
}


C#에서는 기준점이 0001-01-01이지만 Java에서는 1970-01-01이다.
또한 C#은 local date를 사용하지만 Java는 UTC date를 사용한다.

따라서 기준일을 1970 년 1월 1일로 설정하고 시간도 UTC로 얻어와 계산하면 된다.

[출처] Java System.currentTimeMillis() equivalent
Trackback 0 Comment 0
2010.04.08 09:45

Java Variable Arguments (Varargs)

Varargs
The varargs, or variable arguments, feature allows a developer to declare that a method can take a variable number of parameters for a given argument. The vararg must be the last argument in the formal argument list.

You might use varargs to allow adding one to many objects to a collection that you have defined by using a single add method call. Suppose you need a method that writes any number of records to the console. Using varargs, you can make any of the following method calls:

write("line 1");
write("line 2", "line 3");
write("line 4", "line 5", "line 6", "line 7", "line 8", "line 9", "line 10");

Before 1.5, you would need to define overloaded versions of the write method to accomplish this. This isn't an effective solution if you want to be able to support any number of records being passed in.

The write method introduces a couple new bits of syntax:

public void write(String... records) {
    for (String record: records)
        System.out.println(record);
}

First, the records argument is defined as type String.... This indicates to the compiler that calling code can pass a variable number of String parameters. For all other intents and purposes, however, String... equates to a String array (String[]).

The Enhanced For Loop
Second, the code uses the new enhanced for loop syntax that is being introduced in Java 1.5. You read the for loop in the example above as "for each String record in records." Or more explicitly, "for each object stored in the String array records, assign the object to a reference named record of type String."

The enhanced for loop, also known as foreach, is a nice feature that can also be used with any of the collection classes in Java 1.5. In addition, Java supplies hooks that allow you to make your own class iterable, so that it can be referenced in a foreach loop.

Behind the Scenes
There isn't any real magic going on with respect to varargs. Behind the scenes, Java is converting the new syntax into equivalent code that would work under Java 1.4 and older versions. The example above is equivalent to coding the following:

write(new String[] { "line 1" });
write(new String[] { "line 2", "line 3" });
write(new String[] { "line 4", "line 5", "line 6"});
// ... 
public void write(String[] records) {
    for (String record: records)
        write(record);
}


[출처] Looking at Varargs in J2SE 1.5

Trackback 0 Comment 0
2010.03.24 18:12

Java Anonymous Class

A type of inner class that has no name that you define right in the middle of a method (where static init blocks and instance init blocks count as methods). You define it, and create an object of that type as a parameter all in one line. An anonymous class is defined and instantiated in a single succinct expression using the new operator.Used for creating simple delegate callback objects.These anonymous inner classes can access the static and instance variables of the enclosing outer class.
Instead of passing arguments to a constructor, your inner class methods can reach out and grab what they need directly from local variables in the enclosing method. The other technique is to use an instance initialiser block. You are only allowed one per anonymous inner class.

Syntax for Anonymous Classes

new class-name ( [ argument-list ] ) { class-body }

package Vinay;
public class VinayMain
{
    public static void main(String[] args)
    {
        // Here is where the anonymous class is defined and instantiated //
        Runnable vinayrun = new Runnable()
        {
            int Total = 0;
            public void run()
            {
                for (int i=0; i<10000000; i++)
                {
                    Total++;
                }
                System.out.println(Integer.toString(Total));
            }
        };

        vinayrun.run();
    }
}


create an instance, named vinayrun , of a nameless (anonymous) class which implements the java.lang.Runnable interface.And we are calling the run mehtod of anonymous class through vinayrun object.

Drawback– The big drawback with anonymous classes is they can’t have explicit constructors. You can’t pass them any parameters when they are instantiated.

Benefits of Anonymous Classes
The key benefit of an anonymous class is encapsulation (or clutter reduction). An anonymous class is, in a sense, the ultimate in private object oriented encapsulation. It is defined exactly where it is needed, it can never be used anywhere else, and it has totally local scope.One final key benefit of anonymous classes is that they have access to all data and methods of their containing classes, including private members; meaning that for small highly localized tasks, they may require less initialization.

References -
http://ssmela.googlepages.com/AnonymousClassesinJava.pdf
http://mindprod.com/jgloss/anonymousclasses.html

[출처] techartifact.com

Trackback 0 Comment 0
2008.09.01 17:03

Java 프레임 타이틀바 없애기

import javax.swing.*;
import java.awt.*;

public class NoTitleBarFrameTest {
    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setUndecorated(true);
        f.setSize(300,300);
        f.setVisible(true);

        //JWindow w = new JWindow();
        //w.setSize(300,300);
        //w.setVisible(true);
    }
}


setUndecorated 메소드를 사용해 보세요. 주의점을 setVisible(true) 전에 해야 한다는 것입니다.
또는 JFrame 대신 JWindow를 사용하시던지요. (밑에 주석 처리한 것입니다.)

[출처] Java 프레임 타이틀바 없애기
Trackback 0 Comment 0