Java - Sizuha/devdog GitHub Wiki

์„ค์น˜ ํ™˜๊ฒฝ

OS X์—์„œ JDK ์„ค์น˜๊ฒฝ๋กœ(JAVA_HOME) ํ™•์ธํ•˜๊ธฐ

$ /usr/libexec/java_home

Data Types

enum

๋‹จ์ˆœํ•œ ํ˜•ํƒœ๋กœ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด,

public enum ErrorCode { SERVER_CONNECT_ERR, LOGIN_WRONG }

๊ทธ๋Ÿฌ๋ฐ, enum๋„ ์‚ฌ์‹ค์€ ํด๋ž˜์Šค์ด๋‹ค. ๋”ฐ๋ผ์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ƒ์„ฑ์ž๋Š” ๋ฌผ๋ก ์ด๊ณ , ๋งค์„œ๋“œ๋„ ํฌํ•จ ํ•  ์ˆ˜ ์žˆ๋‹ค.

public enum ErrorCode {
        SERVER_CONNECT_ERR(400), LOGIN_WRONG(100);

        private int value;
        private ErrorCode(int value) { this.value = value; }

        public int toInt() { return value; }
}

Generics

Generic Methods

static <T> void fromArrayToCollection(T[] a, Collection<T> c) {
    for (T o : a) {
        c.add(o); // Correct
    }
}

์ œ๋„ˆ๋ฆญ ํด๋ž˜์Šค ์ •์˜

public class Hoge<T> {
    private T t;
}
์ œํ•œ ์„ค์ •
public class A {}
public class B extends A {}
public class C extends B {}

public class Foo1<T extends B> {} // Bใ‚„Cใ‚„ใใฎๆดพ็”Ÿ

// class&interface[&interface[...] ]
public class Bar<T extends B & java.io.Serializable> {}

์™€์ผ๋“œ์นด๋“œ

ๅค‰ๆ•ฐใชใฉใฎๅž‹ใฎๅฎฃ่จ€ๆ™‚ใซใฏใƒฏใ‚คใƒซใƒ‰ใ‚ซใƒผใƒ‰ใ‚’ไฝฟใ†ใ“ใจใŒใงใใ‚‹ใ€‚

Hoge<?> hoge = new Hoge<String>();
Hoge<? extends A> a = new Hoge<B>();

ใ“ใฎใƒฏใ‚คใƒซใƒ‰ใ‚ซใƒผใƒ‰?ใซใฏsuperใจextendsใงๅขƒ็•Œใ‚’่จญๅฎšใ™ใ‚‹ใ“ใจใŒใงใใ‚‹ใ€‚&ใซใ‚ˆใ‚‹่ค‡ๆ•ฐใฎinterfaceใฎ็ถ™ๆ‰ฟใฏ่กจ็พใงใใชใ„ใ€‚

IO

HTTP

GET

    String request_get(String url, List<NameValuePair> params) {
        HttpClient httpclient = new DefaultHttpClient();
        String content;

        try {
            if (params != null && params.size() > 0) {
                boolean first = true;
                String param_str = "?";

                for (NameValuePair nv : params) {
                    param_str += (first ? "" : "&") + nv.getName() + "=" + nv.getValue();
                    first = false;
                }

                URLEncoder.encode(param_str, "UTF-8");
            }

            HttpGet httpget = new HttpGet(url);
            HttpResponse response = httpclient.execute(httpget);

            content = EntityUtils.toString(response.getEntity());
        }
        catch (IOException e) {
            content = null;
        }

        return content;
    }

POST

    String request_post(String url, List<NameValuePair> params) {
        Response result = new Response();
        String content;

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);

        try {
            if (params != null && params.size() > 0) {
                httppost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
            }

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);

            content = EntityUtils.toString(response.getEntity());
        }
        catch (ClientProtocolException e) {
            content = null;
        }
        catch (IOException e) {
            content = null;
        }

        return content;
    }

