Java basic syntax

Posted by PHPHorizons on Sat, 05 Mar 2022 00:18:39 +0100

Basic Java syntax (I)

Comment, identifier, keyword

Single-Line Comments

//Single-Line Comments 

multiline comment

multiline comment 

Documentation Comments

JavaDoc: document comments

*Documentation Comments 


  • Keywords cannot be used as variable names
  • Identifier case sensitive
  • The identifier should be A-Z or A-Z, dollar sign $, or underscore_ start


Generally, variable names defined by the system itself, such as class, void, public, etc., cannot be used as identifiers (variable names)

data type

Java is a strongly typed language. The data types of Java are divided into two categories

  • Basic type
  • reference type

Basic type

  • Integer type

    1. int (4 bytes)

    2. byte(1 byte)

    3. short (2 bytes)

    4. Long (to be followed by l/L) (8 bytes)

      Integer extension

      Base system

      Binary 0b

      decimal system

      Octal 0

      Hex 0x

  • Floating point type

    1. float (followed by f/F) (4 bytes)
    2. Double (default double) (8 bytes)
  • Character type (note that String is a class, not a keyword)

    1. char
  • boolean type (1 byte)

    1. true or false

reference type

  • class
  • Interface
  • array

Type conversion

  • Forced conversion (high - > low) int a = (char) a

  • Auto convert (low - > high) char a = int

    Low ------------------------------------------ > High


be careful

  • Boolean values cannot be converted
  • Cannot convert an object type to an unrelated type
  • There will be memory overflow or accuracy problems during conversion s

Variable, constant

be careful

  • Variable names should be standardized
  • Semicolon after variable

Variable scope

public class Demo08{
    //Attributes: Variables
    //Object: subordinate variable
    //The instance variable is subordinate to the object. If it is not initialized, the default value of this type is 0 or 0.0, and the Boolean value is false by default
    //The default value is null except for the basic type
    String name;
    //Class variable
    static int a;//It can be output directly in main
    public static void main(String[] args){
        //Local variables: values must be declared and initialized
        int i;  

Constant: the value cannot be changed after initialization (generally uppercase characters)

final constant name = value

final double pi=3.14;

static final double pi=3.14;// Modifier does not exist in order


Arithmetic operators: +, -, *, /,%, + +, –

public class test{
   public static void main(String[] args){
       Long a=123213123123;
       int b=300;
       short c=20;
       byte d=10;
       System.out.println(a+b+c+d);//As long as there is, the output is long
       System.out.println(b+c+d);//If there is no Long, it is always int
       System.out.println(c+d);//If there is no Long, it is always int
//Power operation 2 ^ 3

Assignment operator:=

Relational operators: >, <, > =, < =, = =,! =, instanceof

Logical operators: & &, ||,!

Bitwise operators: &, |, ^, ~, > >, <, > > (understand)

public static void main(String[] args){
    A=0011 1100
    B=0000 1101
    A&B =0000 1100((both are 1, only 1)
    A|B =0011 1101((both are 0, only 0)
    A^B =0011 0001((0 for the same and 1 for the different)
    ~B  =1111 0010(Direct (reverse)
    2 Shift left 3 bits, high efficiency!!!, Underlying principle (computer composition principle)
    0000 0010   2
    0000 0100   4
    0000 1000   8
    0001 0000   16

Conditional operator: (?:)

X?Y :Z

If X is true, the result is Y, otherwise the result is Z

Extended assignment operators: + =, - =, / =, * =+

public static void main(String[] args){
    int a=20;
    int b=30;
    //String connector "+", string
    System.out.println(" "+a+b);//If the string is in the front, the following string will be spliced
    //Output: 2030
    System.out.println(a+b+" ");//If the string is at the end, the preceding ones are added by themselves
    //Output: 50 
    //Summary: who listens to who in front, String in front, + becomes splicing

Package mechanism, JavaDoc

Package mechanism (essentially folder)

  • In the future, classes will be better organized. java provides a package mechanism to distinguish the namespace of class names

  • Package syntax:

    package pkg1[. pkg2[. pkg3......] ];

  • Generally, the inverted company domain name is used as the package name;

    Package name: com baidu. www

  • In order to use a package member, we need to explicitly import the package in the Java program. Import is required

    Statement to complete this function

    See the video crazy God said Java

    Import all classes under the package


    You can see Alibaba java development manual to standardize development habits


  • The javadoc command is used to generate its own API documents

  • Reference parameters:

    @Author author name

    @Version version number

    @since indicates that the earliest version of jdk is required

    @param parameter name

    @Return return value

    @throws exception thrown

package com.kunag.base
*@author kuangsheng
*@version 1.0
*@since 1.8
public class Doc{
    String name;
    *@author luhaipeng
    *@param name
    *@throws Exception
    public String test(String name)throws Exception{
        return name;

//If it is added to the class, it is the annotation on the class
//Added to the method is the annotation of the method
//For document annotation

Document comments can automatically generate API documents from the command line

Learn to use IDEA to generate JavaDoc documents

See crazy God said JAVA

Topics: Java