一些Java相关的

都是从《Thinking in Java》英文第四版中摘抄的

_______________________________________________________________________________________________________________________

变量初始化:

The default values are only 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, 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.

This guarantee doesn‘t apply to local variables--those that
are not fields of a class.

(也就是方法内部定义的变量,我试了一下,如果只声明不初始化也不使用的话,不会报错,会报警告,如果使用的话,编译时报错)

_______________________________________________________________________________________________________________________

sizeof:

Java has no "sizeof"

In C and C++, the sizeof() operator tells you the number of bytes allocated
for data items. The most compelling reason for sizeof() in C and C++ is for
portability(可移植性). Different data types might be different sizes on different
machines(参看http://www.zhihu.com/question/19580654), so the programmer must
discover how big those types are when performing operations that are sensitive
to size.For example, one computer might store integers in 32 bits, whereas
another might store integers as 16 bits. Programs could store larger values in
integers on first machine. As you might imagine, portability is a huge headache
for C and C++ programmers.

Java does not need a sizeof() operator for this purpose, because all the data
types are the same on all machines. You do not need to think about portability
on this level--it is designed into the language.

Java determines the size of each primitive type. These sizes don’t change
from one machine architecture to another as they do in most languages. This
size invariance is one reason Java programs are more portable than programs
in most other languages.

The size of the boolean type is not explicitly specified; it is only defined
to be able to take the literal values true or false.

_______________________________________________________________________________________________________________________

if while 的判断条件必须是boolean,不能是int

while(x = y) {
// ....
}

The programmer was clearly tryingto test for equivalence (==) rather than do
an assignment. In C and C++ the result of this assignment will always be true if
y is nonzero, and you’ll probably get an infinite loop. In Java, the result of
this expression is not a boolean, but the compiler expects a
boolean and won’t convert from an int, so it will conveniently give you a
compile-time error and catch the problem before you ever try to run the program.
So the pitfall never happens in Java. (The only time you won’t get a compiletime
error is when x and y are boolean, in which case x = y is a legal expression,
and in the preceding example, probably an error.)

All conditional statements use the truth or falsehood of a conditional
expression to determine the execution path. An example ofa conditional
expression is a == b. This uses the conditional operator == to see if the value
of ais equivalent to the value of b. The expression returns trueor false. Any of
the relational operators you’ve seen in the previous chapter can be used to
produce a conditional statement. Notethat Java doesn’t allow you to use a number
as a boolean, even though it’s allowed in C and C++ (where truth is nonzero and
falsehood is zero). If you want to use a non-booleanin a booleantest, such as
if(a), you must first convert it to a boolean value by using a conditional
expression, such as if(a != 0).

_______________________________________________________________________________________________________________________

goto:

Although goto is a reserved word in Java, it is not used in the language;
Java has no goto.

_______________________________________________________________________________________________________________________

return

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.

The returnkeyword has two purposes: It specifies what value a method will
return (if it doesn’t have a void return value) and it causes the current method
to exit, returning that value.

_______________________________________________________________________________________________________________________

constructor

The constructor is an unusual typeof method because it has no return value.
This is distinctly different from a voidreturn value, in which the methodreturns
nothing but you still have the option to make it return something else.
Constructors return nothing and you don’t have an option (the newexpression does
return a reference to the newly created object, but the constructor itself has
no return value).

The expression new Bird() creates a new object and calls the default
constructor, even though one was not explicitly defined. Without it, you would
have no method to call to build the object. However, if you define any
constructors (with or without arguments), the compiler will notsynthesize one
for you.

_______________________________________________________________________________________________________________________

overloaded 参数类型不同,返回值不同是不可以的

If the methods have the same name, how can Java know which method you mean?
There’s a simple rule: Each overloaded method must take a unique list of
argument types. If you think about this for a second, it makes sense. How else
could a programmer tell the difference between two methods that have the same
name, other than by the types of their arguments? Even differences in the
ordering of arguments are sufficient to distinguish two methods, although you
don’t normally want to take this approach because it produces
difficult-tomaintain code.

It is common to wonder, “Why only class names and method argument lists? Why
not distinguish between methods based on their return values?” For example,
these two methods, which have the same name and arguments, are easily
distinguished from each other:
void f() {}
int f() { return 1; }

This might work fine as longas the compiler could unequivocally determine the
meaning from the context(我试了,编译器报错了), as in int x = f( ). However, you can also
call a method and ignore the return value. This is often referred to as calling
a method for its side effect, since you don’t care about the return value, but
instead want the other effects of the method call. So if you call the method
this way:
f();
how can Java determine which f( ) should be called? And
how could someone reading the code see it? Because of this sort of problem, you
cannot use return value types to distinguish overloaded methods.

_______________________________________________________________________________________________________________________

this:

1. The thiskeyword—which can be used only inside a non-static method—produces
the reference to the object that the method has been called for.

2. The thiskeyword is also useful for passing the current object to another
method: (这个用法不熟悉)


 1 // : initialization/PassingThis.java
2 class Person {
3 public void eat(Apple apple) {
4 Apple peeled = apple.getPeeled();
5 System.out.println("Yummy");
6 }
7 }
8
9 class Peeler {
10 static Apple peel(Apple apple) {
11 // ... remove peel
12 return apple; // Peeled
13 }
14 }
15
16 class Apple {
17 Apple getPeeled() {
18 return Peeler.peel(this);
19 }
20 }
21
22 public class PassingThis {
23 public static void main(String[] args) {
24 new Person().eat(new Apple());
25 }
26 } /*
27 * Output: Yummy
28 */// :~

Appleneeds to call Peeler.peel( ), which is a foreign utility method that
performs an operation that, for some reason, needs to be external to
Apple(perhaps the external method can be applied across many different classes,
and you don’t want to repeat the code). To pass itself to the foreign method, it
must use this.

3. Calling constructors from constructors


 1 //: initialization/Flower.java
2 // Calling constructors with "this"
3 import static net.mindview.util.Print.*;
4
5 public class Flower {
6 int petalCount = 0;
7 String s = "initial value";
8
9 Flower(int petals) {
10 petalCount = petals;
11 print("Constructor w/ int arg only, petalCount= " + petalCount);
12 }
13
14 Flower(String ss) {
15 print("Constructor w/ String arg only, s = " + ss);
16 s = ss;
17 }
18
19 Flower(String s, int petals) {
20 this(petals);
21 // ! this(s); // Can’t call two!
22 this.s = s; // Another use of "this"
23 print("String & int args");
24 }
25
26 Flower() {
27 this("hi", 47);
28 print("default constructor (no args)");
29 }
30
31 void printPetalCount() {
32 // ! this(11); // Not inside non-constructor!
33 print("petalCount = " + petalCount + " s = " + s);
34 }
35
36 public static void main(String[] args) {
37 Flower x = new Flower();
38 x.printPetalCount();
39 }
40 } /*
41 * Output: Constructor w/ int arg only, petalCount= 47 String & int args default
42 * constructor (no args) petalCount = 47 s = hi
43 */// :~

The constructor Flower(String s, int petals)shows that, while you can call
one constructor using this, you cannot call two. In addition, the constructor
call must be the first thing you do, or you’ll get a compiler error message.

In printPetalCount( ) you can see that the compiler won’t let you call a
constructor from inside any method other than a
constructor.(构造函数中使用this调用构造函数,不能在非构造函数中调用构造函数)

4. This example also shows another way you’ll see thisused. Since the name of
the argument s and the name of the member data sare the same, there’s an
ambiguity. You can resolve it using this.s, to say that you’re referring to
the member data.

_______________________________________________________________________________________________________________________

Static:

With the this keyword in mind, you can more fully understand what it means to
make a method static. It means that there is no this for that particular method.
You cannot call non-static methods from inside static methods(although the
reverse is possible), and you can call a static method for the class itself,
without any object. In fact, that’s primarily what a staticmethod is for. It’s
as if you’re creating the equivalent of a global method. However, global methods
are not permitted in Java, and putting the static method inside a class allows
it access to other static methods and to static fields.

Some people argue that static methods are not object-oriented, since they do
have the semantics of a global method; with a static method, you don’t send a
message to an object, since there’s no this. This is probably a fair argument,
and if you find yourself using a lot of static methods, you should probably
rethink your strategy. However, statics are pragmatic, and there are times
when you genuinely need them, so whether or not they are “proper OOP”
should
be left to the theoreticians.

When you say something is static, it means that particular field 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
static field. With ordinary, non-static fields and methods, you must create an
object and use that object to access that fieldor method, since non-static
fields 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 can‘t 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).

