Basic Java Tutorial-Object oriented (i)

Source: Internet
Author: User
Tags java se

First, object-oriented

1.1 Java Keyboard input

1.1.1 This method I think is the simplest, the most powerful, is to use the scanner class

Import Java.util.Scanner;  public static void Main (String [] args) {Scanner sc = new Scanner (system.in);  System.out.println ("Please enter your name:");  String name = Sc.nextline ();//String Input System.out.println ("Please enter your Age:");  int age = Sc.nextint ();//Integer input System.out.println ("Please enter your salary:");  float salary = sc.nextfloat ();//floating-point input System.out.println ("Your information is as follows:");  System.out.println ("Name:" +name+ "\ n" + "Age:" +age+ "\ n" + "Salary:" +salary); }

This code has shown that the scanner class, whether it's a string or integer data or a variable of type float, can do it with a little bit of change! No doubt he's the strongest.

 1.1.2

while (A.hasnext ())  {   System.out.println ("You entered:" +a.nextint ());  } Hasnext () is a method of the scanner class that determines whether there is an input, an input, A. Hasnext () is true, no input, A. Hasnext () is False

1.2. Array

Defines an array of int [] [] a=new int [10][10];

1.3. Java Variable naming conventions

1. Class Name: Capitalize the first letter of each word, example username

2. Method name and variable name: first letter lowercase, other words first uppercase

Example: Getinfor (), getinfor

    Note: The first word of a class method must be a verb

Construction method (used to complete initialization of an object): The name must be the same as the name of the class

3. Package name: All lowercase, Example: example

4, constant: All letters uppercase, if more than one word, usually connected with _

Example: Min_value,pi

1.4. Java Memory parsing

Parameters and local variables allocate memory space in the stack, run out of auto-release, the memory programmer in the heap is freed, parse the memory parsing, and start step-by-step analysis from main (). Some of the basic types of variables and object reference variables defined in the function are allocated in the stack memory of the function. Heap memory is used to hold objects and arrays created by new. The memory allocated in the heap is managed by the automatic garbage collector of the Java Virtual machine. A reference variable is a normal variable that is defined when memory is allocated in the stack, and the reference variable is released in the program run to the extraterritorial scope. The array and object itself is allocated in the heap, and even if the program runs beyond the block of code that uses the new generation of arrays and objects, the heap memory that the array and the object itself occupies will not be freed, and arrays and objects become garbage, no longer used, but still occupy memory when no reference variable points to it. is released by the garbage collector at a later indeterminate time. This is also the main reason for the memory of Java comparison, in fact, the variables in the stack point to the heap memory variables, this is the pointer in Java!

static variables (statics) are stored in the data area

New creates the object and executes the constructor directly,

  First, let's explain the various areas in memory.

Stack: a reference to a primitive type of data and an object that holds variables.

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

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

    Heap: Stores the data generated by new.

    Data segment: Divided into static and constant fields.

     Static domain:

A static member (that is, a static variable) that is defined with static in the object, in the same vein, if the static variable is the base type, the variable name and value are stored in the static domain, and if the reference type points to the object that is new.

     Chang:

Chang refers to some data that is determined at compile time and is saved in the compiled. class file. In addition to the constant values (final) that contain the various basic types defined in the code (such as int, long, and so on) and object types (such as strings and arrays), there are some symbolic references that appear as text, such as the fully qualified name of the class and interface, the name and descriptor of the field, the method and the name and the descriptor

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

1.5. Static

For each new object, the member variable is assigned a value, and the static member variable is unique, fixed, and shared with all members, and access to the statically variable occupies uniform block memory.

1.6. Package

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

Call Pack InPort package name. * (All classes under the call package)

or import package name. Class name (call a specific class)

The package that is called when called must be in the same directory as the program, and the class must be the parent directory (the parent directory of the COM file) under the/com/company/lksk/sk/folder or the topmost package of the class file in Classpath

Executes a class that needs to write the full-package name, which is the Java Com.company.lksk.sk.Dog

Rt.jar in the Jre/lib folder in the JDK, contains the various classes that the Java comes with, and the classes in the Java.lang package can be used directly (including System.class String.class, etc.) without import, and the rest require import

Generate jar package, directory to the parent directory of the top-level package of class file, execute JAR–CVF test.jar * * (JAR package name)

1.7. Java SE EE Me differences:

Java Se=java Standard Edition
Java Ee=java Enterprise Edition
Java Me=java Mobile Edition

SE (Standard Edition) primarily for desktop applications, console development (JFC)
EE (Enterprise Edition) Enterprise Development (JSP,EJB): Web development and programming based on B/S mode
ME (miniature version) embedded development (mobile, small appliances): Mobile software Game programming

1. Java SE (Java platform,standard Edition). Java SE was formerly known as J2SE. It allows you to develop and deploy Java applications that are used in desktops, servers, embedded environments, and live environments. Java SE contains classes that support Java WEB Service development and provides the foundation for Java platform,enterprise Edition (Java EE).

2. Java EE (Java platform,enterprise Edition). This version was formerly known as the Java EE. The Enterprise Edition helps develop and deploy portable, robust, scalable, and secure server-side Java applications. Built on the Java SE, Java EE provides Web services, component models, management, and communication APIs that can be used to implement enterprise-class service-oriented architecture (service-oriented Architecture,soa) and Web 2.0 applications.

3. Java ME (Java platform,micro Edition). This version was formerly known as J2ME. 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 a flexible user interface, robust security model, many built-in network protocols, and rich support for Web and offline applications that can be dynamically downloaded. Applications based on the Java ME specification need to be written once, can be used for many devices, and can take advantage of the native capabilities of each device.

1.8. Use of the random class in Java1.8.1Random Class (Java.util)

The random algorithm implemented in the random class is pseudo-random, that is, random with rules. At random, the origin of the random algorithm is called the seed number (seed), the number of seeds on the basis of a certain transformation, resulting in the need for random numbers.

Random objects of the same seed number are identical in number. That is, two random objects with the same number of seeds, the first generation of randomly generated numbers exactly the same, the second generation of random numbers are exactly the same. This requires special attention when generating multiple random numbers.

The methods in the random class are relatively simple, and the functionality of each method is easy to understand. It should be stated that the random numbers generated by each method in the random class are evenly distributed, that is, the probability that the numbers within the interval are equal. Here's a basic introduction to these methods:

common methods in 1.8.2 and random classes

A, public boolean nextboolean ()

The function of this method is to generate a random Boolean value that generates a value of true and false that is equal to the probability of 50%.

B, public double nextdouble ()

The function of this method is to generate a random double value between [0,1.0].

c, public int nextint ()

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

If you need to generate an int value for a specified interval, you need to make some mathematical transformations, as shown in the following example of using code.

d, public int nextint (int n)

The function of this method is to generate a random int value that is in the interval of [0,n], which is a random int value between 0 and N, containing 0 without n.

If you want to generate an int value for a specified interval, you also need to make some mathematical transformations, see the code in the example below.

E, public void setseed (long Seed)

The function of this method is to reset the number of seeds in the random object. The random object with the same seed number after the seed number is set is the same as the random object created with the New keyword

Examples of methods:

Random r = new Random ();

Generate decimal for [1,2.5] interval: Double d3 = r.nextdouble () * 1.5 + 1;

Generates an integer for the [ -3,15) interval: int n4 = R.nextint (18)-3;

1.9. polymorphic

Polymorphism is actually, when invoking a method, if the method is rewritten, which method is actually called, depends on what your new object is.

Dynamic binding is the actual type of the referenced object that is judged during execution, not during compilation, and its corresponding method is called according to the actual type.

three prerequisites for polymorphism: 1, to have inheritance 2, to have rewrite 3, the parent class reference to the child class object . When three conditions are met, the overridden method in the parent class is called, and the object's method is actually called on what object of new.

1.10. Abstract class/object Class

  Abstrcat abstract methods are used for rewriting, which are only defined by themselves and overridden in subclasses, and abstract methods must be in an abstract class. Abstract classes must be inherited, abstract classes cannot be instantiated, abstract methods must be overridden, and abstract methods need to be declared without implementation. When a subclass inherits an abstract class, it must override the methods in the abstract class.

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

The Equals function in the object class must be a pointer to the same reference to return true, so it is generally necessary to override the Equals function.

1.11. Final

Final variable value cannot be changed, final method cannot be overridden, final class cannot inherit

1.12. Interface

An interface is a collection of abstract and constant definitions, in fact an interface is a special abstract class that contains only the definition of a constant method, without the implementation of variables and methods. The implementation of the interface method must be implements with class

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

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

Implementation of the interface with implements

Example Interface Singer () {public void Sing (), public void Sleep ():}class Student implements singer{}

  A class can inherit only one parent class, and a class may implement multiple unrelated interfaces, and multiple unrelated classes can implement the same interface.

Example: Class Teacher implements Singer,painter () {}

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

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

Basic Java Tutorial-Object oriented (i)

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.