Variable textbook series (two) - Java has eight basic variable types

Variable textbook series (two) - Java has eight basic variable types

More, click here to learn, sign up for

The type of a variable determines what the value of this variable can contain.
There are eight basic types of Java, the Java language are pre-defined, and keyword.

Eight basic types are:
Integer (four kinds)
character (one kind)
float (2 types)
Boolean (1 species)

Step 1: Integer
Step 2: Character
Step 3: Float
Step 4: bool
Step 5: String Type

Example 1: Integer
Integer Variable is used to store an integer
byte
Short
int
Long
difference lies in the different types of integer, the maximum value, the minimum value is not the same
if an attempt to impart a variable byte type value exceeds the range, a compile error this time on the need for type conversion, in later chapters will explain

Integer

public class HelloWorld{
    
  public static void main(String[] args){
      byte b = 1;
      short s = 200;
      int i = 300;
      long l = 400;
 
      /*如果试图给byte类型的变量赋予超出其范围的值,就会产生编译错误*/
      byte b2 = 200;
  }
}

Example 2: character
char type for storing a character value in single quotes' represents (a string representing the double quotes)
length and short, just as 16-bit
only store a character, more than one character will generate compilation errors

public class HelloWorld{
    
  public static void main(String[] args){
      char c = '中';
      //char 只能存放一个字符,超过一个字符就会产生编译错误
      char c2 = '中国'; //报错
      char c3 = 'ab'; //报错
 
  }
}

Example 3: float
float, there are two types of
float length is 32 bits
double length of 64 bits
Note: The default values are small double type
so float f = 54.321 a compile error occurs, because the default type is double 54.321, the type of length 64, exceeds the length of the float 32
by a letter f after the number, this number directly declared as type float
float f2 = 54.321f,
so will not be wrong

Float

public class HelloWorld{
     
  public static void main(String[] args){
      double d = 123.45;
        
      //该行会出现编译错误,因为54.321是double型的
      float f = 54.321;
        
      float f2 = 54.321f;
        
  }
}

Example 4: bool
Boolean true or false for indicating
a length of. 1
Boolean = B1 to true;
Boolean = B2 to false;

Represent true and false
Although the data is stored in real Boolean 0 (false) 1 (true)
, however, can not directly use the 01 assignment

Boolean

public class HelloWorld {
 
    public static void main(String[] args) {
 
        boolean b1 = true;
        boolean b2 = false;
 
        // 虽然布尔型真正存放的数据是0(false) 1(true)
        // 但是,不能直接使用0 1 进行赋值
        boolean b3 = 1;
 
    }
}

Example 5: String type
String type is not really basic types, but it is so widely used, is often mistaken for a primitive type.
String type is Immutable, once created can not be changed, learn more about String, refer to String

public class HelloWorld {
    public static void main(String[] args) {
        String str = "Hello Java";
    }
}

More, CLICK HERE

Published 32 original articles · won praise 182 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_44092440/article/details/102962277