The syntax of the Java basic variables and constants

Java Specification of basic grammar, identifiers, keywords, constants and variables

Java writing specifications

  1. End "}" must begin "{" longitudinally aligned with the first character of the row.
  2. Beginning "{" on the upstream end of the code, and before adding the spaces.
  3. Up the "{", the downlink downlink code indentation horizontal tab.
  4. No uplink "{", the first letter of the downlink and the uplink code is aligned

    Identifier

    Outline

    In Java variables, constants, functions, and statements are all block names, their names are collectively referred to as Java identifiers. For the classes, objects, methods, variables, data types, and custom interfaces named.

    composition

    Identifiers are numbers, letters, underscores (_) and dollar sign ($) symbol or RMB (¥) components.

    Naming Convention

    1. There can be letters, numbers, underscores, dollar signs composed, can not start with a number.
    2. Strictly case-sensitive.
    3. It is not Java keywords and reserved words.
    4. Meaningful, usually nouns.

    E.g

    Legal name: Hello, hello_CSDN, _hello, abc $ 123,

    Illegal name: class, 123abc, abc, 123

    Keyword

    Java keywords have special meaning to the Java compiler string, and also a convention programmers, programmers use a keyword to tell the compiler what I do.

    Keyword

    meaning

    abstract

    Members of the class or method show abstract properties

    assert

    Assertions, used for debugging

    boolean

    One of the basic data types, declare Boolean types of keywords

    break

    Advance out of a block

    byte

    One of the basic data types, Type Byte

    case

    Used in the switch represents a branch which is among the statements,

    catch

    In exception processing for capturing an abnormal

    char

    One of the basic data types, character types

    class

    Declare a class

    const

    Reserved keywords, there is no specific meaning

    continue

    Back to the beginning of a block

    default

    By default, for example, used in a switch statement, indicating that a default branch. Java8 , also acts on the default implementation of the interface functions declared

    do

    Used in the do-while loop structure

    double

    One of the basic data types, double precision floating point type

    else

    Used in a conditional statement, indicating that the time when the branch condition is not satisfied

    enum

    enumerate

    extends

    It indicates that a type is a subtype of another type. For a class, or it may be another class abstract class; For an interface, the interface may be another

    final

    Used to describe the final attribute that identifies a class can not derive subclasses or members of the method can not be covered, or the value of the member of the domain can not be changed, the constants used to define

    finally

    Statement block for processing exceptions, to declare a substantially certain to be performed to

    float

    One of the basic data types, single precision floating point type

    for

    An endless guide word structure

    goto

    Reserved keywords, there is no specific meaning

    if

    Guide word conditional statement

    implements

    Showed that a class implements the given interface

    import

    To access the show specified class or package

    instanceof

    It used to test whether an object instance of the object type is designated

    int

    One of the basic data types, integer type

    interface

    interface

    long

    One of the basic data types, long integer type

    native

    The method is used to declare a computer-related language (e.g., C / C ++ / FORTRAN implementation language)

    new

    To create a new instance of an object

    package

    package

    private

    An access control mode: the private mode

    protected

    An access control method: Protected Mode

    public

    An access control method: a common mode

    return

    Return data from member methods

    short

    One of the basic data types , short integer type

    static

    Show a static properties

    strictfp

    用来声明FP_strict(单精度或双精度浮点数)表达式遵循IEEE754算术规范

    super

    表明当前对象的父类型的引用或者父类型的构造方法

    switch

    分支语句结构的引导词

    synchronized

    表明一段代码需要同步执行

    this

    指向当前实例对象的引用

    throw

    抛出一个异常

    throws

    声明在当前定义的成员方法中所有需要抛出的异常

    transient

    声明不用序列化的成员域

    try

    尝试一个可能抛出异常的程序块

    void

    声明当前成员方法没有返回值

    volatile

    表明两个或者多个变量必须同步地发生变化

    while

    用在循环结构中

     

    可以按照关键字作用划分为四组:

    1. 用于数据类型:boolean、byte、char、double、false、float、int、long、new、short、true、void、instanceof、
    2. 用于语句:break、case、catch、continue、default、do、else、for、if、return、switch、try、while、finally、throw、this、super
    3. 用于修饰:abstract、final、native、private、protected、public、static、synchronized、transent、volatile
    4. 用于方、类、接口、包和异常:class、extends、implements、interface、package、import、throws

      没有几个被Java保留的没有意义的关键字:cat、uture、generic、innerr、operator、outer、rest、var

      还有三个保留字他们不是关键字而是文字包含了Java定义的值,他们不能用作标识符

      true、false、null

    变量

    什么是变量

    变量是内存地址的别名,因为内存地址不好记忆。是为了方便存取内存地址中的数据。

    变量三元素:变量类型、变量名、变量值

    变量名的命名约定

    1. 满足标识符命名约定
    2. 符合驼峰法命名约定
    3. 见名如意
    4. 变量名的长度没有限制

    数据类型

    Demo

    变量声明

        变量类型 变量名;

        int n;//声明一个整型变量名为n

    赋值

        变量名=变量值;

        n=3;//给变量n赋值为3.

    边声明边赋值

        int n = 3;

    特点

    1. 先声明,在赋值,后使用
    2. 局部或全局变量不能重复定义

    转义字符

    类型转换

    自动类型转换

    Demo

    int d= 123;

    doublef = d;

    自动类型转换是一个小数据范围转大的数据范围,不会丢失数据细节。那么double的值就是123.00。

    强制类型转换

    如果a类型的数据表示范围比b类型大,a赋值给b则需要强制类型转换。

    Demo

    double d = 123.45;

    float f = (float)d;

    这是一个简单的强制类型转换。因为double类型是双精度而float是单精度,double类型的数据表示范围要比float大,所以需要强制类型转换,会丢失数据细节。

    也就是说double类型强制转换为float类型会丢掉一个精度,那么float的值就是123.4。就像我们要把大象放在冰箱里需要把它切开丢掉一些不需要的东西,是同样的道理。

    常量

    关键字final

    是用final定义后的变量不能再次进行更改操作。

    Demo

    fnal int num = 5;

    这时候这个名为num的常量变量就不能再次更改了,如果对他进行更改了系统会划红线报错,不能执行必须改正。

     

    Demo

    class Demo1{
    
        public static void main(String[] args){
    
            int a = 1;
    
            int b = 2;
    
            int c;
    
            System.out.printle("变量a的值=" + a + "/n变量b的值=" + b);
    
            c = a;
    
            a = b;
    
            b = c;
    
            System.out.printle("变量a的值=" + a + "/n变量b的值=" + b);
    
        }
    
    }
    

      

    这是比较简单的a、b互换数字的逻辑代码。今天就到这里啦,我们下期再见!

     

     

    会长时间不间断跟新我所学到的知识。

    错误希望指正。

    我们会变得更好,加油!

    我们评论区里见!

Guess you like

Origin www.cnblogs.com/auditoryfeather/p/12384308.html