java pre Addendum - basic data types and automatic type promotion

/ *
Variable
basic data type
conversion between basic data type
conversion between basic data types and the String
to convert between decimal and binary
variable concept
1, the storage area of the memory
2, the data may be in the region of the same type changing the range
3, the program variables are the basic storage unit comprises a variable type, variable name and stored values
of a variable
1, for storing data in memory
precautions used variables
1, each of the variables in Java must be declared reuse
2, using the variable name to access this data region
3, variable scope: defining a pair located in the {}
4, the variable is valid only in its scope
5, the same scope, not the definition of the same name variables

Using variable
1, variable format defined java: variable name data type variable value =
2, then the variable must be declared use;
variables are defined in the scope thereof, effective in scope. In the role of extraterritorial failure;
not in the same scope statement names the same two variables;

*/

class  VariableTest{
	public static void main(String[] args) {
		
		int myAge = 22//变量定义 int 为整数型
		//或者拆分为两步 第一步声明 int myAge,第二步赋值 myAge = 12
		System.out.println(myAge);//变量使用,变量在经过声明和赋值后才能被使用 
		System.out.println(myClass);//myClass变量在下方public中声明,无法作用到本作用域
	}
}
	public void method(){
		int myClass = 1//在这里定义一个变量名myClass,只能用在本作用域
		System.out.println(myClass);
}

/ *
Java-defined data types
java belonging to a strongly typed language, for each data type has a clearly defined specific, and different sizes of memory space is allocated in memory.
The data sub-type
one, the basic data types (primitive type)
. 1 number (integer byte short int long, float float double) small to large memory space
2 char char
. 3 boolean boolean
two, reference data types ( reference type)
class 1 class string belonging to the class
2 interface interface
. 3 array [] array

According to the statement position different categories

Variables declared inside the body of a method for local variables

Member variable: 1, instance variables (not in static modification)
2, class variable (static modification)
local variables: 1, parameters (process variable constructor defined)
2, the method of local variables (defined internal method)
3, block local variables (defined in block)

Shaping Type: Short byte long int
java integers have a fixed number of types of tables and field length range, not affected by the specific OS, to ensure portability java program
java shaping constant default type int, long constants must declare after the addition of "l" or "L"
Java program typically declared as variables ini-type, unless insufficient to represent a large number, using only long
type number table storage range
byte 1 byte = 8 8bit -128 to 1272 of the 256 power distribution positive and negative sides, a middle 0
Short byte 2 to the power of 15 -2 2 15 -1
31 int 4 bytes of power to the power of 31 -2 to -12 about 2.1 billion
long 8 bytes 63 -2 power to a power of 2 63 -1
1MB = 1024KB 1KB = 1024bit

Float:
integer types Similarly, java type floating-point number has a fixed field length table range and
is not affected by the specific operating system

There are two types of floating-point expressions:
decimal form: e.g. 5.12 512.0f .512 (there must be a decimal point)
scientific notation form: e.g. 5.12e2 512E2 100E-2

float: single precision, accurate to seven digits significant digits, in many cases, difficult to meet the accuracy requirements
double: double precision, double precision float, usually this type
java default floating-point type double type constant statement float constants, f, or F shall be added after
single precision float occupies 4 bytes table range -3.403E38 3.403E38 E38 to power a range of 10 to 38 than the large long
occupies 8 bytes scope of the expression -1.798 double double E308 to 1.798E308
as floating point type may be used scientific notation, the range is larger than the integer

Character: char
char = character class variable occupies 2 bytes of a space
all characters in the java use Unicode code, so that a character can be stored a kanji character or a letter or other languages

Three forms of character types
1, character constants by single quotes '' enclosed in a single character, such as C1 = char 'A'
char = C2 'in the' char C3 = '. 9'
2, also allows use of a spinning Java escape character \ subsequent to the special characters into a character constant
such as char c4 = '\ n' \ n represent a newline character \ indicates that the following explanation is no longer escape, if the output \ n-, writing the format \ n-
. 3, direct use Unicode character values to represent constants: The '\ xxxx', where XXXX is
a hexadecimal integer, such as \ u000A represents \ n

char type is involved in the calculation, since it corresponds to a Unicode code

ASCII code
internal computer all data using binary representation, each bit (bit) 0 1 and two kinds of charged
Thus eight binary states 256 can be combined, is called a byte, a byte can be
used to represent 256 different kinds of state, a state corresponds to a symbol, that is 256 symbols
in the 1960s, the United states to develop a character encoding, known as ASCII code, provides for a total of 128 character codes
such as spaces is 32, binary is 00100000, a 65 binary 01000001. It comprises 128 symbols that can not be played control symbols 32
occupies only a byte 7 behind the foremost one unified 0
drawbacks, not all characters represent characters, the same coding is a discrepancy
exists a variety of encoding the world way, with a binary number can be explained that different symbols, thus opening a document needs to know coding, or garbled

