代码改变世界我是程序员;您好程先生;叫我序员就好了

java- beginning 1

2016-02-03  本文已影响28人  小简猫

chapter 1 Null

chapter 2 Basic JAva

commend in terminal in mac:

    javac filename.java // compile the file 
    java filename // run the file 

sign:

    if (n > 10){
        n++;
    }  // {} used for grouping 
    else{
        n--;
    }
    
    // while loop 
    while (i < n){
        i = 3;
        i++;
    }

    // for loop 

    for (int i = 1; i < n; i++) {
        total += i;
    }
     // if i++ is in the for loop, it would run by adding one on the second time;
     
    
    // same as for loop as above but express as while loop 
    int i = 1;
    while (i < n) {
        total += i;
        i++;
    }

i++ and ++i:

declaring the variable:

    int x = 14;
    
    // or 
    int x;
    x = 14;

String:

    String y = "string"; // String are object; and 'a' is type char, not string
    chat t = y.charAt(1); // find the character in the y at index of 1 (start at 0);
    String suby = suby.substring(2,5); // create new string from y start from index of 2 and end at 5 --> "rin";
    int count = y.lenth(); // calculate the lenth of the string;
    
    double radius = 10.5; // can have fraction;
    
    final pi = 3.141592658 // final is the value cannot be changed for pi;

output printing:

    System.out.println("print out on the new line"); 
    System.out.print("print out on the same line"); 

Math calculation:

    String x = "one";
    int y = 2;
    System.out.println("x + y euqal to : "+(x+y)); // x + y euqal to : 2string
    
    int n = 5;
    System.out.println("y+n" + n + y ); // y+n52 <-- start at string, then others are 

string

    System.out.println( n + y + "y+n" ); // 7y+n <-- start at two integer, then calcualte tham first 

equality

    String start = "qwert";
    String end = "qwertyui";
    end.equals(start); // return true if start have same sequence of characters as end;
    
    if (start == end){
        return ture 
    }
    // false, b/c they are not totally the same 

inequality (sorting by alphabetically )

    String minion = "minion";
    String marble = "marble";
    int valueOfSame = minion.compareTo(marble);
    // 0 if exact euqal; positive is minion alphabeticaly greater than marble; nor ...

input by user

    import java.util. *; // import all classes ; or import java.util.Scanner; 
    // or just decling into the variable: java.util.Scanner name = new java.util.Scanner(System.in);
    
    Scanner scanner = new Scanner(System.in);
    int newInt = scanner.newInt():
    double newDouble = scanner.nextDouble(); // input type double;
    scanner.next(); // input next token as string, delete space on the front, as space create after some characters, it delets the rest of them;
    scanner.nextLine(); // input at the current line;
    scanner.hasNextInt(); // return true if they only contain integer 

void: no return from the method
break: end of loop;
return: return variable of method;

    System.exit(n); // normal exit if n = 0; 
    public static call(int a, String b, double c){
    }  // static can be called from the main method  automatically 

vairbale cannot redefine the type :

    int a = 4;
    double a = 5;// illegal 

array:

    // no length specified; 
    int [] NewArrayName;
    int  NewArrayName[];
    
    NewArrayName = new int[20];
    String[] TimeList = new String[]{"morning", "afternoon", "evening"};
    
    int [] ArrayName = int [<rowNumber>]; // <type>[] <name> = new <type>[<size>]
    int [][] twoDimentionalArray = int [<rowNumber>][<columnNUmber>];
    twoDimentionalArray.length(); // check the size of row;
    twoDimentionalArray[0].length(); // check the size of column at row one;
    
    int  NewArrayName[][];
    twoDimentionalArray[0] // <array name> [ <index> ], index start from 0 
    
    public static int[][] array( int singleArray[], int[][] twoDArray){
        return twoDArray
    }

chapter 3 Class

special method toString:

    public String toString(){
    }
    System.out.println(printOut.toString());
    // or 
    System.out.println(printOut); // toString can automatically print printOut as formate in toString;

Constructor:

public/ private

get/set method:

???

    className.getvariable()

this:

    this(variable1,varaible2);// within the same class

?? about this

    public void zero() {
        classname.variable(this);
    } // end zero

java.lang

null:

List:

    // List<Integer> newList = new ArrayList<Integer>();
    
    newList.size(); // length or size of the newList
上一篇下一篇

猜你喜欢

热点阅读