5, Java data types of variables and constants

Original link: http://www.cnblogs.com/hlc-123/p/10995751.html

Java constants: string identifier, to distinguish between different types of data.
A constant integer: 123
real constant: 3.14
character constant: 'a'
logical constants: true, false
string constants: "HelloWord"
Note: distinguishing character constants and string literals;
Note: "constant" term will be used; Also other contexts immutable value of the variable, see the final keyword

Java variables:
. 1, variable Java program is the most basic unit of storage, wherein the element comprises a variable name, variable type and scope.
2, Java programs each variable belongs to a specific type of data, it must be declared before using the format statement:
type varName [= value] [{, varName [= value]}]
For example:
int I = 100;
F = 12.3f a float;
Double D1, D2, D3 = 0.123;
String S = "Hello";
essentially, the variable is actually a small area of memory, using the variable name to access this area, and therefore, each before using variables must first apply (declaration), then you must assign (filled with content), you can use

Execution of the program:
a program (hard) --- Code (RAM) operating system code {} --- [code segment (stored codes) --- data segment (static variables, string constants) --- Stack (local variable) --- heap (new out of something)]
1, the hard drive to load a program memory area;
2, find the main method of the program started;
3, during execution of memory management;

Classification Java variables:
by location is declared:
local variables: variable method statement defined within
member variables: internal method defined outside the class variables
Note: class outer (large outside the class corresponding brackets) can not be made variable statement
by data type belongs division:
basic data type variable
reference data type variable

HelloWord.java
public class HelloWord {
int j = 9;
public static void main(String[] args) {
System.out.println("HelloWord!");
int i = 8;
}
}

main as a method, behind the main {} internally method thereof
wherein: int i = 8, args (parameters of the method is a local variable) of a local variable; int j = 9 as a member variable;
variable defined inside the braces, the the braces, the other does not recognize

数据类型---{基本数据类型,引用数据类型}
基本数据类型:数值型(整数类型(byte,short,int,long),浮点类型(float,double))、字符型(char)、布尔型(boolean)
引用数据类型:类(class)、接口(interface)、数组
Java中定义了4类8中基本数据类型。
逻辑型:boolean;
文本型:char;
整数型:byte,short,int,long;
浮点数型:float,double;
区分:内存小格的大小,内存中的布局

逻辑型boolean:
boolean 类型适于逻辑运算,一般用于程序的流程控制;
boolean 类型数据只允许取值true和false,不可以0或者非0的整数替代true和false,这点和C语言不同。
用法举例:

boolean flag;
flag = true;
if(flag){
//do something
}

字符型char
char型数据类型用来表示通常意义上的“字符”
字符常量为用单引号括起来的单个字符,例如:char echar = 'a' , char cchar ='中';
Java字符编码采用Unicode编码,每个字符占用两个字节,因而可用十六进制形式表示,
例如:
char c1 = '\u0061';
注: Unicode是全球语言统一的编码
Java语言中还允许使用转义字符‘\’来将其后的字符转变为其他的含义,例如:
char c2 = '\n';
注://'\n'代表换行符
补充:2进制、10进制、16进制之间的转换
1101 :1*1+0*2+1*4+1*8
13 :1+4+8:1101
1101 :D

  

整数类型:
Java各整数类型有固定的表表数范围和字节长度,其不受具体操作系统的影响,以保证Java程序的可移植性。
Java语言整型常量的三种表示形式:
1、十进制整型,如:12、-314、0;
2、八进制整型,要求以0开头,如:012;
3、十六进制数,要求0x或0X开头,如:0x12;。
Java语言的整型常量默认为int型,声明long型常量可以后面加‘l’或‘L’,如:
int i1 = 600;//正确 long l1 = 88888888888L;//必须加l否则会出错

类型 占用存储内存空间 表数范围
byte 1字节 -128~127
short 2字节 -2**15~2**15-1
int 4字节 -3**31~2**31-1
long 8字节 -2**63~2**63-1

浮点类型
与整数类型类似,Java浮点类型有固定的表数范围和字段长度,不受平台的影响。
Java浮点类型常量有两种表示形式:
十进制数形式,例如:3.14、314.0、.314
科学记数法形式,如:3.14e2、3.14E2、100E-2
Java浮点型常量默认为double型,如要申明一个常量为float型,则需在数字后面加f或F,如:
double d = 12345.6;//正确 float f = 12.3f;//必须加f否则会出错
下面列出Java的各种浮点类型
类型 占用存储空间 表数范围
float 4字节 -3.403E38~3.403E38
double 8字节 -1.798E308~1.798E308

举例:

Testvar2.Java
public class TestVar2 {                  //文件名和public要一致;
public static void main(String[] args) { //固定的方法;
boolean b = true;                        //声明一个布尔类型的变量b,初始值true;
int x, y = 9;                            //声明两个int变量 y为9,x没有值;
double d = 3.1415;                       //声明一个double类型的变量等于3.1415;
char c1, c2;                             //声明两个变量
c1 = '\u534e'; c2 = 'c';                 //c1为Unicode的表示
x = 12;                                  //这里才对x进行了赋值
System.out.println("b=" + b);
System.out.println
("x=" + x + ",y=" + y);
System.out.println("d=" + d);
System.out.println("c1=" + c1);
System.out.println("c2=" + c2);

}
}

  

 

转载于:https://www.cnblogs.com/hlc-123/p/10995751.html

Guess you like

Origin blog.csdn.net/weixin_30193897/article/details/95283087