Java language Overview

Source: Internet
Author: User
Tags bitwise operators finally block

  • Java translates the source code (sourcecode) into bytecode (bytecode): Javac Myclass.java, and then executes bytecode on the Java Virtual Machine (JVM): Java MyClass.
  • Java is based on object-oriented Programming (OOP), and the three principles of OOP are encapsulation (encapsulation), inheritance (inheritance), and polymorphism ( Polymorphism), encapsulation is to bundle data and code , inheritance is for hierarchical classification , polymorphism is for "one interface, multiple methods".
  • In a Java file, the name of the main class (MyClass) and the name of the file (Myclass.java) must be the same, and the class that is the entry for the program must implement the main () method. identifiers consist of letters, numbers, underscores _, dollar breaks, and numbers cannot begin. The comments in Java are the same as those in C, and there is one more document comment.
  • Java isstrongly typedLanguage, divided intoBasic TypeAndObject TypeBasic TypeThere are 8 kinds, the integral type has 4:byte(8-bit), Short(16-bit),int(32-bit) andLong(64-bit) with 2 floating-point types:float(32-bit) andDouble(64-bit), with 1 characters and two Boolean types:Char(16-bit) andBoolean(8-bit), the base type is process-oriented and does not need to be created with the new statement, and the object must be created with the new statement, we can encapsulate the base type in the object, such an object calledBasic Type wrapper, the basic type and the basic type can be automatically converted between the package, the integral type does not support unsigned, all the integers are signed, the character type is not based on ASCII but Unicode-based, the character type is integral type, can be integer operation, Boolean type only true and false, Boolean and integral types cannot be converted to each other, and type conversions are divided intoAutomatic type conversionAndForcing type conversions, digital, character, and Boolean cannot be converted to each other, two types are compatible (same as digital) and can be type-converted, and automatic type conversion is also calledWidening Conversion, coercion type conversion is also calledNarrowing Conversions, the decimal part is truncated when the float is converted to an integer type, and the expression is automatically type lifted: Byte, short, char.
  • literal (Literal) has integer literals, floating-point literals, character literals, Boolean literals and string literals, the first four are basic types, and only the last is the object type. variable called in class , the block can be nested, the variables of the inner block can access the variables of the outer block, the variables of the outer block cannot access the variables of the inner block, The variable of the inner block cannot have the same name as the variable of the outer block, but the instance variable can have the same name as the variable in the method, at which time the instance variable is overwritten and can be explicitly accessed using the Self keyword.
  • operator is divided into arithmetic operator , bitwise operators , Boolean operator and other operators, Arithmetic operators can function in numeric and character types, in addition to assignment operator , instanceof and so on. control Statements is divided into conditional statement , Looping Statements and jump statement , in the switch statement, the value in () can be a byte, short, int, char, Enumerations and strings , the value after the case is the corresponding constant expression, and the For statement has the form " For-each ": for (type Itr-val:collection) {...}, The Itr-val assignment does not affect the values in collection, and both break and continue can add label later, which is designed for nested loops.
  • An array is an object type that needs to be created using the new statement: type arr-name[] = new Type[size], and each element of the array is automatically initialized: 0 (Numeric) (with character), False (Boolean), or null (reference type). You can also use the {,} statement to create an array: type arr-name[] = {val1, Val2, ..., valn}, you can create an array of arrays. The string is also an object type, for the string class, usually using the "" statement to create the strings, the string object is immutable, the string is either recreated, or the StringBuffer class is used.
  • Java is based onclass(class), classes areObject(object), which is an instance of the class, includinginstance VariableAndMethod, instance variables and methods are called classes.member, the object must be created using theNewStatement, the object reference for Java is essentially the same as the memory pointerObject reference VariableThe assignment is not a copy, the method includesconstructor FunctionAndFinalize () method, the constructor is used to initialize the object, the constructor has the same name as the class name, and there is no return type, but implicitly for that class, the Finalize () method frees some resources (such as closing the file, disconnecting the network, etc.) before the object is destroyed, and Java does not call the Finalize () method when it leaves the block. But inGarbage Collection(Garbage Collection).overloadedThe name of the method is the same, but the parameter list (the type of the parameter, the number of arguments, the order of the parameters) must be different, and the type of the argument can be used to match the type that is automatically converted.StaticA member (static instance variable +static method) is essentially an instance variable and method of a class, is not an instance variable and method of an object, can access a static member directly with a class, only static instance variables and other static methods in a static method, static instance variables can be initialized at the time of declaration, or you can useStatic code block(Static {...}) To initialize.FinalStatic variables are immutable, can be initialized at declaration time, or can be initialized in constructors. A class can be nested like a block, you can define a class in a class, you can only instantiate an inner class in an external class, you define a class in a block, or you use an inner class only in a block.
  • Java inheritance (inheritance) only supports single inheritance , subclasses cannot access private members of the parent class, self refers to the current object, andSuper refers to the parent class object. You can use super to access instance variables and methods of a superclass (including constructors), and the first statement of the subclass constructor is to call the super () method, if the default constructor for the superclass is not automatically executed, that is, Java calls the constructor in the order of inheritance. overriding methods are methods in which a subclass can overwrite a parent class, the final variable cannot be modified, the final method cannot be overridden, and the final class cannot inherit. A parent class variable can refer to a subclass variable, and when an overriding method is called with the parent class variable, Java invokes the appropriate override method at run time based on the true type of the object to which the runtime is polymorphic . abstract classes contain abstract methods , of course, can also implement some methods, cannot instantiate abstract classes, so abstract classes can not declare abstract constructors, static methods are essentially classes, so abstract classes can not declare abstract static methods, In fact, abstract class is the parent class for the implementation class, so the abstract variable can also refer to the child class variable like the parent class variable.
  • Package(package) is a container of classes, avoiding the same class names of different packages that cause conflicts, the first statement of the Java file is PackageStatement, if not, puts the class intoDefault Package, Java usesFile system directoryStorage package, based on the hierarchical structure of the directory can create a hierarchical package, Java first in theCurrent Working DirectoryFound in the package, can not find the words fromclasspath Environment VariablesFind it in the directory, and then look at the Javac/java instructions in the-classpath OptionsThere is no in the specified directory, and if not, an error will be given.Fully qualified nameIncludingPackage NameAndclass nameUseImportStatement to import a related package or class, you can omit the package name or the class name, and all of Java's standard classes are stored in theJava Package, the Java compiler automatically importsJava.lang Sub-package: Import java.lang.*. of a class memberaccess ModifiersThere are public, protected, and private, and one is the default (none) (that is, no access modifier), which is different from the first three types. PublicMembers are visible inside and outside the package,(None)Can only be seen inside the package,protectedBetween public and (none), it has more than (none) a "visible in subclasses of other packages",PrivateCan only be visible in the class. There are only two types of access control for classes and interfaces: public and (none), and the rules are the same as the public and (none) of the class members.
  • Unlike a single inheritance, a class can implement multiple interfaces (Interface). All the members of an interface are implicitly declared public, and the variables in the interface are implicitly declared final and static, and the methods in the interface are simply abstract methods (only; no blocks). The class that implements the interface either implements all interface methods or is declared abstract, and of course the method that implements the interface must be declared public. You can use an interface variable reference to implement an instance of an interface class, an interface can inherit like a class, a subinterface can extend a parent interface, or it can be nested like a class, an interface is defined in an interface, and an access control rule for an internal interface is the same as a class member. There are public, protected, private and (none).
  • The so-called exception (Exception) is a run-time error , you can throw an exception (throw) in a try block, or you can let the method throw an exception (throws ), you can catch the exception (catch) and handle it appropriately, or you can pass an exception to the outer try block or external method, let them handle it, regardless of whether an exception occurs, execute a finally block, Finally blocks function like the Finalize () method, which frees some resources (such as closing a file, disconnecting a network, and so on), and can use a try statement with resources if you do not want to use the finally block.

Java language Overview

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.