免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1270 | 回复: 0
打印 上一主题 下一主题

Thinking in Java - Everything is an Object [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-07-13 18:09 |只看该作者 |倒序浏览

               
                Although it is based on C++, Java is more of a "pure" object-oriented language.
You manipulate objects with references
    You treat everything as an object, using a single consistent syntax. Although you treat everything as an object, the indentifier you manipulate is actually a "reference" to an object.
You must create all the objects
    when you crete a reference, you want to connect it with a new object. You do so, in general, with the new keyword. new says, "make me a new one of these objects."
  Where storage lives
    1. Registers.This is the fastest storage because it exists in a place different from that of other storage: inside the processor.
    2. The stack. This lives in the general RAM area, but has direct support from the processor via its stack pointer. The stack pointer is moved down to create new memory and moved up to release that memory. Java objects themselves are not placed on the stack.
    3. The heap. This is a general-purpose pool of memory (also in the RAM area) where all Java objects live. Whenever you need to create an object, you simply write the code to create it using new, and the storage is allocated on the heap when that code is executed.
    4. Static storage. You can use the static keyword to specify that a particular element of an object is static, but Java objects themselves are never placed in static storage.
    5. Constant storage.
    6. Non-RAM storage.
  Special case: primitive types
Primitive type
Size
Minimum
Maximum
Wrapper type
boolean
-
-
-
Boolean
char
16-bit
Unicode 0
Unicode 2^16-1
Character
byte
8-bit
-128
+127
Byte
short
16-bit
-2^15
+2^15-1
Short
int
32-bit
-2^31
+2^31-1
Integer
long
64-bit
-2^63
+2^63-1
Long
float
32-bit
IEEE754
IEEE754
Float
double
32-bit
IEEE754
IEEE754
Double
void
64-bit
IEEE754
IEEE754
Void
    High-precision numbers
    Java includes two classes for performing high-precision arithmetic: BigInteger and BigDecimal. You can do anything with a BigInteger or BigDecimal that you can with an int or float, it's just that you must use method calls instead of operators.
  Arrays in Java
    When you create an array of objects, you are really creating an array of reference, and each of those references is automatically initialized to a special value with its own keyword: null. When Java sees null, it recognizes that the reference in question isn't pointing to an object.
    You can also create an array of primitives. Again, the compiler guarantees initialization because it zeroes the memroy for that array.
You never need to destroy an object
  Scoping
    Most procedural languages have the concept of scope. This determines both the visibility and lifetime of the names defined within that scope.
  Scope of objects
    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.
Creating new data types: class
  Fields and methods
    When you define a class, you can put two types of elements in your class: fields and methods. Each object keeps its own storage for its fields; the fields are not shared among objects.
    Default values for primitive memebers
    When a primitive data type is a member of a class, it is guaranteed to get a default value if you do not initialize it:
Primitive type
Default
boolean
false
char
'\u0000'(null)
byte
(byte)0
short
(short)0
int
0
long
0L
float
0.0f
double
0.0d
    This guarantee doesn't apply to "local" variables - those that are not fields of a class.
Methods, arguments, and return values
    Methods in Java determine the messages an object can receive. The fundamental parts of a method are the name, the arguments, the return type, and the body. Methods in Java can be created only as part of a class. A method can be called only for an object, and that object must be able to perform that method call. The act of calling a method is commonly referred to as sending a message to an object. Object-oriented programming is often summarized as simply "sending messages to objects."
  The argument list
    As in any situation in Java where you seem to be handing objects around, you are actually passing references.
Building a Java program
    There are several other issues you must understand before seeing your first Java program.
  Name visibility
    A problem in any programming languages is the control of names. To produce an unambiguous name for a library, the specifier used is not unlike an Internet domain name. In fact, the Java creators want you to use your Internet domain name in reverse since those are guaranteed to be unique.
  Using other components
    import tells the compiler to bring in a package, which is a library of classes you want to use.
  The static keyword
    You don't actually get anything until you create an object of that class with new, and at that point data storage is created an methods become available. But there are two situations in which this approach is not sufficient. 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.
Your first Java program
    At the beginning of each program file, you must place the import statement to bring in any extra classes you'll need for the code in that file. A certain library of classes named java.lang are automatically brought into every Java files.
Comments and embedded documentation
  Syntax
    All of the javadoc commands occur only with /** comments. The comments end with */ as usual. There are two primary ways to use javadoc: embed HTML, or use "doc tags." Standalone doc tags are commands that start with a '@' and palced at the beginning of a comment line. Inline doc tags can appear anywhere within a javadoc comment, also start with a '@' but are surrounded by curly braces.
    There are three "types" of comment documentation, which correspond to the element the comment precedes: class, variable, or method.
  Some example tags
    @see: referring to other classes
    {@link package.class#member label}
    {@docRoot}
    {@inheritDoc}
    @version
    @author
    @since
    @param
    @return
    @throws
    @deprecated
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/8554/showart_140283.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP