JavaTutorials之Variables

我们通过之前的学习知道,一个Object存储它的State在fields中,但是我们对于fields可能有一些问题:

  • 命名一个field的规则和习惯是什么
  • 除了int还有其他什么数据类型
  • 当field被声明的时候需要初始化嘛
  • 当fields没有被显示初始化的时候会被分配一个默认值嘛

Java编程语言定义了如下的变量类型:

  • 实例变量 Instance Variables (Non-Static Fields):实例变量是不加static的,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) currentSpeed。由类产生的每个对象的Instance Variable是不同的。
  • 类变量 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.就是这些由类产生的对象的这一种变量数值都是一样的。
  • 本地变量 Local Variables: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:public static void main(String[] args).

Primitive Data Types

The Java programming language is statically-typed, which means that all variables must first be declared before they can be used.
Java是静态类型的语言,所有的变量在使用之前必须被声明。
在这里插入图片描述
在这里插入图片描述
Arrays
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. 数组建立的时候它的长度就已经确定了。
在这里插入图片描述
Like declarations for variables of other types, an array declaration has two components: the array’s type and the array’s name. An array’s type is written as type[], where type is the data type of the contained elements; the brackets are special symbols indicating that this variable holds an array. The size of the array is not part of its type (which is why the brackets are empty). [ ]预示了这个变量hold了一个数组。

数组操作:
For your convenience, Java SE provides several methods for performing array manipulations (common tasks, such as copying, sorting and searching arrays) in the java.util.Arrays class

  • 拷贝:java.util.Arrays.copyOfRange(copyFrom, 2, 9);
  • 二分查找:Searching an array for a specific value to get the index at which it is placed (the binarySearch method).
  • 比较两个数组是否相等:Comparing two arrays to determine if they are equal or not (the equals method).
  • 赋值操作:Filling an array to place a specific value at each index (the fill method).
String[] a9 = new String[6];
Arrays.fill(a9, "Hello");
Arrays.fill(a9, 3, 5,"World");
结果是 a9[] = {Hello,Hello,Hello,World,World,Hello};
  • 排序:sort
  • Stream:Creating a stream that uses an array as its source (the stream method).
  • Converting an array to a string.
java.util.Arrays.toString(copyTo)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值