Computers/Language java

week4. Fundamental Data Types

emzei 2011. 4. 10. 14:42

◆ Number Types

- int : integer, no fractional parts

- double  : floating-point numbers (double precision)

※ Primitive Types

Type

Description

Size

int

The integer type, with range -2,147,483,648 . . . 2,147,483,647

4 bytes

byte

The type describing a single byte, with range -128 . . . 127

1 byte

short

The short integer type, with range -32768 . . . 32767

2 bytes

long

The long integer type, with range 
-9,223,372,036,854,775,808 . . . 9,223,372,036,854,775,807

8 bytes

double

The double-precision floating-point type, with a range of about ±10308 and about 15 significant decimal digits

8 bytes

float

The single-precision floating-point type, with a range of about ±1038 and about 7 significant decimal digits

4 bytes

char

The character type, representing code units in the Unicode encoding scheme

2 bytes

boolean

The type with the two truth values false and true

1 bit





◆ Number Types : Floating-point Types

Rounding errors occur when an exact conversion between numbers is not possible

- Java : Illegal to assign a floating-point expression to an integer variable

*** Java는 자동형변환을 지원하지 않는다캐스팅을 반드시 해주어여 한다.



◆ Constants : final

- A final variable is a constant (c에서 const가 있다면, java에선 final이 있다!)

- Once its value has been set, it cannot be changed

- Convention : Use all-uppercase names for constants



◆ Constants : static final

- Only 1 copy ~ sharing wit/ objects

- If constant values are needed in several methods, declare them together with the instance fields of a class and tag them as static and final



◆ Arithmetic Operator

- basic : +, -, *, /

- Priority of *, / is higher than +,-

- Parenthses control the order of subexpression computation :

ex. ( a + b ) / 2



◆ Increment and Decrement 

- item++   ->>> item = item + 1;

- item--     ->>> item = item - 1;



◆ Integer Division

- / is the division operator

- If both arguments are integer, the result is an integer. The remainder is discarded.

(정수끼리 연산일 때 몫만 계산)



◆ Powers and Roots

Math class : contains methods sqrt and pow to compute square roots and powers

- To compute x^n, you write Math.pow(x,n)

- To take the square root of a number, use Math.sqrt ~ square root of x Math.sqrt(x)

- Mathematical Methods

Function

Returns

Math.sqrt(x)

square root

Math.pow(x, y)

power xy

Math.exp(x)

ex

Math.log(x)

natural log

Math.sin(x), Math.cos(x), Math.tan(x)

sine, cosine, tangent 
(
x in radians)

Math.round(x)

closest integer to x

Math.min(x, y), Math.max(x, y)

minimum, maximum



◆ Cast and Round

Cast converts a value to a different type

- Math.round converts a floating-point number to nearest integer



 Calling Static Methods

- A static method does not operate on an object (ex. Math Class)

- Static methods are declared inside classes

- Naming convention : Classes start with an uppercase letter; objects start with a lowercase



◆ The String Class

- A string  is a sequence  of characters

- Strings are objects of the String class

- A String literal is a sequence of characters enclosed in double quotation marks : "Hello, Emzei!"

- String length is the number of characters in the String

- Empty string : ""



◆ Concatenation

- use the + oeprator :

ex.

String name = "Emzei";

String message = "Hello, " + name;

// message is "Hello, Emzei"


- If one of the arguments of the + operator is a string, the other is converted to a string

ex.

String a = "Agent";

int n =7;

String bond = a + n ; // bond is "Agent7"


- in Print Statements

* Useful to reduce the number of System.out.print instructions

ex.

System.out.print("The total is ");

System.out.println(total);

versus

System.out.println("The total is "+total);



◆ Converting between Strings and Numbers

Convert to number :

int n = Integer.parseInt(str);

double x = Double.parseDouble(x);

- Convert to string :

String str = "" + n;

str = Integer.toString(n);


- convert 할 때...

※ String과 숫자는 위의 함수들을 이용

※ Casting은 숫자와 숫자 사이에서 이용



◆ Substrings (부분문자열)

- String greeting = "Hello, World!";

  String sub = greeting.substring(0, 5); // sub is "Hello"

- Supply start and "past the end" position

- (string object's name).substring(starting index, ending index)

※ charAt(index) -> 해당 index의 char를 리턴



 Reading Input

- System.in has minimal set of features ㅡ it can only read on bye at a time

- In Java 5.0, Scanner class was added to read keyboard input in a convenient manner


ex. 

Scanner in = new Scanner(System.in);

System.out.println("Enter quantity:");

int quantity = in.nextInt();


nextDouble reads a double

nextLine read a line (until user hits Enter)

next reads a word (until any white space)



◆ Reading Input From a Dialog Box

String input = JOptionPane.showInputDialog(prompt)

- Convert strings to numbers if necessary:

int count = Integer.parseInt(input);

- Conversion throws an exception if user doesn't supply a number - see u ch.11

- Add System.exit(0) to the main method of any program that uses JOptionPane

※ 

JOptionPane ~ OptionPanel 만들어주는 API

System.exit(0) -> close program after saving correctly


'Computers > Language java' 카테고리의 다른 글

week6. Arrays and Array List  (0) 2011.04.10
week5. Decision  (0) 2011.04.10
week3. Implementing Classes (Basic Java Concept part2)  (0) 2011.04.10
week2. Using Objects (Basic Java Concept part1)  (0) 2011.04.10
week1. Introduction  (0) 2011.04.09