Thinking in Java (Chapter 2)
===
###### tags: `Java` `OOP`
1. Java references are much more akin to C++ references than to pointers in their syntax.
2. Storage
(1)Registers: Fastest, in the processor, in Java no evidence that it exists, in C/C++ you can suggest
(2)Stack: Second fastest, only reference of object is there, since you have to decide the lifetime of object hence not flexible, it is in RAM(random-access-memory)
(3)Heap: Java object is being stored here, more flexibility but takes more time to allocate and clean up, also in RAM(random-access-memory)
(4)ROM(read-only-memory):Constant value is stored here, like string pool
(5)Embedded system, Contant value can be placed here
(6)Streamed objects: Objects are turned into streams of bytes, generally to be sent to another machine
(7)Persistent objects: Objects are placed on disk so they will hold their state even when the program is terminated
3. Primitive type
| Primitive type | Size | Minimum | Maximum | Wrapper type |
| -------------- | ------- | --------- | -------------- | ------------ |
| boolean | - | - | - | Boolean |
| char | 16 bits | Unicode 0 | Unicode 216- 1 | Character |
| byte | 8 bits | -128 | +127 | Byte |
| short | 16 bits | -2^15 | +2^15-1 | Short |
| int | 32 bits | -2^31 | +2^31-1 | Integer |
| long | 64 bits | -2^63 | +2^63-1 | Long |
| float | 32 bits | IEEE754 | IEEE754 | Float |
| double | 64 bits | IEEE754 | IEEE754 | Double |
| void | - | - | - | Void |
4. The “wrapper” classes for the primitive data types allow you to make a non-primitive object on the heap to represent that primitive type,autoboxing will convert from primitive to wrapper mode automatically or backwards
5. High-precision arithmetic: BigInteger, BigDecimal(detail on JDK documentation for constructor and methods)
(1)BigInteger supports arbitrary-precision integers. This means that you can accurately represent integral values of any size without losing any information during operations.
(2)BigDecimal is for arbitrary-precision fixed-point numbers; you can use these for accurate monetary calculations, for example.
6. Array:
(1)C/C++ access array outside memory block or uninitialize can cause unpredictable result
(2)Java is guranteed to be initialized plus can't be accessed outside its range
7. The range checking comes at the price of having a small amount of memory overhead on each array as well as verifying the index at run time, but the assumption is that the safety and increased productivity are worth the expense
8. When Java sees **null**, it recognizes that the reference in question isn’t pointing to an object. You must assign an object to each reference before you use it, and if you try to use a reference that’s still null, the problem will be reported at run time
9. The C and C++ ability to “hide” a variable in a larger scope is not allowed, because the Java designers thought that it led to confusing programs.
10. 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.This eliminates a certain class of programming problem: the so-called “memory leak,” in which a programmer forgets to release memory.
11. Fields (sometimes called data members), and methods (sometimes called member functions). a period (dot)
12. Default value for prmitive members
| boolean | false |
| ------- | -------------- |
| char | `\u0000`(null) |
| byte | (byte)0 |
| short | (short)0 |
| int | 0 |
| long | 0L |
| float | 0.0f |
| double | 0.0d |
13. The default values are what Java guarantees when the variable is used as a member of a class.This guarantee doesn’t apply to “local” variables—those that are not fields of a class. You get a compile-time error telling you the variable might not have been initialized. (Many C++ compilers will warn you about uninitialized variables, but in Java these are errors.)
14. Static methods can be called for the class, without an object
15. Function for C/C++, Method for Java
16. Object-oriented programming is often summarized as simply “sending messages to objects.”
17. If you’ve given a non-void return type, then the compiler will force you (with error messages) to return the appropriate type of value regardless of where you return.
18. C++ still allows global data and global functions, so clashing is still possible. To solve this problem, C++ introduced **namespaces** using additional keywords.
19. Java creators want you to use your Internet domain name in reverse since domain names are guaranteed to be unique
20. Import tells the compiler to bring in a package, which is a library of classes. (In other languages, a library could consist of functions and data as well as classes, but remember that all code in Java must be written inside a class.)
21. If you want to have only a single piece of storage for a particular field, regardless of how many objects of that class are created, or even if no objects are created.
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
22. class StaticTest {
static int i = 47;
}
Now even if you make two StaticTest objects, there will still be only one piece of storage for
StaticTest.i. Both objects will share the same i.
23. 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
24. Using the class name is the preferred way to refer to a static variable. Not only does it emphasize that variable’s static nature, but in some cases it gives the compiler better opportunities for optimization.
25. An important use of static for methods is to allow you to call that method without creating an object. This is essential, as you will see, in defining the main( ) method that is the entry point for running an application
26. There’s a certain library of classes that are automatically brought into every Java file: java.lang
27. The name of the class is the same as the name of the file. When you’re creating a standalone program such as this one, one of the classes in the file must have.That class must contain a method called main( )
28. 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
29. Java Developer’s Kit (JDK) from Sun
30. Set up your computer’s path information so that it will find javac and java
31. IBM’s “jikes” compiler is a common alternative, as it is significantly faster than Sun’s javac
32. build.xml in contains “Ant” commands for automatically building the files
33. The tool to extract the comments is called Javadoc, and it is part of the JDK installation.From the Java compiler it looks for special comment tags that you put in your programs
34. You can write your own Javadoc handlers, called doclets
35. There are two primary ways to use Javadoc: Embed HTML or use “doc tags.” Standalone doc tags are commands that start with an ‘@’ and are placed at the beginning of a comment line