Home  >  Article  >  Java  >  Java basics data types, memory, modifiers, code blocks

Java basics data types, memory, modifiers, code blocks

巴扎黑
巴扎黑Original
2017-06-26 11:28:371080browse

Java data types

  • Basic data types

  • ##Values: int, short, long

  • Character: char

  • Boolean: boolean

  • ##Reference data type

  • class (class)
  • interface (interface)
  • Array[]
  • Number of bytes occupied

    1 byte= 8 bits

  • int:4 bytes
  • char: Specifies 2 bytes. If UTF-8 encoding is used, numbers and English occupy 1 byte, and Chinese takes 3 bytes; if GBK encoding is used, Chinese takes 2 bytes
  • float: 4 characters Section
  • short: 2 bytes
  • long: 8 bytes
  • double: 8 Bytes
Note: The default for integers in Java is
Int

, and the default for decimals is double

float f = 2.3; // 2.3默认为double,不能转为float,可能丢失精度float f = 2.3f; // true 末尾加f,表示这是一个float型long l = 12343565475989778034; // false, 默认为int,但是超出了范围long l = 12343565475989778034l; // 末尾加l,表示这是long型
But the first line of the following code is correct.

byte b = 4; // 默认int,但经过检查4是在byte型范围内的,发生int -> byte的隐式强转b =  b + 3; // false,和为int,int型不能赋值给byte// aa和bb都是变量,其和不能确定是否在范围内byte aa = 3;byte bb = 4;byte cc = aa + bb; // falsebyte cc = 3 + 4; // 但若明确给出常量,则true

    Variables (local) are only valid within a pair of
  • {}

    and are then destroyed. Local code blocks can determine the life cycle of local variables.

  • {  int a = 3;
    }
    System.out.println(a); // false, 已经出了这对{},被销毁了
    switch statement
  • // 若不加break,从匹配到的那个case开始,之后如果都没break,都执行,不会跳出switch (expression) {  case x: 
        code;break;  case y:
        code;break;  default:
        code;break}
Function overloading and Override (override @Override)

    Function overloading
  • In the same class
  • Same name
  • The parameters are different, or the types are different
  • Function overloading and return type
  • has nothing to do with it

    !

  • Function override (@Override)
  • Inherited the parent class or implemented an excuse, if something needs to Override the method of the parent class, define the child Class-specific methods.
  • The return type, parameter list, and function name are all consistent. Modifiers are generally the same, and in general, everything is the same except for the internal implementation.
  • Array--Reference type

    Definition of array
  • int[] a = {1, 23, 3};int[] b = new int[4]; // 此种定义方式必须指定数组长度int[] c = new int[]{1, 2, 3}; // 一般不这么写
    Two-dimensional array
  • int[][] arr2= new int[3][]; // 至少给第一维指定长度// 每行的列数相同,3行2列int[][] = arr2 = new int[3][2];// 每行列数不一样int[][] arr2= new int[3][];
    arr2[0] = new int[2]; // 第1行arr2[1] = new int[3]; // 第2行arr2[2] = new int[4]; // 第3行
Stack memory and heap memory

    Stack memory: storage are local objects and will be released when the scope ends
  • Heap memory: Arrays and objects are stored, and everything created by
  • new

    is in the heap memory

  • int[] arr = new int[3];// 这句右边new一个引用数据到堆内存,给三个值默认初始化为0// a指向这个数组对象,即地址。也可以说new出来的数组把它的地址给了arr// arr = null; 表示空,不绑定地址了int[] a = new int[3];int[] b = new int[5];
    a = b; // 把b的地址绑定到a,a和b都指向b的地址了System.out.println(arr); // 打印的是地址,格式为"[I@number",表示int,number为16进制表示的地址
Access modifier

    public: Any class and any package can access
  • private: Access is limited to the current class
  • default (no modifiers are written, default): Anyone in the same package can access it, and subclasses in different packages cannot. Access
  • protected: Only subclasses of the same package and different packages can access. (The range is slightly larger than default)
  • public static void main(String args[])

This is a fixed format!

Why must it be written like this?

    public: The outside world (JVM) calls the main() function. In order to facilitate access, it is set to the highest authority.
  • static: static function, not Class member functions can be called directly through
  • class name.main()

    without new objects.

  • void: The main() function is to execute the task and does not need to return any value.
  • main(): JVM recognizes the word main(). When it meets the writing method of
  • public static void main(String args[])

    , it will be regarded as The entry point of the program starts execution.

  • String args[]: Command line parameters, which must be
  • public static void abc(String args[]) // 函数名不是main,不是主函数public static void main() // 缺少命令行参数 String args[]也不是主函数static void abc(String args[]) // 不是主函数,一定要是public static void main(String args[])
static static

static function

Static functions are generally used as tool functions.

class members

cannot be used in them. Only static members or methods can be used, but member functions can Use static data and methods. Static variable

static int money

This is a static data. It can be shared by many objects. If object A changes a, a in object B will also change. Everyone uses the same amount of money.

package Test;public class StaticDemo {private static int money = 100;public int getMonney() {return money;
    }public void useMoney() {
        money--;
    }public static void main(String[] args) {
        StaticDemo a = new StaticDemo();
        StaticDemo b = new StaticDemo();// a用了一块a.useMoney();// 还剩99System.out.println(a.getMonney());// b用的是同一份moneyb.useMoney();// 还剩98System.out.println(b.getMonney());
    }
}

  • 成员变量和静态变量

  1. 成员变量随对象的建立而建立,随对象消亡而消亡。13dd6e535e0887248bab718236989bd4 静态变量随着(和对象即实例区别开)的加载而加载,随类的消亡而消亡。即静态成员是先于实例存在的,还没new出对象就存在了。

  2. 成员对象只能是new出新实例后调用。13dd6e535e0887248bab718236989bd4 静态成员可以直接使用,类名.静态变量即可。

  3. 成员变量被称为实例变量。13dd6e535e0887248bab718236989bd4 静态变量被称为类 变量

  4. 成员变量位于堆内存的对象中,是对象的特有数据。 13dd6e535e0887248bab718236989bd4 静态变量位于方法区(的静态区)中,也叫对象的共享数据

Q:static为何不能调用非静态方法或者数据?

A:因为静态数据先于对象就已经产生,成员变量还不存在,不能访问。同理static函数中不能出现this、super关键字。

Q: 什么时候用static?

A:1. 静态变量,共享数据时,各个对象都使用同一个数据,不必作为对象特有的数据时候。如每个学生的姓名是特有的,但是每个学生可以共用同一个图书馆。

  1. 静态方法,无需访问类成员时(非静态),就可以定义为静态,一般为工具函数。

各种代码块

public class Abc {  
  // 构造代码块,没有static。每new一个对象,就执行一次,故称为构造代码块
  // 针对不同的对象,进行相同的初始化,而构造函数对不同的对象进行不同的初始化,如给不同的人传入不同的名字和年龄进行初始化
  {
    System.out.println("我是构造代码块")
  }  // 构造函数
  Abc() {
    System.out.pritnln("我是构造函数,后于构造代码块执行");
  }  // 静态代码块最先执行,不论放在main()前面还是后面,都先于main加载,且只执行一次
  static {
  System.out.println("我最先");
}  public static void main(String args[]) {
    Abc a = new Abc(); // 先进入先构造代码块,// 以下是局部代码块,b只在这对{}内有效{      int b = 3;
    }
    System.out.println(b); // false,b已经被释放
  }
}

!!!总的来说,执行顺序:static代码块 --> main() --> 构造代码块 --> 构造方法

The above is the detailed content of Java basics data types, memory, modifiers, code blocks. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn