3 JAVA basic variable types

1. Digital

  • Integer

 
Types of Byte count  range
int  4 -2^31~ 2^31-1
short 2 -2^15~ 2^15 -1
long 8 -2^63 ~ 2^63 -1
byte 1 -2^8 ~ 2^7-1

 

 

 

 

 

 

 

 

  Wherein, 10000000, is defined as - 2 ^ 7 = 0 is defined as -128.00000000. If the cross-border assignment when, then java will be forced into default variable major types of variables and error:

 1 package learnjava;
 2 
 3 public class TestType {
 4     public static void main(String args[])
 5     {
 6         byte a = -129;
 7         System.out.println("a is "+ a);
 8     }
 9 
10 }

   Exception in thread "main" java.lang.Error: Unresolved compilation problem:
     Type mismatch: cannot convert from int to byte. at learnjava.TestType.main(TestType.java:6)

    tips: different from C ++, java on different platforms, the architecture of CPU, java always correspond to the 4-byte int. This makes the program different migration on the machine platform length will not be an error. Java is better than a place of C ++.

  •  Float

     Float-4 is divided into byte, double-8 bytes, two types, the default real double type. By adding F, f specified float. Such as 3.14f, 3.14F. General use double

Type do development. Sometimes the situation 2.0-1.1 = 0.8999999999 will appear, because the computer can not be an accurate representation of a decimal, only 2 of powers and approximation.

2. Boolean

    Boolean type in JAVA to boolean, the value is true, false.

 

3. Character

   JAVA classes are extracted as string type String. This is also variable, a type unique needs capital, String.

 

4. Type Conversion

   One of the core principles of java type conversion, just do not overflow, can be transferred from small to big. The int turn double.

 

The variable declaration and initialization

  You declare multiple variables at once

         float a,b;

 Variables can also be initialized when declared:

         float a=3;

  If you need to define constants such as Pi, you can add the final keyword, you will be used to add the static keyword, to save memory, and use uppercase ID:

        final static double PI = 3.1415926

Guess you like

Origin www.cnblogs.com/peeyee/p/11354898.html