• java基本数据类型--Basic Datatypes


    Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in the memory.---说的好有道理

    Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. Therefore,

    There are two data types available in Java

    • Primitive Data Types--8种
    • Reference/Object Data Types

    Primitive Data Types

    There are eight primitive datatypes supported by Java. Primitive datatypes are predefined by the language and named by a keyword.

      bit range default value example usage
    byte 8-bit -128~127(inclusive)(2^7 -1) 0 byte a = 100  
    short 16-bit -32,768~32,767 (inclusive) (2^15 -1) 0 short r = -20000  
    int 32-bit -2,147,483,648~(inclusive) (2^31 -1) int a = 100000 Integer is generally used as the default data type for integral values unless there is a concern about memory.
    long 64-bit  

    -2^63~(inclusive)(2^63 -1)

    0L long a = 100000L This type is used when a wider range than int is needed
    float  single-precision 32-bit   0.0f float f1 = 234.5f Float is mainly used to save memory in large arrays of floating point numbers
    double double-precision 64-bit   0.0d  double d1 = 123.4

    This data type is generally used as the default data type for decimal values, generally the default choice.

    Double data type should never be used for precise values such as currency

    boolean one bit  false or true false boolean flag = true

    There are only two possible values: true and false

    This data type is used for simple flags that track true/false conditions

    char single 16-bit Unicode character 'u0000' (or 0)~'uffff' (or 65,535 inclusive)   char letterA = 'A' Char data type is used to store any character 

    Reference Datatypes

    • Reference variables are created using defined constructors of the classes. They are used to access objects. These variables are declared to be of a specific type that cannot be changed. For example, Employee, Puppy, etc.

    • Class objects and various type of array variables come under reference datatype.

    • Default value of any reference variable is null.

    • A reference variable can be used to refer any object of the declared type or any compatible type.

    • Example: Animal animal = new Animal("giraffe");

    Difference between Primitive and Reference

    1. primitive variables: store primitive values

     reference variables: store addresses

    2.Assignment-赋值操作,看图

     primitives: the primitive value is copied

     eferences: the address is copied

     

     3.Comparisons (e.g. ==)--比较

     primitives: the primitive values are compared

     references: the addresses are compared

     4.Passing Parameters--参数传递

     copies the contents of actual parameter(实参) into the formal parameter(形参) (i.e., pass-by-value)

    • primitives: the primitive value is copied
    • references: the address is copied
    • primitives: changing the formal parameter's value doesn't affect the actual parameter's value
    • references: changing the formal parameter's address doesn't affect the actual parameter's address but changing the formal parameter's object does change the actual parameter's object since they refer to the same object

     5. Store--存储

    primitives:存储在栈内存中(stack memory)

    references:存储在栈内存中(stack memory),其引用的对象存储在堆内存中(heap memory)

     6. 补充一点儿,对于String来说,==比较的是地址值,equals方法比较的是对象内容。

    public class StringDemo {
        public static void main(String[] args){
            String s1="abc"; //s1是一个类类型变量,“abc”是一个对象
                            //字符串最大特点,一旦被初始化就不可以被改变。
            String s2=new String("abc"); //上面这两种写法等价的,不过这个好像没见到有用的
            System.out.println(s1==s2);  //它俩指向的是两个对象,==,比较的是地址值
            System.out.println(s1.equals(s2));  //String类覆写了Object类中的equals方法,原方法比较地址值,String类中该方法比较字符串内容是否相同
            s1="kk";
            System.out.println(s1);  //打印结果是kk,不是abc变成了kk,abc这个对象在堆内存中,kk这个对象在堆内存中。s1之前指向“abc”
            //现在指向“kk”了
        }
    }

    【引文】https://www.tutorialspoint.com/java/java_basic_datatypes.htm

    【引文】https://www.tuicool.com/articles/NBvUFrY

    【引文】http://pages.cs.wisc.edu/~bahls/cs302/PrimitiveVsReference.html

  • 相关阅读:
    A
    B
    A
    A
    B
    C
    有趣的平方和的推导
    一篇写的非常好的匈牙利算法文章
    2014 UESTC Training for Data Structures G
    2014 UESTC Training for Data Structures H
  • 原文地址:https://www.cnblogs.com/live-for-learning/p/12254133.html
Copyright © 2020-2023  润新知