java syntax 1

.000000static void main(String args[])

-Create objects

Box mybox; // declare reference to object
mybox = new Box(); // The
first line of allocate a Box object declares mybox as a reference to an object of type Box. When this sentence is executed,
the value contained in mybox is null, indicating that it does not reference an object. At this time, any attempt to reference mybox will result in a compilation error.
The second line creates an actual object and assigns a reference to it to mybox. Now, you can use mybox as an object of Box. But in fact, mybox only saves the memory address of the actual Box object.
Note: Those readers who are familiar with the C/C++ language may have noticed that object references look similar to pointers. This suspicion is essentially correct. An object reference is similar to a memory pointer. The main difference (that is, the key to Java security) is that you cannot manipulate it like an actual pointer. In this way, for object references, you can't arbitrarily allocate memory addresses like pointers, or manipulate it like integers.

As explained earlier, the new operator dynamically assigns an address to an object. Its general format is as follows:
class-var = new classname( );
Among them, class-var is a variable of the created class type. classname is the name of the class being instantiated. The parentheses following the class specify the constructor of the class. The constructor defines what happens when an object of a class is created. Structure
constructors are an important part of all classes, and there are a number of important properties. Most classes explicitly define constructors within their own. If a class does not explicitly define its own constructor, then Java will automatically provide
a default constructor. This is the case for the definition of the class Box. For now, we will use the default constructor.
Soon, you will see how to define your own constructor.

Assignment between objects

Box b1 = new Box();
Box b2 = b1;
You might think that the variable b2 is assigned a copy of the variable b1 object reference. That is, you may think that b1 and b2 refer to different objects, but the actual situation is the opposite, b1 and b2 will refer to the same object. Assigning b1 to b2 does not allocate any memory or make any partial copy of the original object. Since they are the same object, the change to the object through variable b2 will also affect the object corresponding to b1.
Note: When you assign an object reference to another object reference, you do not create a copy of the object, but only a copy of the reference.
The most common use of object parameters involves constructors. You often want to construct a new object and make its initial state the same as some existing objects. In order to do this, you must define a constructor that takes an object as a parameter of its class. For example, the following version of Box allows one object to initialize another object:

// Here,Box allows one object to initialize another.
class Box {
    
    
double width;
double height;
double depth;
// construct clone of an object
Box(Box ob) {
    
     // pass object to constructor
width = ob.width;
height = ob.height;
depth = ob.depth;
}
Box(double w,double h,double d) {
    
    
width = w;
height = h;
depth = d;
}
  • Java's main function
    public static void main(String[] args) { } }

First of all, the public modifier main method is a public method. The static Main method is a static method.

Java stipulates that the methods that are called directly are static .

Secondly, the main method has an array of string type as a parameter that [] can be placed anywhere in the brackets. For Java, the definition of an array is more arbitrary. The name is usually args, but you can also change it to another name you like. Main is the same as other methods, except that the function is the entry point of the program class.

The function of the Args[] array is to accept the parameters of the command line, and then pass the parameters when the program is running. In use, it can be used directly like an array. If no parameters are passed, the parameters are empty.

If you have C++ programming experience, please note that the class declaration and method implementation must be stored in the same place and cannot be defined separately. Since all class definitions must all be defined in a single source file, this sometimes generates very large .java files. This feature is designed in Java because in the long run, specifying, defining, and implementing in one place will make the code easier to maintain.

Superclass variables can refer to subclass objects

A reference variable of a superclass can be assigned by any reference of a subclass derived from the superclass

class RefDemo {
    
    
public static void main(String args[]) {
    
    
BoxWeight weightbox = new BoxWeight(3, 5, 7, 8.37);
Box plainbox = new Box();
double vol;

Here, weightbox is a reference to the BoxWeight object, and plainbox is a reference to the Box object. Since
BoxWeight is a subclass of Box, it is allowed to assign a value to plainbox with a reference to a weightbox object.
When a reference of a subclass object is assigned to a superclass reference variable, you can only access the part of the object defined by the superclass. The
subclass can call the constructor method defined in the superclass, using the following form of
super : super (Parameter-List);
Super (),
. When a subclass is called, it calls the
super() constructor of its direct superclass . In this way,
always refer to the direct superclass of the calling class. This is true even in multi-level structures.
, And super() must be the first execution statement in the
subclass constructor. In the class hierarchy, the constructors are called in the order of derivation, from superclass to subclass. Moreover, although super() must be the first execution statement of the subclass's constructor, whether you use super() or not, the order remains the same. If super() is not used, the default or parameterless constructor of each superclass will be executed.

Method coverage only occurs when the name and type declaration of the two methods are the same. If they are different, then the two methods are just overloaded

The difference between c++ and java

· Java no longer supports operator overloading. Operator overloading can cause ambiguities in C++-like programs in some cases, and
Java designers feel that it brings more trouble than it brings benefits.
· Java no longer includes preprocessing, and no longer supports preprocessing instructions. Preprocessing is not as important in C++ as it is in C. The designers of Java think it is time to eliminate preprocessing.
· Java does not support automatic type conversion, because this type conversion leads to reduced precision. For example, when converting from a long integer to an integer, the type conversion must be explicitly enforced.
· Code in Java must be encapsulated in one or more classes. Therefore, so-called global variables or global functions are no longer included in Java.
· Java does not support multiple inheritance, that is, one child class is not allowed to inherit multiple parent classes.
· Although Java supports constructors, destructors are no longer supported. However, Java has added the finalize() function.
· Java no longer supports typedefs.
· The interface of Java is similar to the abstract class of C++ (an abstract class in C++ is a class that includes at least one pure virtual function).
For example, neither C++ abstract classes nor Java interfaces can create instances. Both are used to specify a consistent interface implemented by a subclass. The biggest difference between the two is that the interface clearly shows this concept.
· Both Java and C++ support Boolean data, but Java implements true and false in a different way than C++. In C++, true is a non-zero value and false is zero. In Java, true and false are both predefined constants, and they are the only two values ​​that a Boolean expression can get. Although C++ also defines true and false and specifies them as Boolean variables, C++ automatically converts non-zero values ​​to true and zero values ​​to false. This situation does not occur in Java.

Swing

Swing graphical interface class

It is an extension of AWT, which provides a more powerful and flexible set of components.

In addition to the familiar components such as buttons, checkboxes, and labels, it also includes many new components, such as palettes, scrolling windows, trees, and tables. Many components that developers are already familiar with, such as buttons, have added new features. Moreover, the icon of the button can also be changed when the state of the button changes.

package

Java provides a mechanism for dividing the class name space into more manageable blocks. This mechanism is the package. Packages are both a naming mechanism and a visibility control mechanism.
You can define a class in the package, and code outside the package cannot access the class. This allows your classes to have privacy with each other, but not be known to other worlds.

Multiple files can contain the same package declaration . The package declaration only specifies which package the file defined in the file belongs to. It does not reject other methods of other files to be part of the same package. Most actual packages stretch to many files.
You can create package hierarchy. To do this, just separate each package name from its upper package name with a dot ".". The general form of a multi-level package declaration is as follows:
package pkg1[.pkg2[.pkg3]]; The
package level must be reflected in the file system of the Java development system. For example, a
package defined by the following statement: package java.awt.image;
needs to be saved in java/awt/image, java\awt\image or java:awt:image of your UNIX, Windows or Macintosh file system. Be sure to choose the package name carefully. You cannot rename a package without renaming the directory where the classes are stored.

Guess you like

Origin blog.csdn.net/qq_41358574/article/details/111292250