Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

시작하기

  • 최신 버전의 자바를 http://java.sun.com 에서 다운로드 받으세요.
  • 인스톨러를 실행합니다.
  • Groovy 배포판을 웹사이트 에서 받아서 하드 디스크에 저장하세요.
  • 배포판 압축을 디스크 어딘가에 풀어주세요. 저는 C:\dev\groovy-1.0-jsr-03 에 풀어 두었습니다.
  • jTDS 데이터베이스 드라이버를 이곳 에서 받은 후, 압축을 풉니다(저는 C:\dev\jtds-1.1 에 풀었습니다). 이 압축파일은 폴더 전체를 묶은 것이 아니라서 압축을 풀기 전에 미리 폴더를 만들어두어야 합니다.
  • JAVA_HOME, GROOVY_HOME, CLASSPATH 환경 변수를 지정해주세요. Windows라면 다음 단계를 따라하세요:
    • 내컴퓨터 등록정보을 열고
    • 고급 탭을 클릭한 후
    • 환경 변수 버튼을 누르세요
    • JAVA_HOME 시스템 변수를 추가하고 자바가 설치된 디랙토리를 입력하세요(제 경우는 C:\Program Files\Java\jdk1.5.0_04 입니다).
    • GROOVY_HOME 시스템 변수를 추가하고 Groovy가 설치된 디랙토리를 입력하세요(저는 C:\dev\groovy-1.0-jsr-02).
    • CLASSPATH 사용자 변수가 없다면 추가하고, 경로에 jTDS jar 파일을 추가하세요(저는 C:\dev\jtds-1.1\jtds-1.1.jar).
    • 명령 프롬프트를 실행하고 "set" 명령을 입력해서 위 환경변수들이 제대로 입력되었는지 확인하세요.
  • Groovy 배포판이 설치된 곳의 bin 디랙토리의 groovyConsole.bat 를 실행하세요. 실행되지 않는다면, 명령 프롬프트를 열고 bin 디랙토리로 이동해서 그곳에서 실행해서 어떤 에러 메시지가 나오는지 살펴보세요.

Hello, World

groovyConsole 창 위쪽에 다음 코드를 입력하세요:

그리고 <CTRL-R> 을 누르세요.
결과는 OS 콘솔창(groovyConsole 창 뒤에 있는 검은 창)에 출력됩니다. 그리고 groovyConsole 아래쪽에는 다음 코드가 출력됩니다.

"groovy>"로 시작하는 줄은 콘솔에 의해 처리된 텍스트 입니다. "null"은 처리 결과를 말합니다. 메시지를 출력하는 명령은 어떠한 "값"도 반환하지 않기 때문에 "null"이 찍힌 것입니다.

이번엔 뭔가 "값"을 갖는 명령을 입력해보지요. 콘솔에 다음 텍스트를 입력하세요:

<CTRL+R>을 누르면(앞으로는 <CTRL-R>을 누르라는 말을 생략하겠습니다), 이제 의미있는 "값"이 출력되는 것을 알 수 있습니다.

변수

값을 변수에 입력해놓고 나중에 써먹을 수 있습니다. 아래 코드를 실행해보세요:

Lists 와 Maps

The Groovy language has built-in support for two important data types, lists and maps (Lists can be operated as arrays in Java language). Lists are used to store ordered collections of data. For example an integer list of your favorite integers might look like this:

You can access a given item in the list with square bracket notation (indexes start at 0):

Should result in this output:

You can get the length of the list with the "size" method:

Should print out:

But generally you shouldn't need the length, because unlike Java, the preferred method to loop over all the elements in an list is to use the "each" method, which is described below in the "Code as Data" section.

Another native data structure is called a map. A map is used to store "associative arrays" or "dictionaries". That is unordered collections of heterogeneous, named data. For example, let's say we wanted to store names with IQ scores we might have:

Note that each of the values stored in the map is of a different type. Brett's is an integer, Pete's is a string, and Andrew's is a floating point number. We can access the values in a map in two main ways:

Should produce the output:

To add data to a map, the syntax is similar to adding values to an list. For example, if Pete re-took the IQ test and got a 3, we might:

Then later when we get the value back out, it will be 3.

should print out 3.

Also as an aside, you can create an empty map or an empty list with the following:

Conditional Execution

One of the most important features of any programming language is the ability to execute different code under different conditions. The simplest way to do this is to use the '''if''' construct. For example:

Don't worry too much about the first line, it's just some code to determine whether it is currently before noon or after. The rest of the code executes as follows: first it evaluates the expression in the parentheses, then depending on whether the result is '''true''' or '''false''' it executes the first or the second code block. See the section below on boolean expressions.

Note that the "else" block is not required, but the "then" block is:

Boolean Expressions

There is a special data type in most programming languages that is used to represent truth values, '''true''' and '''false'''. The simplest boolean expression are simply those words. Boolean values can be stored in variables, just like any other data type:

A more complex boolean expression uses one of the boolean operators:

Most of those are probably pretty intuitive. The equality operator is '''==''' to distinguish from the assignment operator '''='''. The opposite of equality is the '''!=''' operator, that is "not equal"

So some examples:

Boolean expressions are especially useful when used in conjunction with the '''if''' construct. For example:

An especially useful test is to test whether a variable or expression is null (has no value). For example let's say we want to see whether a given key is in a map:

Generally null is used to indicate the lack of a value in some location.

Debugging and Troubleshooting Tips

  • Print out the class of a variable that you're interested in with myVar.getClass(). Then look up the documentation for that class.
  • If you're having trouble with a complex expression, pare it down to a simpler expression and evaluate that. Then build up to your more complex expression.
  • Try restarting the groovyConsole (this will clear out all the variables so you can start over.
  • Look for the topic you're interested in in the http://groovy.codehaus.org/Language+Guide Groovy language guide

If you are a Java developer

  • No labels