Unicode code
all the world incorporate symbols, each symbol is given a unique code, using Unicode is not garbled.
Shortcomings, only a predetermined binary code symbols, does not specify how the binary code storage
, can not distinguish between the ASCII and Unicode, the computer can not distinguish between a symbol represented by three bytes or symbols 3
additional letters represented by a single byte, if each symbols with a predetermined three or four bytes, a great waste of storage space will

UTF-8 encoding

The most widely used implementation of Unicode
is a variable-length encoding scheme, using 6 bytes can be one symbol, the symbol changes depending on the byte length
coding rule
1, for single-byte UTF-8 encoding, the most significant bit 0, the remaining 7 is used to encode characters, equivalents the ASCII
2, of the multi-byte-8 UTF encoding, if containing n bytes, then the first byte of the first n bits is 1, the first byte of the n + a is 0
, the remaining bytes of the character you used to encode all the bytes after the first byte, the highest two bits are 10, the remaining six character encoding used to

Boolean (boolean)
can only take one of two values, true or false
conditions for determination, the cyclic structure.

*/

class VariableTest1{
	public static void main(String[] args) {
		byte b1 = -128;
		byte b2 = 127;
		System.out.println(b1);
		System.out.println(b2);
		int i1 = 223242535;
		long l1 = 22323232214141L;//long类型变量赋值结尾加L或者l
		System.out.println(l1+1);
		double d1 = 123.34234;
		System.out.println(d1);
	    float f1 = 133.3456782F;//float类型的变量赋值结尾加f或F
		System.out.println(f1);
		char c1 = 'a';//定义char类型变量只能写一个字符 可以是字母数字中文特殊符号等
        char c3 = 97;//char 可以直接赋值ASCII码,很少这么写
		char c4 = '5';//在此处5为字符,对应一个ASCII码
		int i2 = (int)c4;//强转字符5为int型
		char c5;
		c5 = 'g';//赋值变量的书写方式也可以分步写。
		System.out.println(i2);//输出53,代表字符5的ASCII码
		System.out.println(c3);
		System.out.println("hello"+c1);
		char c2='\n';
		System.out.print("hello" + c2);
		System.out.println("world");
		System.out.println("\\n");
		System.out.println("你是\"最\"大的");//加\后表示后面的字符不做特殊用途
		boolean bb1 = false;
        System.out.println(bb1);
        boolean isOld = true;
			if(isOld){
				System.out.println("不行");
			}else{
			    System.out.println("可以");	
			}


	}
}

/ *
String type (string) variable applications
. 1, a reference belonging to the class String variable type, variable type does not belong to the basic data
2, "", relative char variable declaration using the '' is a variable declared of type String.
3, the value of String type may be a single character, also be empty. char type is not empty, but the space may be
, for example, char c = ''; not by compiler char c = ''; compile
4, String eight basic data types and can do operation, and the operation can only be connected to operation, use + No.
5, String, and other types of calculation result is of type String.

*/

