Variables and data types

Source: Internet
Author: User

/* The problem to be aware of when using variables: A: The scope variable is defined in which curly brace, it is valid within this curly brace. Also, a variable with the same name cannot be defined within the same curly brace. B: Variables with initialization values that do not have an initialization value cannot be used directly. You just have to give the value before you use it, and you don't have to give the value immediately when you define it. It is recommended to give values at the time of definition. Define the format of the variable: A: Data type variable name = initialization value; b: data type variable name;  Variable name = initialization value; C: It is recommended to define more than one variable on a line, but it is not recommended */class DataTypeDemo2 {public static void main (string[] args) {//define variable int x = 100;//error, cannot have the same name/ /int x = 200;//The defined variable must give the value//int y;//system.out.println (y); int z;z = 100; System.out.println (z);//define multiple variables on a single line//int a = 10; int B = 20; int c  = 30;//above is possible, but not recommended. int a = 10; int B = 20; int c  = 30;//int D, e;//d = 40;//e = 50;//int f,int g;//error//int h; int i;//Correct}}


/*+ is an operator (we'll explain it). Do the addition operation. In general, when we are computing, we require that the data types involved in the operation must be identical. Note: Boolean types cannot be converted to other data types by default conversions (from small to large conversions) A:byte,short,char-int-long-float-doubleb:byte,short,char do not convert to each other, They participate in the operation first converted to the int type */class DATATYPEDEMO3 {public static void main (string[] args) {///Direct output to do the addition//SYSTEM.OUT.PRINTLN (3 + 4) ;//Two int type do addition int x = 3;int y = 4;int z = x + y; System.out.println (z);//defines a byte type, an int type, which does the addition of byte a = 3;int B = 4; System.out.println (A + b);//possible loss of precision//byte C =  A + b;int C = a + B; System.out.println (c);}}


/* Cast: From a large data type to a small data type. Format: Target data type variable = (target data type) (converted data); Note: Do not arbitrarily use casts, as it implies a loss of precision. */class DataTypeDemo4 {public static void main (string[] args) {byte A = 3;int B = 4;//This certainly does not matter//int C = A + b;//byte c = 7 ;//This is problematic//byte C = a + B; Improved byte C = (byte) with coercion type conversion (A + B); System.out.println (c);}}


/* Study Questions 1: Excuse me, is there a problem with double d = 12.345;float F = d; Study Questions 2: Look at the following two definitions there is no difference? float F1 = (float) 12.345;float F2 = 12.345f;f1 is actually through a D The ouble type is converted over. The F2 itself is a type of float. */class DataTypeDemo5 {public static void main (string[] args) {//assigns a double to float, plus coercion type conversion double d = 12.345;float F = (Floa T) d;//look at the following two definitions there is no difference? float F1 = (float) 12.345;float F2 = 12.345F;}}


b=b1+b2; First, type promotion (int, double) is evaluated, and finally converted to the type you want (high-type, low-type cut in front of the byte) (conversion to the end of the type will lose precision by default, conversion error, strong conversions will not error but also loss of precision).



/* Interview question: Byte B1=3,b2=4,b;b=b1+b2;b=3+4; which is a compilation failure? Why is it? B = B1 + b2; it's a problem. Because the variables are added, the type problem is looked at first, and the resulting assignment will also take into account the type problem. The constants are added, the addition is done first, and then the results are shown in the range of the assigned data type, if not, an error is given. */class DataTypeDemo6 {public static void main (string[] args) {//defines three variables of type Byte, b1,b2,b3//b1 value is 3,b2 value is 4,b no value byte B1 = 3, b2 = 4,b;//b = B1 + b2; This is type lift, all with question B = 3 + 4; Constants, the results are calculated first, and then see whether in the range of byte, if there is no error. }}




/*byte B = 130; Is there a problem? What can I do if I want to get the assignment right? Exercise: Byte B = (byte) 300;*/class DataTypeDemo7 {public static void main (string[] args) {//Because the range of byte is:-128 to 127. And 130 is not within this range, so error. byte B = 130; We can use the coercion type to convert byte B = (byte) 130;//What is the result? System.out.println (b);}} /* Analysis Process: If we want to know what the result is, we should know how to calculate it. And we know that the computation of the data in the computer is in the complement. To get the complement, the first thing to do is to calculate the binary of the data. A: Get 130 binary for this data. 00000000 00000000 00000000 10000010 This is the original code of 130, is also anti-code, or complement. B: Intercept operation, cut into byte type. 10000010 The result is a complement. C: Known complement to seek the original code. Sign bit value complement: 10000010 code: 10000001 Original code: 11111110*/

/* See program write results by adding a character and an integer, we give a table: the ASCII code table. After reading this table, we have to remember three values: ' A ', ' a ' 0 ' 48*/class DataTypeDemo8 {public static void main (string[] args) {// Direct output of a character System.out.println (' a '); a//output a character and an integer to do the addition System.out.println (' a ' + 1); 98}}

/* See program Write result string data and other data to do +, result is string type. The + is not an addition operation, but a string connector. */class DataTypeDemo9 {public static void main (string[] args) {System.out.println ("Hello" + ' a ' + 1); Helloa1System.out.println (' A ' +1+ "Hello"); 98hellosystem.out.println ("5+5=" +5+5); 5+5=55system.out.println (5+5+ "=5+5"); 10=5+5}}


Variables and data types

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.