(原创笔记)加州伯克利大学CS61b数据结构(Java描述)一:对象

OOP(object-oriented programming):
object: a repository of data;

class: type of object.

method: procedure of function that operates on objects or class.

Inheritance: A class may inherit properties from a more general class.

Polymorphism: one method works on several class, even if the class need different implementations. 

object-oriented: each object knows its own class & methods.

Java
variable: you must declare them and their type.
eg: int x; <——java does 2 things:①allocate memory to store an integer, type int. ②names variable “x".
variable also used to reference objects.
2 ways to get classes:
① use one defined by somebody else, java has tons.
② define your own.
eg: String myString; <—— java store a reference to a string object. here, myString is just a variable not a object.
      myString = new String();
 2 steps:
     ①new String() is a constructor;
     ②assignment “=“ cause myString to reference to the object.

Java programs must be compiled before you can run them.
javac is the compiler for java. compile .java file to .class file.
run the .class file(without .class) to get your answer.
unlike python, you can directly type:” python xxxx.py” to run the program.
objects & constructors
String s = new String( ); // declare a string variable and point to a string object.
s = “ Yow!” ; //s is no longer point to an empty string object, it point to “Yow!”
String s2 = s; //copy what is in s(a reference to “Yow!”), then s2 is also referred to “Yow!"
s2 = new String(s); //make a new string called “Yow!” and s2 no more point the old “Yow!”. they are identical but they are different objects.
a very instructive e.g.:
     String s; // create string variable s which refers to NULL
     String s2 = s; // create string variable s2 with copy the what was in s an also refers to NULL
     s = “Yow!”; // s is now refer to a string “Yow!” but s2 still point to NULL
what if:
    ①s2 = “Yow!”; // java will check if there exist a same object, if it is(as this situation), s2 will refer to the same string which s refers to.
     ②s2 = new string(s); this will force java to create a different but identical string object as what s refers to;

3 string constructors:
①new String() construct an empty string(zero characters)
②”whatever"
③new string(s) takes a parameter s. makes copy of object that s references.

constructors  always have same name as their class. except “stuffinquotes” (the second one in string constructors)

Methods
s2 = s.toUppercase( ); <—— method call.
String s3 = s2.concat(“!!”); // this will return s2 + “!!” to a new object s3. here “!!” is a string object by constructor ②. double quote for string single quote for character.
String s4 = “*”.concat(s2).concat(“*”);//also can be short hand as s4 = “*” + s2 + “*”;
The object “Yow!” did not change.
String are immutable after constructed.

I/O classes & objects
Objects in System class for interacting with a user:
System.out is a printstream object that outputs to the screen.
System.in   is a inputstream object that reads from the keyboard.
readLine is defined on BufferedReader objects.
- How do we construct a BufferedReader? with an InputStreamReader.
- how do we construct an InputStreamReader? with an InputStream.
- how do we construct an InputStream? System.in is one.
- how to figure this out? online java API to search. —— > java.io

InputStream reads raw data.
InputStreamReader is compose into character(2byte long).
BufferedReader is compose into entire lines of text. 

e.g:

import java.io.*;

class SimpleIO {
     public static void main(string[] arg) throw Exception {
          BufferedReader keybd = new BufferedReader(new InputStreamReader(System.in));
          System.out.println(keybd.readLine( ));
     }
}

To use java libraries, other than java.lang, you should import them.
java.io includes InputStreamReader, BufferedReader, etc.
Java program always starts at a method called “main”. 

你可能感兴趣的:(java学习)