Acquaintance JAVA language

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/shirln/article/details/86293658

Recommended reading:

Foreword

       Many game developers may be in doubt, you will C #, JS, TS, why acquaintance JAVA do? Some might say, to learn more on their own good pictures. This can be considered a reason for me to learn JAVA of it, it is mainly because many games are online games, most companies are using server written in JAVA, so I think it is really necessary to learn of the JAVA.
       In fact, had heard JAVA and C # are very similar, and has been busy, no time to understand, and now finally have time to understand, at the moment, the heart actually a little excited, and now everyone took a look at it together.

Classes and Objects

For example: MyClass create a class instance of an object and

//MyClass类
public class MyClass{
	//构造函数
   public MyClass(String name){
      System.out.println("小狗的名字是 : " + name ); 
   }
   
   public static void main(String []args){
      // 实例化对象
      MyClass myClass= new MyClass( "我自己" );
   }
}

By way of example above can be found in the java classes and objects using C # with exactly the same can be said.

JAVA package

Mainly used for packet classification classes and interfaces. Import statement is used to provide a reasonable path so the compiler can find a class.
For example: the compiler to load all the classes in the java_installation / java / io path

import java.io.*;

Reference types

No java pointers, with reference to the function implementation pointer. java references types: Object, Array

Java constants

Constants in the program is running can not be modified. Constants with the const keyword modifications in C #, Java use in the final modified keywords.
For example: Π = 3.141592

final double PI = 3.1415927;

Java variable types

1. class variables: independent variables other than a method, modified with static.
2. Examples of variables: independent of variables other than the method, but no static modification.
3. Local variables: The class of variables.
1. Local variables
Local variables are allocated on the stack, local variables no default, so the local variable is declared, must be initialized before they can be used.
2. Instance variables
instance variables declared in a class, but outside the method, constructor, and block. Instance variables have a default value. Default numeric variable is 0, the default value of the Boolean variable is false, the default reference value type variable is null.
3. class variables

Access control modifiers

Java supports four different access privileges: default (which is the default, nothing to write), private, public, protected
significance similar with C #, due to the presence of java, therefore, protected will vary, you need two types of where:
1. a subclass of the base class in the same package : a variable is declared as protected, methods and constructors can be any other type of access to the same packet;
2. subclass of the base class is not in the same package : it in a subclass, the subclass instance can access its protected methods inherited from the base class, the base class and can not access the protected methods instance.
Java is a protected class member access to the most difficult to understand qualifiers.

Non-access modifier

static modifier for modification of class variables and class methods.

final modifier for modifying classes, variables and methods, a modified final class can not be inherited, a modified method of the class can not be inherited redefined, the modified variables constant, can not be modified. f Inal modifier and static modifier is usually used together to create a class constants

abstract modifier is used to create abstract classes and abstract methods.

synchronized and volatile modifier, mainly for programming thread.

instanceof operator

The operator is used to operate an object instance, to check whether the object is of a particular type (class type or interface type)
in the following format:

( Object reference variable ) instanceof  (class/interface type)

Guess you like

Origin blog.csdn.net/shirln/article/details/86293658