Basic java tutorial-object-oriented (1), Basic java tutorial

Source: Internet
Author: User

Basic java tutorial-object-oriented (1), Basic java tutorial

1. Object-oriented

1.1 java keyboard input

1.1.1 In my opinion, this method is the simplest and most powerful, that is, using the struct class.

Import java. util. secret; public static void main (String [] args) {consumer SC = new consumer (System. in); System. out. println ("enter your name:"); String name = SC. nextLine (); // string input System. out. println ("enter your age:"); int age = SC. nextInt (); // integer input System. out. println ("enter your salary:"); float salary = SC. nextFloat (); // float type input System. out. println ("your information is as follows:"); System. out. println ("name:" + name + "\ n" + "age:" + age + "\ n" + "salary:" + salary );}

This Code already shows that the functions can be implemented by making a slight change to variables of the string, integer, or float type! Undoubtedly, he is the most powerful

 1.1.2

While (. hasNext () {System. out. println ("You entered:" +. nextInt ();} hasNext () is a method of the struct class. It determines whether there is input or input,. hasNext () is true, no input,. hasNext () is false

1. 2. Array

Define the array int [] [] a = new int [10] [10];

1.3.java variable naming rules

1. Class Name: the first letter of each word in uppercase, for example, UserName

2. Method Name and variable name: the first letter of a word is in lowercase, And the other letters are in uppercase.

Example: getInfor (), getInfor

Note: the first word of the class method must be a verb.

Constructor (used to initialize the object): The name must be the same as the class name.

3. Package name: All lowercase letters, for example, example

4. Constant: All letters are in uppercase. If multiple words are used, _ is used to connect them.

Example: MIN_VALUE, PI

1.4.Java memory Parsing

Parameters and local variables are allocated memory space in the stack, and are automatically released after running. Memory programmers in the heap are released. When analyzing memory parsing, you must start with main () for further analysis. Variables of some basic types defined in the function and referenced variables of the object are allocated in the function stack memory. Heap memory is used to store objects and arrays created by new. The memory allocated in the heap is managed by the Java Virtual Machine's automatic garbage collector. The referenced variable is a common variable. When defined, the memory is allocated in the stack. The referenced variable is released outside the scope of the program. Arrays and objects are allocated in the heap. Even if the program runs outside the code block where new statements are used to generate arrays and objects, the heap memory occupied by arrays and objects is not released, arrays and objects become junk only when no referenced variable points to it. They cannot be used again, but still occupy the memory. They are released by the garbage collector at an uncertain time. This is also the main reason why java accounts for memory usage. In fact, the variables in the stack point to the variables in the heap memory. This is the pointer in Java!

Static variables are stored in the data zone.

New creates an object and runs the constructor directly,

First, we will explain each area in the memory.

Stack): Stores basic types of data and object references, that is, stores variables.

If the data of the basic type (non-static variables) is stored, the variable name and value are directly stored in the memory of the stack;

If it is a reference type, the variable name is stored in the stack and then points to the new object (stored in the heap ).

    Heap): Stores data generated by new.

    Data segment: Static domain and constant domain.

     Static domain:

Static members defined in static are stored in the object (I .e. static variables. Similarly, if the static variable is of the basic type, the variable names and values are stored in the static domain, if it is a reference type, it points to the new object ).

     Constant pool:

A constant pool refers to some data that is determined during the compilation period and stored in the compiled. class file. Besides containing the basic types (such as int and long) defined in the Code and the constant values (final) of the object type (such as String and array) it also contains some symbolic references that appear in the form of text, such as fully qualified names of classes and interfaces, field names and descriptors, methods and names and descriptors.

Note: The String type is not a basic data type, so it can create an object and its value is placed in the constant pool.

1.5.Static

Each new object is assigned a new value to the member variable. The static member variables are unique and fixed, and shared to all members. The accessed static variables occupy the same block memory.

. Package

The package name is for the company, com. company. lksk. sk.

Inport package name for the call package. * (all classes under the call package)

Or import package name. Class Name (call a specific class)

The package called during the call must be in the same directory as the program, the class must be in the/com/company/lksk/sk/folder or the parent directory of the top-level package of the class file (the parent directory of the com file) located in the classpath

To execute a class, you need to write the full-outsourcing name, that is, java com. company. lksk. sk. Dog.

In the jre/lib folder of jdk, rt. jar, including various types of java, and Java. classes in the lang package can be used directly (including System. class String. class, etc.), no import is required, and all others need import

Generate a jar package, change the directory to the parent directory of the upper-level package of the class file, and execute jar-cvf test. jar *. * (name of the jar package)

1.7. Differences between Java se ee me:

Java SE = Java Standard Edition
Java EE = Java Enterprise Edition
Java ME = Java Mobile Edition

SE (Standard Edition) is mainly used for desktop programs and console Development (JFC)
EE (Enterprise Edition) enterprise-level development (JSP, EJB): website development and B/s-based programming
ME (mini version) embedded development (mobile phone, small household appliances): Mobile Phone software game programming

1. Java SE (Java Platform, Standard Edition ). Java SE is previously called J2SE. It allows the development and deployment of Java applications used in the desktop, server, embedded environment, and real-time environment. Java SE Contains classes that support Java Web service development and provides the foundation for Java Platform and Enterprise Edition (Java EE.

2. Java EE (Java Platform, Enterprise Edition ). This version was previously called J2EE. The Enterprise Edition helps develop and deploy portable, robust, scalable, and secure server-side Java applications. Java EE is built on the basis of Java SE. It provides Web Services, component models, management and communication APIs, and can be used to implement enterprise-level service-oriented architecture (service-oriented architecture, SOA) and Web 2.0 applications.

3. Java ME (Java Platform, Micro Edition ). This version was previously known as j2-based. Java ME provides a robust and flexible environment for applications running on mobile devices and embedded devices (such as mobile phones, PDAs, TV set-top boxes, and printers. Java ME includes flexible user interfaces, robust security models, many built-in network protocols, and rich support for dynamic download of network connections and offline applications. Applications Based on Java ME specifications can be used on many devices only once, and the local functions of each device can be used.

1.8.usage of the random class in java 1.8.1Random class (java. util)

The Random algorithm implemented in the Random class is pseudo-Random, that is, Random with rules. When a random algorithm is executed, the numbers of the origins of the random algorithm are called seed. A certain number is transformed based on the number of seeds to generate the required random numbers.

For a Random object with the same number of seeds, the Random numbers generated by the same number of times are identical. That is to say, two Random objects with the same number of seeds have identical Random numbers generated for the first time and identical Random numbers generated for the second time. This requires special attention when generating multiple random numbers.

Methods In the Random class are relatively simple, and the functions of each method are easy to understand. It should be noted that the Random numbers generated by each method in the Random class are evenly distributed, that is, the number generation probability within the range is equal. Below are some basic introductions to these methods:

1.8.2 common methods in the Random class

A, public boolean nextBoolean ()

This method is used to generate a random boolean value. The probability of true and false values generated is equal, that is, the probability is 50%.

B. public double nextDouble ()

This method generates a random double value between 0 and 1.0.

C, public int nextInt ()

The function of this method is to generate a random int value, which is between the int range, that is, between-231 and 231-1.

If you need to generate the int value of the specified range, you need to perform a certain mathematical transformation. For details, see the Code in the following example.

D. public int nextInt (int n)

The function of this method is to generate a random int value, which is an interval between [0, n), that is, a random int value between 0 and n, contains 0, not n.

If you want to generate the int value of the specified range, you also need to perform a certain mathematical transformation. For details, see the Code in the following example.

E. public void setSeed (long seed)

This method is used to reset the number of seeds in the Random object. The Random object after the number of seeds is set is the same as the Random object created with the new keyword for the same number of seeds.

Example:

Random r = new Random ();

Generate Decimals in the range [1, 2.5): double d3 = r. nextDouble () * 1.5 + 1;

Generate an integer in the range [-3, 15): int n4 = r. nextInt (18)-3;

1. 9. Polymorphism

Polymorphism is actually, when calling a method, if the method is overwritten, the actual method to call depends on what your new object is.

Dynamic binding is used to determine the actual type of the referenced object during execution (rather than during compilation) and call corresponding methods based on the actual type.

Three necessary conditions for Polymorphism: 1. Inheritance 2. Rewriting 3. parent class reference pointing to subclass object. When the three conditions are met and the override method in the parent class is called, the method of the new object is called.

1.10.abstract/object Class

The abstrcat abstract method is used to override it. It only serves as the definition and is rewritten in the subclass. The abstract method must be in the abstract class. Abstract classes must be inherited and cannot be instantiated. abstract methods must be rewritten. Abstract METHODS only need to be declared and not implemented. When the subclass inherits the abstract class, you must override the methods in the abstract class.

abstract class Animal(){   public abstract void enjoy();}

True is returned only when the equals function in the object class points to the same reference. Therefore, you must override the equals function.

1.11.final

The final variable value cannot be changed, the final method cannot be rewritten, and the final class cannot inherit.

1. 12. Interface

An interface is a set of abstract classes and constant definitions. In fact, an interface is a special abstract class that only contains the definition of a constant method, without the implementation of variables and methods. The implementation of interface methods must use class to implements

public interface Runner(){public static final int id = 1;public void start();public void run();}

The attribute declared in the interface can only be public static final (whether you write or not), the method can only be an abstract method (whether you write or not), and must be public.

Implements

Example interface Singer () {Public void sing (); Public void sleep ():} class Student implements Singer {}

Classes can only be individually inherited. One subclass can only inherit one parent class. One class can implement multiple irrelevant interfaces, and multiple irrelevant classes can implement the same interface.

Example: class Teacher implements Singer, Painter (){}

Class Teacher extends School implements Singer, Painter (){}

When an interface inherits from another interface, the class that implements the interface must override all methods of the two interfaces.

Related Article

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.