class StringTest {
	public static void main(String[] args) {
        System.out.println("Hello World!");
		String s1 = "Hello大家好";
		String s2 = "H";
		String s3 = "";
		System.out.println(s1);
		//*************************
		int number = 100;
		String numberStr = "学号";
		String info =numberStr + number;//加号+表示连接运算
		boolean b1 =true;
		String info1 = info + b1;//加号+表示连接运算
		System.out.println(info1);
		System.out.println(info);
		// 练习1
		char c = 'a';//ASCII中 a 的值是97,A的值是65
		int num = 10;
		String str = "hello";
		System.out.println(c + num + str);//输出 107hello ,第一个加号是相加,char和int相加输出int型,第二个加号是连接,int与String连接输出String
		System.out.println(c + str + num);//输出 ahello10
		System.out.println(c + (num + str));//输出a10hello 括号内先运算
		System.out.println((c + num) + str);//输出107hello
		System.out.println(str + num + c);//输出 hello10a
		System.out.println(str + 111);//输出 hello111
		System.out.println(str + "world");//输出 helloworld 
		//System.out.println(str + world);//无法编译,world必须加""才能被认为是String类

        //练习2
		//输出*  *的方法
		System.out.println("* *");//正确
		System.out.println('*' + '\t' + '*');//错误,输出char类型数值93
		System.out.println('*' + "\t" +'*');//正确
		System.out.println('*' + '\t' + "*");//错误 输出51*
		System.out.println('*' + ('\t' + "*"));//正确
		/*练习3
		String str1 = 4; 无法编译,没有""
		String str2 = 3.56f + "" ; 可以编译
		System.out.println(str2);输出3.56
		System.out.println(3 + 4 + "hello");输出7hello
		System.out.println("hello" + 3 + 4);输出hello34
		System.out.println('a' + 1 + "hello");输出98hello
		System.out.println("hello" + 'a' + 1);输出helloa1
		 练习4
		判断对错
      1   short s = 5;
		  s = s - 2;     无法编译,2int型常量
      2   byte b = 3;
		  b = b + 4; 无法编译
		  b = (byte)(b + 4);可以编译,强制转换intbyte
	  3   char c = 'a';
		  int i = 5;
		  float d = .314f;
		  doule result = c + i + d;可以编译
      4   byte b = 5;
	      short s = 3;
		  short t = s + b;无法编译,byteshort运算应为int*/
		//String str1 = 123; 编译不通过
		String str1 = 123 + "";//编译可以通过,整数常量int型与String运算输出String类。
		System.out.println(str1);//输出123字符串,将数值转换为字符串可以用这种方式。
		
		//int num2 = str1; 编译不通过
		//int num2 = (int)str1;编译也不通过
		int num2 = Integer.parseInt(str1);
		System.out.println(num2);//通过Integer方法可以将String解析为int。
	}


}

/ *
Arithmetic rule between the basic data types
discussed only seven basic operation between the data types of variables, does not contain a boolean

1, the automatic lift type
when the data type of a small capacity and a large capacity variable data types of variables do calculation, the result is automatically promoted to a large capacity of data types
at this time the capacity size refers to the size range represents the number of words is not occupied the size of the section, such as float (4 bytes) is larger than the capacity of long (8 bytes) is
byte-> SHORT-> INT-> Long-> float-> Double
byte, char, SHORT-> INT-> Long-> float-> Double
byte, when char, short calculating each output is done three int type or more of
2, cast
automatic lifting operation inverse operation type
1, the symbol will need to use strong () e.g. long l1 = 112.9L; short s1 = (short) l1; output 112, taking only the integer part.
2, the use of mandatory conversion, loss of precision may occur.

Note that
an encoding where
long 1 = 122; can be compiled by default to type int
long l2 = 12345673333; compiler does not pass beyond int range needed at the end plus L,
a float F1 = 12.3; compilation failure, must be added F., because the default type double.
2, the case of encoding 2
byte = 12 is B;
byte B1 = B + 1; compilation fails, the number 1 is an integer constants, default is int type, the fractional default double

*/

 class VariableTest2{
	public static void main(String[] args) {

		byte b1 = 2;
		int i1 = 1278;
		//byte b2 = b1 + i1
		//编译不通过,因为int范围大
		int i2 = b1 + i1;
		long l1 = b1 +i1;	
		System.out.println(i2);
		System.out.println(l1);
		float f1 = b1 + i1; //整数型数值运算结果可以定义为浮点型,系统输出自动加小数点
		System.out.println(f1);
		short s1 = 123;
		double d1 = s1;//可以将整数型直接转换为浮点型,后面自动加小数点
		System.out.println(d1);
		char c1 = 'a';// a 对应的数值为97
		int i3 = 10;
		int i4 = c1 + i3;//char只能容纳一个字符,所以i4不能为char类型
		System.out.println(i4);
	}
}
 class VariableTest3{
	public static void main(String[] args) {
        long l1=123L;//数值没有超出long范围可以不加L,也能编译,会自动转换为int类型
		short s1 = (short)l1;//数值没有超出short范围,强制转换后精度没有损失。
		System.out.println(s1);
		double d1 = 12.9;
		int i1 = (int)d1;//执行截断操作,结果只取整数部分,不是四舍五入
		System.out.println(i1);//输出12,损失小数点,精度损失
		int i2 = 128;
		byte b1 = (byte)i2;
		System.out.println(b1);//输出-128跟二进制规则有关,损失精度。
		long l2=128456744453453L;
		int i3 = (int)l2;
		System.out.println(i3);
		short s2 = (short)l2;
		System.out.println(s2);
		byte b2 = (byte)l2;
		System.out.println(b2);

	}
}
发布了47 篇原创文章 · 获赞 1 · 访问量 1043

Guess you like

Origin blog.csdn.net/wisdomcodeinside/article/details/104457997