一些Java相关的

时间: 2024-10-13 11:47:02

一些Java相关的的相关文章

SAE Java相关问题小结

转自:http://blog.csdn.net/bhq2010/article/details/8580412 Sae中使用的servlet容器是jetty7.4.x 我想在web.xml中配置一个自己编写的servlet,实现web启动时的初始化工作,但是总是出现各种问题,下面总结了一下在sae中使用java的一些注意事项: 1.在eclipse中开发java web项目时,我总喜欢直接把需要的jar包复制到WEB-INF/lib下,但在开发sae项目时,最好把需要的jar包放到usr lib

java相关的面试题目

1.java/c++/ruby/python集中语言的对比 java和c++ java和ruby java和python java相关的面试题目,布布扣,bubuko.com

Java相关配置合集

Java环境变量配置: 1.安装JDK,安装过程中可以自定义安装目录等信息,例如我们选择安装目录为C:\java\jdk1.6.0_08: 2.安装完成后,右击“我的电脑”,点击“属性”: 3.XP选择[高级]选项卡,WIN7选择[高级系统设置] ,然后点击“环境变量”: 4.在“系统变量”中新建变量名为:JAVA_HOME,变量值为:指明JDK安装路径,就是刚才安装时所选择的路径 C:\java\jdk1.6.0_08: 点确定 5.在“系统变量”找到path这个变量,选中后点编辑,鼠标放在变

