Thinking Java 笔记(一)

我在学校的时候就没有认真学习过Java,在开始学android的时候,也没有时间及时补上,现在有点时间,就觉得自己的基础太差了,所以开始把Thinking java的电子书拿出来读一读。因为英语不好,所以读得很慢。我读的是英文版的。我觉得不要因为自己的英语不好,就老读中文版的,其实先进IT技术都是从国外先发明的,所以你想要领略地道的东西还是读读英文的好。
我决定把在读的过程中觉得需要记住的地方摘录下来,方便以后复习。嘿嘿!

1, [color=red]Everything is an object.[/color]

2,[color=red]Manipulate objects with references[/color]

3,[color=red]Each object keeps its own storage for its fields; the fields are not shared among objects.[/color]

4,[color=red]Scope of objects[/color]
Java objects do not have the same lifetimes as primitives. When you
create a Java object using new, it hangs around past the end of the scope.
Thus if you use:
{
String s = new String("a string");
} // End of scope
the reference s vanishes at the end of the scope. However, the String
object that s was pointing to is still occupying memory. In this bit of code, there is no way to access the object because the only reference to it is out of scope.


5,[color=red]Default values for primitive members[/color]
Note carefully that the default values are what Java guarantees when the variable is used as a member of a class. This ensures that member
variables of primitive types will always be initialized (something C++
doesn’t do), reducing a source of bugs. However, this initial value may not be correct or even legal for the program you are writing. It’s best to always explicitly initialize your variables.
[color=blue]This guarantee doesn’t apply to “local” variables—those that are not fields of a class.[/color] Thus, if within a method definition you have:
int x;
Then x will get some arbitrary value (as in C and C++); it will not
automatically be initialized to zero.


6,[color=red]Return keyword[/color]
You can also see the use of the return keyword, which does two things.
First, it means “leave the method, I’m done.” Second, if the method
produces a value, that value is placed right after the return statement.


7,[color=red]Name visibility[/color]
A problem in any programming language is the control of names.how do you distinguish one name from another and prevent the two names from “clashing?” To solve this problem, C++ introduced
namespaces using additional keywords In fact, the Java creators want you to use your Internet domain name in reverse since those are guaranteed to be unique.
[color=blue]After your reversed domain name, the dots are intended to represent subdirectories[/color]
so now the entire package name is lowercase.

8,[color=red]Using other components[/color]
This is accomplished by telling the Java compiler exactly what classes you want
using the import keyword. import tells the compiler to bring in a
package, which is a library of classes. (In other languages, a library could
Chapter 2: Everything is an Object 101
consist of functions and data as well as classes, but remember that all
code in Java must be written inside a class.)

9,[color=red]The static keyword[/color]
One is if you want to have only one piece of storage for a particular piece of data, regardless of how many objects are created, or even if no objects are created. The other is if you need a method that isn’t associated with any particular object of this class. That is, you need a method that you can call even if no objects are created. You can achieve both of these effects with
the static keyword. When you say something is static, it means that data or method is not tied to any particular object instance of that class. So even if you’ve never created an object of that class you can call a static method or access a piece of static data. With ordinary, non-static data and methods you must create an object and use that object to access the data or method, since non-static data and methods must know the particular object they are working with. Of course, since static methods don’t need any objects to be created before they are used, they cannot directly access non-static members or methods by simply calling those other members without referring to a named object (since non-static members and methods must be tied to a particular object).


10,The name of the class is the same as the name of the file. When you’re creating a stand-alone program such as this one, one of the classes in the file must have the same name as the file. (The compiler complains if you don’t do this.)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值