Java基础概念

简介:

Variables & Fields 变量与字段

The Java programming language defines the following kinds of variables:

Instance Variables (Non-Static Fields) Technically speaking, objects store their individual states in “non-static fields”, that is, fields declared without the static keyword. Non-static fields are also known as instance variables because their values are unique to each instance of a class (to each object, in other words); the currentSpeed of one bicycle is independent from the currentSpeed of another.
实例变量,每个实例不同。

Class Variables (Static Fields) A class variable is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated. A field defining the number of gears for a particular kind of bicycle could be marked as static since conceptually the same number of gears will apply to all instances. The code static int numGears = 6; would create such a static field. Additionally, the keyword final could be added to indicate that the number of gears will never change.
类变量由static定义,静态变量,全局静态存储,类共享。

Local Variables Similar to how an object stores its state in fields, a method will often store its temporary state in local variables. The syntax for declaring a local variable is similar to declaring a field (for example, int count = 0;). There is no special keyword designating a variable as local; that determination comes entirely from the location in which the variable is declared — which is between the opening and closing braces of a method. As such, local variables are only visible to the methods in which they are declared; they are not accessible from the rest of the class.
局部变量定义于方法中,存储于栈里。

Parameters You’ve already seen examples of parameters, both in the Bicycle class and in the main method of the “Hello World!” application. Recall that the signature for the main method is public static void main(String[] args). Here, the args variable is the parameter to this method. The important thing to remember is that parameters are always classified as “variables” not “fields”. This applies to other parameter-accepting constructs as well (such as constructors and exception handlers) that you’ll learn about later in the tutorial.
参数,参数是方法签名中定义的变量,存储于堆栈。

The Java programming language uses both “fields” and “variables” as part of its terminology. Instance variables (non-static fields) are unique to each instance of a class. Class variables (static fields) are fields declared with the static modifier; there is exactly one copy of a class variable, regardless of how many times the class has been instantiated. Local variables store temporary state inside a method. Parameters are variables that provide extra information to a method; both local variables and parameters are always classified as “variables” (not “fields”). When naming your fields or variables, there are rules and conventions that you should (or must) follow.

变量 和字段是两种不同的东西。
变量包括局部变量参数,初始化于栈中
字段包括实例字段类字段,初始化于堆中

Naming 名字

命名规则基本与C一致

原子数据类型

byte,short,int,long,float,char
原子数据类型是语言内建支持的,不是从类创建的。
string不是原子类型,但是语言有特殊支持。

Array 数组

An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. You have seen an example of arrays already, in the main method of the “Hello World!” application. This section discusses arrays in greater detail.

 
  1. //数组可以这样声明,但不推荐
  2. int[] anArray;
  3. //数组初始化
  4. anArray = new int[10];
  5. //列表初始化
  6. int[] anArray = {
  7. 100, 200, 300,
  8. 400, 500, 600,
  9. 700, 800, 900, 1000
  10. };

多维数组

You can also declare an array of arrays (also known as a multidimensional array) by using two or more sets of brackets, such as String[][] names. Each element, therefore, must be accessed by a corresponding number of index values.

In the Java programming language, a multidimensional array is an array whose components are themselves arrays. This is unlike arrays in C or Fortran. A consequence of this is that the rows are allowed to vary in length, as shown in the following MultiDimArrayDemo program:

 
  1. class MultiDimArrayDemo {
  2. public static void main(String[] args) {
  3. String[][] names = {
  4. {"Mr. ", "Mrs. ", "Ms. "},
  5. {"Smith", "Jones"}
  6. };
  7. // Mr. Smith
  8. System.out.println(names[0][0] + names[1][0]);
  9. // Ms. Jones
  10. System.out.println(names[0][2] + names[1][1]);
  11. }
  12. }

The output from this program is:

Mr. Smith
Ms. Jones
Finally, you can use the built-in length property to determine the size of any array. The following code prints the array’s size to standard output:

System.out.println(anArray.length);

目录
相关文章
|
1月前
|
存储 安全 Java
Java面试题:深入探索Java内存模型,Java内存模型中的主内存与工作内存的概念,Java内存模型中的happens-before关系,volatile关键字在Java内存模型中的作用
Java面试题:深入探索Java内存模型,Java内存模型中的主内存与工作内存的概念,Java内存模型中的happens-before关系,volatile关键字在Java内存模型中的作用
20 1
|
12天前
|
安全 Java 编译器
Java 基础语法-面试题(53道)(基础概念+基础语法+流程控制)
Java 基础语法-面试题(53道)(基础概念+基础语法+流程控制)
37 18
|
1天前
|
设计模式 Java
常用设计模式介绍~~~ Java实现 【概念+案例+代码】
文章提供了一份常用设计模式的全面介绍,包括创建型模式、结构型模式和行为型模式。每种设计模式都有详细的概念讲解、案例说明、代码实例以及运行截图。作者通过这些模式的介绍,旨在帮助读者更好地理解源码、编写更优雅的代码,并进行系统重构。同时,文章还提供了GitHub上的源码地址,方便读者直接访问和学习。
常用设计模式介绍~~~ Java实现 【概念+案例+代码】
|
5天前
|
Java
Java 匿名函数的概念和写法
Java 匿名函数的概念和写法
7 1
|
5天前
|
JavaScript 前端开发 Java
java高质量数据流概念讲解,保证一篇文章帮助你搞懂概念!
【8月更文挑战第11天】java高质量数据流概念讲解,保证一篇文章帮助你搞懂概念!
13 0
java高质量数据流概念讲解,保证一篇文章帮助你搞懂概念!
|
5天前
|
缓存 前端开发 JavaScript
一篇文章助你搞懂java中的线程概念!纯干货,快收藏!
【8月更文挑战第11天】一篇文章助你搞懂java中的线程概念!纯干货,快收藏!
15 0
一篇文章助你搞懂java中的线程概念!纯干货,快收藏!
|
7天前
|
前端开发 Java
【前端学java】全网最通俗易懂的JAVA抽象概念(07)
【8月更文挑战第9天】全网最通俗易懂的JAVA抽象概念
17 2
|
27天前
|
Java 程序员 调度
Java中的多线程编程:概念、实现及性能优化
【5月更文挑战第85天】本文主要探讨了Java中的多线程编程,包括其基本概念、实现方式以及如何进行性能优化。首先,我们将介绍多线程的基本概念,然后详细讨论如何在Java中实现多线程,包括继承Thread类和实现Runnable接口两种方式。最后,我们将探讨一些提高多线程程序性能的策略,如使用线程池和减少同步开销等。
|
1月前
|
Java 开发者
【Java探索之旅】初识多态_概念_实现条件
【Java探索之旅】初识多态_概念_实现条件
43 16
|
1月前
|
Java 程序员
【Java探索之旅】继承概念_语法_父类的成员访问
【Java探索之旅】继承概念_语法_父类的成员访问
39 10