Java(ch1)-Expression,ClassString

6698 단어 JavaJava

java is device independent
WORA : write once runs anywhere

other languages: procedure or function
java: method

Java application Programs
application- class with a method named main
applets(little java application)- run on web browser

public class FirstProgram
{
	public static void main (String[] args)
    {
    	System.out.println("Hello World");
        int answer;
        answer = 2+2;
        System.out.println("2 plus 2 is" + answer); 
    }
}

firstProgram : class name
public static void main(String[] args) : the main method

System.out.println

System.out - sending output to the screen
println - system.out 이 사용하는 method

= & +

answer = 2 + 2; 더하기
"2 plus 2 is" + answer; answer 을 string으로 변환하여 붙임

High level language
Machine language
Assembly language (readable machine lan)
Compiler (high lev -> low lev translate)

source code(x.java) -> compiler ->
byte code(x.class) -> interpreter ->
->result on screen

program terminology

Code
Source code
Object code (output of compiler)

class loader

linker in other programming lan

Syntax : grammar rules
Semantics : meaning of program

Type of Errors

Bug: any / debugging: eliminating bug
syntax error: grammatical mistake
run-time error: error after executin (ex. 5/0)
logic errror : mistake in algorithm, not detected by compiler, but program not working

Expressions and Assignments

Naming Convention

variables & methods (ex.int numberOfBeans)
camel case, start w/ lowercase

classes (ex. FirstProgram)
start w/ uppercase

primitive types

assignment statements

variable = expression;

with initialization
int count = 0;
double distance = 55 * .5;
char grade = 'A';
int initialCount = 50, finalCount; (initial의 값은 50이고 final도 int이다)

assignment compatibility

byte,char < short < int < long < float < double

int x = (int)3.5;
=> x는 3이된다
double a = 5.89e-4;
char c = 'z';
String str = "hello world";
long f = 50000000000000L;
boolean d = true;

  • byte a = 10;
    byte b = 20;
    byte c = a + b ; ERROR(byte+byte -> int+int = int)

precedence and associativity rules

use ()s for safety!
base+rate * hours = base+(rate * hours)
n1=n2=n3 -> n1=(n2=n3)

increment and decrement operators

int n = 2;
int m = 2 * (++n);  //prefix increment of n
System.out.println(m);  //6
System.out.println(n);  //3
int n = 2;
int m = 2 * (n++);  //postfix increment of n 
System.out.println(m);  //4
System.out.println(n);  //3

String Class

no primitive type for string in Java

String str1 = "hello";
String str2 = "world";
String str3 = str1 + " " + str2; // hello world
int k = 35; 
String str4 = "yes" + k ; // yes35

classes, objects, methods

class
name of type whose values are objects
objects ex) System.out
entities that store data and take actions
instance of class type
methods ex) println
the action that an object can take
methods can return a value
objects within a same class has same object, different value

calling a method
System.out.println(...)

String Methods

int length()

String greeting = "Hello"; 
int count = greeting.length(); //5
System.out.println("Length is " + greeting.length()); // Length is 5

boolean equals(Other_String)

String greeting = "Hello";
greeting.equeals("hello"); //returns false

boolean equalsIgnoreCase(Other_string)

String greeting = "Hello";
greeting.equeals("hello"); //returns true

String toLowerCase() String toUpperCase()

String greeting = "Hi Mary!";
greeting.toLowercase(); //returns "hi mary!";
greeting.toUppercase(); //returns "HI MARY!";

String trim()

String pause = "      Hmm        ";
pause.trim() // returns "Hmm" 

char charAt(Position)

String greeting = "Hello!";
greeting.charAt(0); // H 

String substring(Start (,End))

String sample = "AbcdEFG";
sample.substring(2); // cdEFG
sample.substring(3,6); // dEF

int indexOf(A_String)

String greeting = "Hi Mary" ;
greeting.indexOf("Mary"); // 3
greeting.indexOf("Sally"); // -1

int compareTo(A_String)
int compareToIgnoreCase(A_String)
사전식 (소문자 << 대문자)

String entry = "adventure";
entry.compareTo("zoo"); // 사전식 뒤 -> returns negative num
entry.compareTo("adventure") // 동일 0 
entry.compareTo("above"); // 사전식 앞 -> returns positive num
entry.compareToIgnoreCase("Zoo") // negative
entry.compareToIgnoreCase("ADVENTURE") // 0 

escape sequences

\" \' \\ \n \r \t

ASCII

UNICODE
a character set used by the Java language
ASCII + many characters used in language(영어/한글/중..) 2bytes(16bits)

Naming Constants C와 동일
class 안에 설정, 바뀌지 않을 변수

public static final int INCHES_PER_FOOT = 12;
public static final double RATE = 2.5 ;

comments C++과 동일

//주석주석
/* 주석주석 */ 

javadoc

/** automatically extract documentation from block comments in the classes 

좋은 코드!

self-documenting

right choice of identifier names

good indenting pattern

좋은 웹페이지 즐겨찾기