java- beginning 1

chapter 1 Null

chapter 2 Basic JAva

commend in terminal in mac:

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

sign:

  • all simple statements end of semi colon;
  • grouping by condition
    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:

  • if i++ as independent statment in the method, it runs after the line;
  • compare ++i vs. i++ : ++i : runs at the same line

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

  • name of file and name of class should be the same;
  • object can instance the class (into the variable was declaring in the class); if the class call "score" and there is variable called "grading", --> can called: score newgrading = score.grading (for the variable in grading)

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:

  • same name as the class name;
  • can invoke the method below inside, as class was called, it can run at the same time;
  • declaring the variable for the method below
  • insert the parameter from the class for method to use as below --> rename them using by "this"
  • if no parameter, default as 0; if defined as exiting of parameters, it should have parameters

public/ private

  • public: can be used outside of class
  • private: can only be used and defined inside the class --> cannot run on class.private name

get/set method:

  • can get from outside; limited the variable can be get
  • set: set the status for the variable; such as public and private
  • use for limitation for access and change the variable compensation
  • use public “get” method to access private variable

???

    className.getvariable()

this:

  • take whole parameters from the class;
  • rename it from the parameters
    this(variable1,varaible2);// within the same class

?? about this

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

java.lang

  • automatically import in your system

null:

  • empty object can defined as null

List:

    // List<Integer> newList = new ArrayList<Integer>();
    
    newList.size(); // length or size of the newList
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,098评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,213评论 2 380
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,960评论 0 336
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,519评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,512评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,533评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,914评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,574评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,804评论 1 296
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,563评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,644评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,350评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,933评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,908评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,146评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,847评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,361评论 2 342

推荐阅读更多精彩内容

  • 今天看了一个调解节目,对心理导师的一句话印象特别深,那就是:爸爸不是一天当成的。现在的80.90后大多是独生子女,...
    燕子回时阅读 201评论 0 2
  • 经过二天紧张激烈的比赛,我校一年一度的校运会在田径场隆重闭幕。在本次校运会上,我院取得团体总分第三的好成绩,突破...
    飛焱懿阅读 1,094评论 0 0
  • 9月10日,湖畔大学内部授课音频,首次在喜马拉雅电台公开上线。作为湖畔大学的校长,马云首次授课的内容为战略课。【校...
    司业飞阅读 3,120评论 1 6