Java相关证书,你考过哪个?

每一个大学生都为CET-4,CET-6考前突击: 每一个会计专业的学生都对注册会计师证心向往之: 每一个法律专业的学生都曾为司考废寝忘食: 那每一个学习Java的学生呢?那些与Java相关的证书,哪一个是你既定目标? SCJP sun certificated java programmer (SCJP) ,一种Java认证考试 对于Java程序设计员,Sun推出两项认证:Sun Certificated Java Programmer (SCJP)和Sun Certificated Java

每天五个java相关面试题(8)--spring篇

首先呢,假设有从事前端开发的大神或者准备从事前端开发的小伙伴无意看到我这篇博客看到这段文字欢迎加我的QQ:[ 845415745 ].即将走入社会的菜鸟大学生有关于前端开发的职业问题想请教或者一起探讨一下,谢谢谢谢. 今天依然不变的每日五个 1使用Spring框架的优点是什么? 答: 轻量: Spring 是轻量的,主要的版本号大约2MB. 控制反转: Spring通过控制反转实现了松散耦合,对象们给出它们的依赖.而不是创建或查找依赖的对象们. 面向切面的编程(AOP): Spring支持面向切

JAVA相关知识复习

1.HTTP访问方式: GET.POST.HEAD.DELETE.TRACE.PUT.OPTIONS HEAD表示查询文档头信息,服务器会返回文件类型.长度.最后修改时间等信息,该方式很少被使用. GET方式常用来查询信息:提交数据不能超过256个字符(URL总长度不能超过255个字符).提交的查询内容java是显示在浏览器地址栏中的. POST方式提交数据,数据不在浏览器地址栏中显示.发送的命令需要提供提交的数据类型和长度.常用来提交表单数据.提交的内容长度不受限制. 数据类型有两种:一种是普

JAVA 相关资料

在技术方面无论我们怎么学习,总感觉需要提升自已不知道自己处于什么水平了.但如果有清晰的指示图供参考还是非常不错的,这样我们清楚的知道我们大概处于那个阶段和水平. Java程序员 高级特性 反射.泛型.注释符.自动装箱和拆箱.枚举类.可变 参数.可变返回类型.增强循环.静态导入 核心编程 IO.多线程.实体类. 集合类.正则表达式. XML和属性文件 图形编程 AWT(Java2D/JavaSound/JMF).Swing.SWT.JFace 网路编程 Applet.Socket/TCP/UDP.

Spring: Jsp+Java 相关知识整理 (十三)

1. 在Java中获取 .properties 文件的路径 (src/main/resources 下) ProjectName |---src/main/java |---src/main/resources |---test.properties package xxx.yyy; public class Utils { private String filePath = Utils.class.getClassLoader().getResource("test.properties&qu

Java相关思维导图分享

非常多朋友都给我发私信希望获得一份Java知识的思维导图,我来不及一一答复.原先是给大家一个百度网盘的链接分享,大家能够自己去下载,可是不知道云盘还能用多久.把相关资源转移到了QQ的群共享中.须要的朋友能够參考置顶帖.增加"Java技术交流"群获取相关资源,给大家带来的不便请原谅,可是保证思维导图中的内容非常的全,包含Java基础.高级.Web.Spring.Hibernate.MyBatis.Oracle等内容.请加群下载,谢谢!