Home > Java > javaTutorial > body text

Summary of Java programming ideas

巴扎黑
Release: 2017-07-17 13:57:35
Original
1995 people have browsed it

This chapter introduces the basic components of Java programs and realizes that almost everything in Java is an object.

Everything is an object

##Directory:

  • 2.1 Manipulate objects with references

  • 2.2 All objects must be created by you

  • 2.3 Never need to destroy objects

  • 2.4 Create new data types: classes

  • 2.5 Methods, parameters and return values

  • 2.6 Building a Java program

  • 2.7 Your first Java program

  • 2.8 Comments and embedded documentation

  • 2.9 Coding style

2.1 Manipulating objects with references

Everything is regarded as an object, and the manipulated identifier is actually a "reference" of the object. The remote control (reference) controls the TV (object). If you want to control the TV, you only need to use the remote control, and the remote control exists independently.

2.2 All objects must be created by you

Once you create a reference, you want it to be associated with a new Objects are associated, and the new operator is usually used to achieve this purpose.

String s = new String("Jiancheng");
Copy after login

 

五个不同的地方可以存储数据:

  • 寄存器:位于CPU内部,寄存器的数量极其有限,所以寄存器根据需求进行分配,速度最快。

  • 堆栈:位于通用RAM(随机访问寄存器)中,Java编译器必须知道存储在堆栈内所有数据的大小和生命周期,“堆栈指针”向下移动则分配新内存,向上移动则释放内存,速度仅次于寄存器,基本数据类型和引用存放在此。

  • 堆:位于RAM区,用于存放所有Java对象,编译器不用知道数据大小和生命周期.

  • 常量存储:常量值通常直接存放在程序代码内部。

  • 非RAM存储:完全存活在程序之外,不受程序的任何控制,例子是流对象持久化对象,在流对象中,对象转换成字节流,通常被发送到另一台机器。在"持久化"对象中,对象被存放于硬盘上,即使程序终止,它们仍可以保持自己的状态。

 9种基本类型:

 

boolean类型所占内存储空间的大小没有明确指定,仅定义为能够取字面值true或false。

高精度数字:

  • BigInteger:支持任意精度的整数

  • BigDecimal:支持任意精度的定点数

操作与对基本类型所能执行的操作相似,但必须以方法调用方式取代运算符方式来实现。

Java中的数组:
数组会被初始化,而且不能在它的范围之外被访问。这种范围检查,是以每个数组上少量的内存开销及运行时的下标检查为代价(换来安全性和效率)。

 2.3 永远不需要销毁对象

作用域:

------------------------------------------------------------------

对象的作用域:

Java的作用和基本类型差别很大,new创建一个Java对象后,它可以存活于作用域之外。只要你需要这个对象,就会一直保留下去,直到垃圾回收器辨别到该对象不会再被引用并释放该对象的内存空间,这样就消除了内存泄漏的问题。

2.4 创建新的数据类型:类

类决定了某一对象的外观与行为,确定了对象的类型。

基本成员默认值:

当变量作为类的成员使用时,Java才确保给定其默认值,来确保基本类型成员变量得到初始化(初始值可能不是你想要的,最好自己初始化)。注意默认初始化的方法不适用于非某个类的字段变量,忘记初始化,Java会在编译时给你返回一个错误。

2.6 构建一个Java程序

static关键字:

下面两种情况需要使用static

  • 只想为某特定域分配单一存储空间,而不去考虑究竟要创建多少对象。

  • 某个方法不与包含它的类的任何对象关联在一起,就是没有创建对象,也能够调用这个方法。

class StatiTest{  static i = 47; //可以直接StatiTest.i这样调用
}
Copy after login

StatiTest st1 = new StatiTest();
StatiTest st2 = new StatiTest();
where st1.i and st2.i point to the same storage space, so they have the same value.

static acts on a method. The difference is that it can be called without creating any object, class.method().

2.9 Coding style

## Camel case style:

  • class, the first letter of each word is capitalized

  • Methods, fields and objects, etc., the first letter of the first word is capitalized Lower case, the remaining first letters are capitalized

Summary:

This chapter is easy to understand and is useful for Java I have an overall understanding of language and some of its basic ideas, and I have been exposed to most knowledge points, but it is easy to forget the details, so it is okay to read more.


The above is the detailed content of Summary of Java programming ideas. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!