Naming Conventions

http://www.oracle.com/technetwork/java/codeconventions-135099.html

 

Identifier Type

Rules for Naming

Examples


 Packages

 The prefix of a unique package name is always written in all-lowercase ASCII letters and should be one of the top-level domain names, currently com, edu, gov, mil, net, org, or one of the English two-letter codes identifying countries as specified in ISO Standard 3166, 1981.

Subsequent components of the package name vary according to an organization’s own internal naming conventions. Such conventions might specify that certain directory name components be division, department, project, machine, or login names.

 com.sun.eng

com.apple.quicktime.v2

edu.cmu.cs.bovik.cheese


 Classes

 Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words-avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).

 class Raster;
class ImageSprite;

 Interfaces

 Interface names should be capitalized like class names.

 interface RasterDelegate;
interface Storing;

 Methods

 Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.

 run();
runFast();
getBackground();

 Variables

 Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with capital letters. Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed.

Variable names should be short yet meaningful. The choice of a variable name should be mnemonic- that is, designed to indicate to the casual observer the intent of its use. One-character variable names should be avoided except for temporary “throwaway” variables. Common names for temporary variables are i, j, k, m, and n for integers; c, d, and e for characters.


int             i;
char            c;
float           myWidth;

 Constants

 The names of variables declared class constants and of ANSI constants should be all uppercase with words separated by underscores (“_”). (ANSI constants should be avoided, for ease of debugging.)

 static final int MIN_WIDTH = 4;

static final int MAX_WIDTH = 999;

static final int GET_THE_CPU = 1;


 

int => string vs string => int

// integer => string type conversion
int i = -10;                    // -10
String str1 = Integer.toString(i); // “-10”
String str2 = String.valueOf(i); // “-10”
String str3 = new Integer(i).toString(); // “-10”

String str4 = String.format(“%d”, i); // “-10”

// string => integer type conversion
String x = “123”;          // “123”
int y = Integer.parseInt(x); // 123 (integer로 변환이 가능한 경우만 변환 가능 그 외엔 run-time exception error)
int z = Integer.valueOf(x); // 123 (integer로 변환이 가능한 경우만 변환 가능 그 외엔 run-time exception error)
int w = new Integer(x).intValue(); // 123 (integer로 변환이 가능한 경우만 변환 가능 그 외엔 run-time exception error)

Data Type

8 Primitive Data Type

https://en.wikibooks.org/wiki/Java_Programming/Primitive_Types

boolean A binary value of either true or false
byte 8 bit signed value, values from -128 to 127
short 16 bit signed value, values from -32.768 to 32.767
char 16 bit Unicode character
int 32 bit signed value, values from -2.147.483.648 to 2.147.483.647
long 64 bit signed value, values from -9.223.372.036.854.775.808 to 9.223.372.036.854.775.808
float 32 bit floating point value
double 64 bit floating point value

Object Types

Data type Description
Boolean A binary value of either true or false
Byte 8 bit signed value, values from -128 to 127
Short 16 bit signed value, values from -32.768 to 32.767
Character 16 bit Unicode character
Integer 32 bit signed value, values from -2.147.483.648 to 2.147.483.647
Long 64 bit signed value, values from -9.223.372.036.854.775.808 to 9.223.372.036.854.775.808
Float 32 bit floating point value
Double 64 bit floating point value
String N byte Unicode string of textual data. Immutable

Reference Types

class A class is also a data type – a non primitive reference data type
interface An interface is a reference type in Java, it is similar to class, it is a collection of abstract methods. 
array An array’s type is written as type[] , where type is the data type of the contained elements; arrays are objects, are dynamically created, and may be assigned to variables of type Object
enum An enum type is a special data type that enables for a variable to be a set of predefined constants.

 

 

Primitive Types vs Reference Types

https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html

Primitive Types

A primitive type is predefined by the Java programming language and named by its reserved keyword (§3.9): boolean, integer, long, float, double, byte, short, char.

Reference Types

There are four kinds of reference types: class types (§8), interface types (§9), type variables (§4.4), and array types (§10).

Lab0

Lab0

HelloWorld 프로그램을 작성한다.

JDK 설치

IDE 설치

환경설정

이클립스를 사용한 자바 프로그램 작성

프로젝트 디렉토리 안에 보고서 (1~2장)를 넣고 Lab0_학번_이름.zip 압축한 후 e-learning(http://lms.dankook.ac.kr/index.jsp)으로 제출

Eclipse

Eclipse Neon (eclipse-java-neon-R-win32.zip) Download

http://www.eclipse.org/downloads/

eclipse

Eclipse 프로젝트 생성 File->New->Java Project

eclipse_project

클래스 생성 File->New->Class

eclipse_class

클래스에 코드 추가

eclipse_code

실행 Run (Ctrl+F11)

eclipse_run1