Lamda (Java 1.8)

์ฐธ๊ณ  ๋งํฌ

Lamda ๊ธฐ๋ณธ ๋ฌธ๋ฒ•

(Argument List) -> body

(int x, int y) -> x + y
() -> 42
(String s) -> { System.out.println(s); } // return์ด ์—†๋Š” ๊ฒฝ์šฐ.

//์ธ์ž์˜ ํƒ€์ž…์„ ์ƒ๋žตํ•  ์ˆ˜๋„ ์žˆ๋‹ค.
(x, y) -> x + y
s -> { System.out.println(s); }

Stream API (Java 1.8)

https://qiita.com/takumi-n/items/369dd3fcb9ccb8fcfa44

ๅŸบๆœฌ็š„ใชๆตใ‚Œ

  • ใ‚ณใƒฌใ‚ฏใ‚ทใƒงใƒณใ‹ใ‚‰streamใ‚’ๅ–ๅพ—
  • streamใซๅฏพใ—ใฆๆบ€่ถณใ™ใ‚‹ใพใงใ€Œไธญ้–“ๆ“ไฝœใ€ใ‚’ๅฎŸ่กŒใ€‚ใ‚ณใƒฌใ‚ฏใ‚ทใƒงใƒณใฎไธญ่บซใ‚’้ƒฝๅˆใ‚ˆใๅค‰ๆ›
  • ใ€Œ็ต‚็ซฏๆ“ไฝœใ€ใงๅค‰ๆ›ใ—ใŸใ‚ณใƒฌใ‚ฏใ‚ทใƒงใƒณใฎไธญ่บซใซๅฏพใ—ใฆๅ‡ฆ็†ใ‚’้ฉ็”จใ™ใ‚‹

Tips

๋„๋ฉ”์ธ ์ด๋ฆ„์— ํ•ด๋‹นํ•˜๋Š” IP ์ฃผ์†Œ ์–ป๊ธฐ

http://stackoverflow.com/questions/2462398/how-to-get-the-ip-address-from-the-domain-name-in-java

InetAddress.getByName("xxxx.yyy.com").getHostAddress();

