월요일, 12월 23
Shadow

미분류

#048 I/O Stream

미분류
An I/O Stream represents an input source or an output destination. A stream can represent many different kinds of sources and destinations, including disk files, devices, other programs, and memory arrays. Streams support many different kinds of data, including simple bytes, primitive data types, localized characters, and objects. Some streams simply pass on data; others manipulate and transform the data in useful ways. No matter how they work internally, all streams present the same simple model to programs that use them: A stream is a sequence of data. A program uses an input stream to read data from a source, one item at a time: Reading information into a program. A program uses an output stream to write data to a destination, one item at time: Writing information...

#047 Generic Methods and Constructors

미분류
Type parameters can also be declared within method and constructor signatures to creategeneric methods and generic constructors. This is similar to declaring a generic type, but the type parameter's scope is limited to the method or constructor in which it's declared. /** * This version introduces a generic method. */ public class Box<T> { private T t; public void add(T t) { this.t = t; } public T get() { return t; } public <U> void inspect(U u){ System.out.println("T: " + t.getClass().getName()); System.out.println("U: " + u.getClass().getName()); } public static void main(String[] args) { Box<Integer> integerBox = new Box<Integer>(); integerBox...

#046 Generic Types

미분류
Generic Types Let's update our Box class to use generics. We'll first create a generic type declaration by changing the code "public class Box" to "public class Box<T>"; this introduces onetype variable, named T, that can be used anywhere inside the class. This same technique can be applied to interfaces as well. There's nothing particularly complex about this concept. In fact, it's quite similar to what you already know about variables in general. Just think of T as a special kind of variable, whose "value" will be whatever type you pass in; this can be any class type, any interface type, or even another type variable. It just can't be any of the primitive data types. In this context, we also say that T is a formal type parameterof the Box class. /** * Generic version of the Box...

#045 Generic

미분류
기존 JDK 1.4.x까지는 java.util 패키지에 있는 컬렉션 패키지를 포함해서 객체를 보관하는 자료 구조들이 모두 Object 타입으로 처리하게 되어 있었지요. 그런데 이렇게 되면, 실제 그 보관된 자료들이 어떤 클래스(또는 인터페이스) 타입인지 알아보기가 어렵습니다. 코드를 작성하면서 하나의 타입으로 통일해서 넣는다고 해도 꺼내 올 때는 강제로 형 변환을 해주어야만 하지요. 바로 아래와 같은 형식이 될겁니다.   Integer iObj = new Integer(10);   ArrayList arr = new ArrayList(); arr.add(iObj);   Integer iObj2 = (Integer) arr.get(0);   다시 정리하면, 저장된 데이터가 어떤 타입인지 알기가 어렵기 때문에 프로그램 실행시 예기치 못한 오류가 발생할 가능성이 커지고, 불필요하게 형변환을 계속 해줘야 하기 때문에 불편하지요.   그런데 저런 것들이 Java 5로 오면서 이렇게 바뀝니다.   Integer iObj = new Integer(10);   ArrayList<Integer> arr = new ArrayList<Integer>(); // Generic을 이용하여 보관할 타입을 지정합니다. arr.add(iObj);   Integer iObj2 = arr.get(0); // 보관된 데이터의 타입이 확실하므로 형변환이 필요...

#043 Manipulating Characters in a String

미분류
Manipulating Characters in a String The String class has a number of methods for examining the contents of strings, finding characters or substrings within a string, changing case, and other tasks. Getting Characters and Substrings by Index You can get the character at a particular index within a string by invoking the charAt()accessor method. The index of the first character is 0, while the index of the last character is length()-1. For example, the following code gets the character at index 9 in a string: String anotherPalindrome = "Niagara. O roar again!"; char aChar = anotherPalindrome.charAt(9); Indices begin at 0, so the character at index 9 is 'O', as illustrated in the following figure:  If you want to get more than one consecutive character from a string, you ca...

#044 Converting Strings to Numbers

미분류
Converting Strings to Numbers Frequently, a program ends up with numeric data in a string object—a value entered by the user, for example. The Number subclasses that wrap primitive numeric types ( Byte, Integer, Double, Float, Long, andShort) each provide a class method named valueOf that converts a string to an object of that type. Here is an example, ValueOfDemo , that gets two strings from the command line, converts them to numbers, and performs arithmetic operations on the values: /* * Copyright (c) 1995 - 2008 Sun Microsystems, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above c...

#042 Strings

미분류
Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects. The Java platform provides the String class to create and manipulate strings. Creating Strings The most direct way to create a string is to write: String greeting = "Hello world!"; In this case, "Hello world!" is a string literal—a series of characters in your code that is enclosed in double quotes. Whenever it encounters a string literal in your code, the compiler creates a String object with its value—in this case, Hello world!. As with any other object, you can create String objects by using the new keyword and a constructor. TheString class has 11 constructors that allow you to provide the initial value of the string using different sour...

#041 Characters

미분류
Most of the time, if you are using a single character value, you will use the primitive char type. For example: char ch = 'a'; char uniChar = 'u039A'; // Unicode for uppercase Greek omega character char[] charArray ={ 'a', 'b', 'c', 'd', 'e' }; // an array of chars There are times, however, when you need to use a char as an object—for example, as a method argument where an object is expected. The Java programming language provides a wrapper class that "wraps" thechar in a Character object for this purpose. An object of type Character contains a single field, whose type is char. This Character class also offers a number of useful class (i.e., static) methods for manipulating characters. You can create a Character object with the Character constructor: Character ch = new Chara...

#040 TestFormat.java, that follows the table

미분류
An Example The following table lists some of the converters and flags that are used in the sample program,TestFormat.java, that follows the table.   Converters and Flags Used in TestFormat.java Converter Flag Explanation d A decimal integer. f A float. n A new line character appropriate to the platform running the application. You should always use %n, rather than n. tB A date & time conversion—locale-specific full name of month. td, te A date & time conversion—2-digit day of month. td has leading zeroes as needed, te does not. ty, tY A date & time conversion—ty = 2-digit year, tY = 4-digit year. tl A date & time conversion—hour in 12-hour clock. tM A date & time conversion—minutes in 2 dig...

#039 Conversion Methods, Integer Class

미분류
Conversion Methods, Integer Class Method Description static Integer decode(String s) Decodes a string into an integer. Can accept string representations of decimal, octal, or hexadecimal numbers as input. static int parseInt(String s) Returns an integer (decimal only). static int parseInt(String s, int radix) Returns an integer, given a string representation of decimal, binary, octal, or hexadecimal (radixequals 10, 2, 8, or 16 respectively) numbers as input. String toString() Returns a String object representing the value of this Integer. static String toString(int i) Returns a String object representing the specified integer. static Integer valueOf(int i) Returns an Integer object holding the value of the specified primitive. static Integer ...