Everything Is an Object


You manipulate objects with references

  • Each programming language has its own means of manipulating elements in memory.
  • Are you manipulating the element directly, or are you dealing with some kind of indirect representation (a pointer in C or C++) that must be treated with a special syntax?
  • Although you treat everything as an object, the identifier you manipulate is actually a “reference” to an object.
  • Have a reference doesn’t mean there’s necessarily an object connected to it.

You must create all the objects

  • The keyword new says, “Make me a new one of these objects.”

Where storage lives

  • There are five different places to store data:

    1.Registers: registers are allocated as they are needed.

    2.The Stack: The Java system must know, while it is creating the program, the exact lifetime of all the items that are stored on the stack. Java objects themselves are not placed on the stack.

    3.The heap: The compiler doesn’t need to know how long that storage must stay on the heap. Whenever you need an object, you simply write the code to create it by using new, and the storage is allocated on the heap when that code is executed.

    4.Constant storage: Constant values are often placed directly in the program code.

    5.Non-RAM storage:If data lives completely outside a program, it can exist while the program is not running, outside the control of the program. The two primary examples of this are Streamed Objects and Persistent Objects.

Special case: primitive types

  • Instead of creating the variable by using new, an “automatic” variable is created that is not a reference.
  • These sizes don’t change from one machine architecture to another as they do in most languages.
  • All numeric types are signed.
  • The “wrapper” classes for the primitive data types allow you to make a non-primitive object on the heap to represent that primitive type.
  • Java includes two classes for performing high-precision arithmetic: BigInteger and BigDecimal. Neither one has a primitive analogue.
  • 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

  • If a program accesses the array outside of its memory block or uses the memory before initialization, there will be unpredictable results.
  • A Java array is guaranteed to be initialized and cannot be accessed outside of its range.
  • When you create an array of objects, you are really creating an array of references, and each of those references is automatically initialized to a special value with its own keyword: null.

You never need to destroy an object

Scoping

  • A variable defined within a scope is available only to the end of that scope.
  • The C and C++ ability to “hide” a variable in a larger scope is not allowed in Java.

Scope of Objects

  • The reference s vanishes at the end of the scope. However, the String object that s was pointing to is still occupying memory.
  • Java has a garbage collector, which looks at all the objects that were created with new and figures out which ones are not being referenced anymore.

Creating new data types: class

Fields and methods

  • You can put two types of elements in your class: fields ( data members), and methods(member functions).
  • A field is an object of any type that you can talk to via its reference, or a primitive type.
  • If it is a reference to an object, you must initialize that reference to connect it to an actual object.

Default values for primitive members

  • 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.

Methods, arguments, and return values

  • Methods in Java determine the messages an object can receive.
  • The method name and argument list ( the signature of the method) uniquely identify that method.
  • This act of calling a method is commonly referred to as sending a message to an object.
  • What you must specify in the argument list are the types of the objects to pass in and the name to use for each one.
  • As in any situation in Java where you seem to be handing objects around, you are actually passing references.
  • A program is just a bunch of objects with methods that take other objects as arguments and send messages to those other objects.

Building a Java program

Name visibility

  • To produce an unambiguous name for a library, the Java creators want you to use your Internet domain name in reverse since domain names are guaranteed to be unique.
  • This mechanism means that all of your files automatically live in their own namespaces, and each class within a file must have a unique identifier.

Using other components

  • import tells the compiler to bring in a package, which is a library of classes.

The static keywords

  • You want to have only a single piece of storage for a particular field, or you need a method that isn’t associated with any particular object of this class.
  • When you say something is static, it means that particular field or method is not tied to any particular object instance of that class.
  • Class Data and Class Methods, meaning that the data and methods exist only for the class as a whole, and not for any particular objects of the class.
  • 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.
  • An important use of static for methods is to allow you to call that method without creating an object.

Your first Java program

  • When you’re creating a standalone program such as this one, one of the classes in the file must have the same name as the file.That class must contain a method called main( ).
  • The args won’t be used in this program, but the Java compiler insists that they be there because they hold the arguments from the command line.

Comments and embedded documentation.

Comment documentation

  • Because of Javadoc, you have a straightforward standard for creating documentation, so you can expect or even demand documentation with all Java libraries.

Syntax

  • Javadoc will process comment documentation for only public and protected members.

Embedded HTML

  • Javadoc passes HTML commands through to the generated HTML document.

