My Java journey Lesson 3: JAVA language syntax basics and java journey

Source: Internet
Author: User

My Java journey Lesson 3: JAVA language syntax basics and java journey

1 integer

Int num = 0000000_000; // The syntax supported since java 7 is easier to read. The java compiler removes 2 strings and cannot use the = Operator to check whether the two strings are equal, instead, use equals (), as shown in the following boolean result = "hello ". substring () = "he"; // The value of result is false boolean result = "hello ". substring (0, 2 ). equals ("he"); // result is true charAt () // returns the character String at the specified position. In Java, the String class contains more than 50 methods. lang. string package 3 Input and Output System. out. println (), in java. for file input and output in the lang Package, the workflow object 4 Process Control break java provides a label-based break statement that jumps out of the specified defined tag location and has 5 big values.
BigInteger and BigDecimal in the java. math package
6 Arrays
For (variable: collection) statement example: for (int element: coll) System. out. println (element); // print the array of each element. Copy the copyTo () array to sort the sort ()
7 objects and Classes
Static fields and static methods belong to the class rather than the class instance. all instances of the class share a static data constant, and few static variables are used, static constants are usually used. Static Method is a method that cannot be operated on an object instance. It can be considered that the static method is a method constructor without the this parameter. this (param, param) can use this to call another constructor of the same class to initialize the block class first, and then run the initialization block, then run the main part of the constructor. This mechanism is neither necessary nor common. Usually, the initialization code is directly put in the constructor. The object destructor and the finalize method Java have an automatic garbage collector, so the Destructor is not supported. When some objects use resources outside the memory, you can add the finalize method to any class. This method is called before the Garbage Collector clears objects. In practical applications, do not rely on the finalize method to recycle any shortage of resources, because it is difficult to know when this method can be called. The Java package uses the package to organize the class. The main reason for using the package is to ensure the uniqueness of the class name. Therefore, Sun recommends that you use the domain name in reverse order as the package name. From the compiler perspective, there is no relationship between nested packages. Import java. util. * or import java. util. Date;
8 inheritance
The super keyword super () calls the constructor of the parent class; super. func () calls the final method of the parent class to ensure that the method or class does not change the semantics in the subclass.
9 Interface
An interface cannot contain instance domains or static methods, but can contain constants. an interface that implements Multi-inheritance implements is a key word used by a class to implement an interface.
10 Internal classes
An internal class is a class defined in another class. A local internal class can define a local class in a method. A local class cannot be declared using public or private, and its scope is limited to the block that declares the Department class. therefore, it is completely hidden from the outside. anonymous internal classes do not need to be named if only one object of this class is created. this type is called an anonymous internal class. I personally feel like the Anonymous class or class initializer in C. static internal classes sometimes use internal classes to hide a class inside another class, and do not need internal classes to reference peripheral class objects. in this case, you can declare the internal class as static to cancel the generated reference.
11 Exceptions
Exception objects are derived from an instance of the Throwable class. you can create your own exception classes. the next layer of Throwable is divided into two branches: Error and Exception Error internal errors and resource depletion errors during Java runtime. the application should not throw this type of object. this type of error rarely occurs. when designing an Exception program, you must pay attention to the Exception hierarchy. it is further divided into two branches: one is derived from RuntimeException, caused by program errors, such as array out-of-bounds, type conversion error, and the other contains other exceptions, such as I/O errors. such as opening a non-existing file. the Java specification calls the exceptions derived from the preceding two classes as unchecked exceptions, and all other exceptions are called checked exceptions. if you want to pass an exception, you must add a throws specifier in the method header. assert condition; and assert condition: expression; (disabled by default. In eclipse, you must enable relevant settings. Open Window-preferences, select Installed JREs, select the relevant version, click edit, and enter-ea in Default VM arguments .) In java, three mechanisms are provided to handle system errors. a throws an exception. B records logs. c. assertions are fatal and unrecoverable errors. assertions are only used for development and testing. Record logs // log Processing PackageJava. util. logging. Logger; java. util. logging. Level;
12 debugging skills
Print or record the value of the variable. Put the main () function in each class to perform unit tests on each class. A little-known but very effective technique. JUnit framework log proxy and so on.

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.