[Java Basics] Stack, Heap, Constructor

Good about Java:

friendly syntax, memory management[GC can collect unreferenced memory resources], object-oriented features, portability.

Stack

Stores method invocations, local variables(include object reference, but the object itself is still stored in heap).

If you look at the stack trace, the top method is the one being called, and once it is finished, it will be removed out of the stack.

Heap

Stores all objects(include instance variables in the class).

.hashcode() returns the memory address of the object, so no same hashcode for two objects.

But when the Class is value object, you could override .hashcode() and .equals().

Constructor

Default parameterless constructor: only generated by compiler when there is no your constructor.

Parent constructor: if parent class has default constructor, compiler will automatically call it without .super(). But if not, or you want to call the parent constructor with parameters, you need to call .super() explicitly.

Overloaded constructor: use .this(...) at the first line.

File I/O

There are two streams: Character stream & Binary stream

Character stream:

to read/write human readable files, like .txt .xml .html

Class: FileReader/Writer, more advanced: BufferedReader/Writer

Binary stream:

to read/write machine readable files, like .exe .class .obj

Class: FileInput/OutputStream, more advanced for Object: ObjectInput/OutpoutStream(.writeObject() method).

Immutable Class

Instance could not be modified.

How to: private final fields, final class, no setter methods, ensure exlusive access to mutable components.

[Java Basics] Stack, Heap, Constructor,布布扣,bubuko.com

时间: 2024-10-27 11:43:47

[Java Basics] Stack, Heap, Constructor的相关文章

Java 栈(stack)与堆(heap) 详解

Java 栈(stack)与堆(heap)1.概念    栈(stack)与堆(heap)都是Java用来在Ram中存放数据的地方.与C++不同,Java自动管理栈和堆,程序员不能直接地设置栈或堆.栈,堆的数据结构    栈就像装数据的桶或箱子      它是一种具有后进先出性质的数据结构,也就是说后存放的先取,先存放的后取.      这就如同我们要取出放在箱子里面底下的东西(放入的比较早的物体),我们首先要移开压在它上面的物体(放入的比较晚的物体).    堆像一棵倒过来的树      而堆

[Java Basics] multi-threading

1, Interrupt Interruption in Java is not pre-emptive. Put another way both threads have to cooperate in order to process the interrupt properly. If the target thread does not poll the interrupted status the interrupt is effectively ignored. Polling o

[Java Basics] Reflection

For every type of object, the Java virtual machine instantiates an immutable instance of java.lang.Class which provides methods to examine the runtime properties of the object including its members and type information. Class also provides the abilit

[Java Basics] Collection

除了Java collection class/interface外,方便的有Google guava的utility class: Lists/Sets/Maps/Queues, 用它们可以方便地创建List等object. List<String> list = Lists.newArrayList(); or Lists.newArrayList("1", "2"); [Java Basics] Collection,布布扣,bubuko.com

Java 语法 索引 ----- 构造函数(constructor)

重载 class MyRectangle{ int x, y; public MyRectangle() { x = 10; y = 20; } public MyRectangle(int a) { x = a; y = a; } public MyRectangle(int a, int b) { x = a; y = b; } } Constructor chaining public MyRectangle() { this(10,20); } public MyRectangle(in

java.util.Stack类中的peek()方法

java.util.stack类中常用的几个方法:isEmpty(),add(),remove(),contains()等各种方法都不难,但需要注意的是peek()这个方法. peek()查看栈顶的对象而不移除它. import java.util.Stack; public class MyStack1 { private Stack<Integer> stackData; private Stack<Integer> stackMin; public MyStack1(){ t

java集合类——Stack栈类

今日走读代码时,遇到stack栈类,特查看java的API文档,总结如下: Stack继承Vector类. 栈的特点是后进先出. API中Stack自身的方法不多,基本跟栈的特点有关. 现附上例子,后续继续总结 /** * @作者 whs * @创建日期 2015年2月4日 * @版本 V 1.0 */ package thread.pool; import java.util.Stack; public class StackExam { public static void main(Str

恶补java(十一)-------Stack类的使用

package com.gc.Stack; /** * java中stack的使用方法,堆栈是一种"后进先出"(LIFO)的数据结构,只能在一端进行插入(称为"压栈")或删除(称为"出栈")数据的操作. * Java中,使用java.util.Stack类的构造方法创建对象 * public class Stack extends vector * 构造方法:public Stack()创建一个空Stack * 1.public push(ite

Java的Stack类实现List接口真的是个笑话吗

今天在网上闲逛时看到了这样一个言论,说“Java的Stack类实现List接口的设计是个笑话”. 当然作者这篇文章的重点不是这个,原本我也只是一笑置之,然而看评论里居然还有人附和,说“Java那种Stack的设计作为笑话,差不多可以算公案了”,我就有点不淡定了,为什么.什么时候“作为笑话”的并且“差不多可以算公案”了呢? 因此我决定写一篇文章来谈谈这个问题. 接口是什么 狭义地讲,接口就是一个类所定义的方法(方法名.参数.返回值).一个类提供了Foo方法,其他类就可以调用它.广义上讲,接口可以理