Thinking in Java--笔记(2)的更多相关文章

  1. Effective Java笔记一 创建和销毁对象

    Effective Java笔记一 创建和销毁对象 第1条 考虑用静态工厂方法代替构造器 第2条 遇到多个构造器参数时要考虑用构建器 第3条 用私有构造器或者枚举类型强化Singleton属性 第4条 ...

  2. java笔记00-目录

    --2013年7月26日17:49:59 学习java已久,趁最近有空,写一个总结: java笔记01-反射:

  3. java笔记整理

    Java 笔记整理 包含内容     Unix Java 基础, 数据库(Oracle jdbc Hibernate pl/sql), web, JSP, Struts, Ajax Spring, E ...

  4. 转 Java笔记:Java内存模型

    Java笔记:Java内存模型 2014.04.09 | Comments 1. 基本概念 <深入理解Java内存模型>详细讲解了java的内存模型,这里对其中的一些基本概念做个简单的笔记 ...

  5. servlet(6) - servlet总结 - 小易Java笔记

    垂阅前必看: 这都是我总结的我觉得是学习servlet应该掌握的,我在学习期间也做了一个博客项目来让所学的知识得以巩固.下面就是博客项目链接.前面的servlet相关的笔记总汇,还有就是我把觉得在学习 ...

  6. Java笔记 —— 继承

    Java笔记 -- 继承 h2{ color: #4ABCDE; } a{ text-decoration: none!important; } a:hover{ color: red !import ...

  7. Java笔记 —— 方法重载和方法重写

    Java笔记 -- 方法重载和方法重写 h2{ color: #4ABCDE; } a{ text-decoration: none !important; } a:hover{ color: red ...

  8. Java笔记 —— 初始化

    Java笔记 -- 初始化 h2{ color: #4ABCDE; } a{ text-decoration: none !important; } a:hover{ color: red !impo ...

  9. Java笔记 —— this 关键字

    Java笔记 -- this 关键字 h2{ color: #4ABCDE; } a{ color: blue; text-decoration: none; } a:hover{ color: re ...

  10. Java 笔记 —— java 和 javac

    Java 笔记 -- java 和 javac h2{ color: #4ABCDE; } a{ text-decoration: none !important; } a:hover{ color: ...

随机推荐

  1. 【面试题】TB

    动态库与静态库区别: 堆栈区别,这样区分的意义: 不用第三个变量,交换两个变量的值: 链表公共节点: 判断链表是否有环: 常用排序算法,哪个可以链表实现: 哪科学的好,感兴趣: 项目介绍,遇到的最大问 ...

  2. 树状数组+STL FZU 2029 买票问题

    题目传送门 题意:中文题面 分析:隔了一个考试周再做,开始没有什么思路,感觉能用线段树/树状数组维护,树状数组维护最小值不会去写线段树,结果超时.后来发现只要维护前缀几个人以及用优先队列/set维护最 ...

  3. mysql case then使用

    表的创建CREATE TABLE `lee` (`id` int(10) NOT NULL AUTO_INCREMENT, `name` char(20) DEFAULT NULL, `birthda ...

  4. oracle中利用trigger,sequence自动生成ID

    http://zhanghong.iteye.com/blog/865937 1. 首先创建数据库表 SQL> create table customer( 2  id number(8) no ...

  5. ACM 喷水装置(一)

    喷水装置(一) 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 现有一块草坪,长为20米,宽为2米,要在横中心线上放置半径为Ri的喷水装置,每个喷水装置的效果都会让以 ...

  6. objective-c 多线程注意的问题

    1.资源竞争:当每个线程都去访问同一段内存时,会导致所谓i资源竞争问题,这时候可以通过“@synchronized block”将实例变量包围起来,创建一个互斥锁, 这样你就可以确保在互斥锁中的代码一 ...

  7. ACM: FZU 2148 Moon Game - 海伦公式

     FZU 2148  Moon Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  8. iframe高度自适应内容

    JS自适应高度,其实就是设置iframe的高度,使其等于内嵌网页的高度,从而看不出来滚动条和嵌套痕迹.对于用户体验和网站美观起着重要作用. 如果内容是固定的,那么我们可以通过CSS来给它直接定义一个高 ...

  9. codeforces589J 简单dfs,队列

    J. Cleaner Robot time limit per test 2 seconds memory limit per test 512 megabytes input standard in ...

  10. linux命令之 top, free,ps

    linux终端查看cpu和内存使用情况 t一.op进入全屏实时系统资源使用信息查看 PID:进程的ID USER:进程所有者 PR:进程的优先级别,越小越优先被执行 NInice:值 VIRT:进程占 ...