Java变量的分类与初始化

发表于:2017-6-26 15:39

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:Daren Lin    来源:51Testing软件测试网采编

#
java
  变量
  Java语言里的变量分以下4类:
  1. Instance Variables: (Non-Static Fields) 就是类里非静态的field
  2. Class Variables: (Static Fields) 类里静态的field
  3. Local Variables: 局部变量
  4. Parameters: 参数
  两个术语要注意,分别是field和variable。field是指上面的1和2,是class拥有的。而不是field的变量就叫variable,对应上面的3和4,或者说局部的都是variable。
  命名
  Java的文档中明确写了推荐的命名方式,这就是为什么java的代码里命名基本都差不多。格式就是用连续的有意义的单词,从第二个单词开始,首字母大写。比如: speed, currentGear
  基本数据类型 Primitive Data Type
  Java有8个基本数据类型
  整数型 4个: byte, short, int, long
  浮点 2个: float, double
  字符 1个: char
  布尔 1个: boolean
  他们的默认初始值如下:
   
  默认值 Default Value
  关于没有初始化的变量的默认的值,c/c++是不会帮你设置的,而java不同,官方文档描述的很清楚:
  The eight primitive data types are: byte, short, int, long, float, double, boolean, and char. The java.lang.String class represents character strings. The compiler will assign a reasonable default value for fields of the above types; for local variables, a default value is never assigned. A literal is the source code representation of a fixed value. 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.
  就是对于Field来说,自动设成默认值,比如int设成0,object设成null。而对于Variable来说,不会自动设置,如果没有初始化的编译器会报错。
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号