Java(ch1)-Expression,ClassString


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使用方法

= & +


answer = 2 + 2; プラス記号
"2 plus 2 is"+ 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; (初期値は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+言語で使用される多くの文字(英語/韓国語/中...)2bytes(16bits)
    名前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