๋™์ ์œผ๋กœ ํด๋ž˜์Šค๋ฅผ ์ง€์ •ํ•ด์„œ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑ

    // for Android
    public void selectFragment (MainMenu mainMenu){
        int position = mainMenus.indexOf(mainMenu);
        onNavigationDrawerItemSelected(position);
        setTitle(mainMenus.get(position).getName());

        Fragment fragment = null;
        try {
            Class<?> fragmentClass = Class.forName(mainMenus.get(position).getClassName());
            fragment = (Fragment) fragmentClass.newInstance();
            replaceFragment(fragment);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (java.lang.InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

Bytes To Hex String

    /**
     * bytesToHex method
     * Found on the internet
     * http://stackoverflow.com/a/9855338
     */
    static final char[] hexArray = "0123456789ABCDEF".toCharArray();
    public static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for ( int j = 0; j < bytes.length; j++ ) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }

ํŒŒ์ผ์ด๋ฆ„ ๊ฒ€์‚ฌ

    public final static boolean isValidFileName(String filename_without_path) {
        final String[] reservedChars = {"|", "\\", "?", "*", "<", "\"", ":", ">"};

        if (filename_without_path == null || filename_without_path.trim().isEmpty()) return false;

        for (String c: reservedChars){
            if (filename_without_path.indexOf(c) >= 0) return false;
        }

        return true;
    }

String Formatting

https://docs.oracle.com/javase/jp/6/api/java/util/Formatter.html

๋ฌธ์ œ ๋ฐ ํ•ด๊ฒฐ

if๋ฌธ ์ตœ์ ํ™”์— ์˜ํ•ด ์˜๋„์น˜ ์•Š์€ ๊ฒฐ๊ณผ๊ฐ€

@Override
public void onClick(View v) {
    if (v == mBuyButton) {
        if (mManagedType != Managed.SUBSCRIPTION &&
                !service.requestPurchase(mItem, Consts.ITEM_TYPE_INAPP, null)) {
            showDialog(DIALOG_BILLING_NOT_SUPPORTED_ID);
        }
        else if (!service.requestPurchase(mItem, Consts.ITEM_TYPE_SUBSCRIPTION, null)) {
            showDialog(DIALOG_SUBSCRIPTIONS_NOT_SUPPORTED_ID);
        }
    }
}

์ฆ์ƒ

๋‹น์‹œ์— mManagedType != Managed.SUBSCRIPTION ์กฐ๊ฑด์ด true์ธ ์ƒํƒœ๋กœ ์™”๊ธฐ ๋•Œ๋ฌธ์— else if์ชฝ์˜ requestPurchase()๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด ์•ˆ๋œ ์ƒํ™ฉ์ด์—ˆ์œผ๋‚˜, ์‹ค์ œ๋กœ๋Š” if์™€ else if์ชฝ์˜ requestPurchase()๊ฐ€ ๋ชจ๋‘ ํ˜ธ์ถœ๋œ ํ˜„์ƒ.

์›์ธ

์•„๋งˆ๋„ ์ตœ์ ํ™”ํ•˜๋Š” ๊ณผ์ •์—์„œ if ~ else if ~ ํ˜•์‹์˜ ๋ฌธ์žฅ์ด if ~ else ~ ํ˜•์‹์œผ๋กœ ๊ณ ์ณ์ง€๊ณ , requestPurchase()๊ฐ€ ์กฐ๊ฑด ๋น„๊ต ๊ณผ์ •์—์„œ ๋‘๋ฒˆ ํ˜ธ์ถœ์ด ๋˜๋Š” ๋“ฏ.

if (V != C1 && !F(x, 1)) doThen();
else if (!F(x, 2)) doElse();

// ์œ„ ์ฝ”๋“œ๊ฐ€...
// ์ด๋ ‡๊ฒŒ ํ•ด์„๋˜๋ฉด...
if ( V != C1 && !F(x, 1) && F(x, 2) )
	doThen();
else
	doElse();

๋˜ ๋‹ค๋ฅธ ์›์ธ์œผ๋กœ, ์ฒซ if๋ฌธ์˜ !requestPurchase()ํ•œ ๊ฒฐ๊ณผ๊ฐ€ false๊ฐ€ ๋  ๊ฒฝ์šฐ, else if์ชฝ์˜ !requestPurchase()๋ฅผ ํ‰๊ฐ€ํ•ด์•ผ ํ•˜๋ฏ€๋กœ ๋‘˜ ๋‹ค ์‹คํ–‰๋  ์ˆ˜ ์žˆ๋‹ค. (๋ฌผ๋ก  ๋‹น์‹œ์—๋Š” !requestPurchase() ๊ฒฐ๊ณผ๊ฐ€ ์ œ๋Œ€๋กœ true๋กœ ๋–จ์–ด์กŒ๋‹ค.)

ํ•ด๊ฒฐ

  • ๋ฐ˜ํ™˜๊ฐ’์ด boolean ํ˜•์ด๋”๋ผ๋„ ์‚ฌ์ด๋“œ ์ดํŽ™ํŠธ๊ฐ€ ์žˆ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ if๋ฌธ ์กฐ๊ฑด์ ˆ์—์„œ ํ˜ธ์ถœํ•˜์ง€ ๋ง๊ฒƒ.
  • else if๋ฅผ ์“ฐ์ง€ ๋ง๊ณ  ์ค‘์ฒฉ๋œ if๋ฅผ ์‚ฌ์šฉ.
if (V == C1) {
	if (!F(x, 2)) doThen();
}
else {
	if (!F(x, 1)) doElse();
}
โš ๏ธ **GitHub.com Fallback** โš ๏ธ