Java Season 1,

Source: Internet
Author: User

Java Season 1,

1. Application of Java Constants

Syntax: final constant name = value;

final String LOVE = "IMOOC";final double PI = 3.14

A simple example

Public class HelloWorld {2 public static void main (String [] args) {3 final char SEX1 = 'male'; 4 final char SEX2 = 'female '; 5 System. out. println (SEX1); 6 System. out. println (SEX2); 7} 8}

2. Java Array

Public class HelloWorld {public static void main (String [] args) {// defines an array and saves the scores of five students int [] scores = {78, 93, 97, 84, 63}; // The second result in the output array: System. out. println ("the 2nd scores in the array are:" + scores [1]);}

In Java, operations on arrays only take four steps:

(1) declare an array

Syntax: array type [] array name;

Or array name [];

(2) Allocate space

Syntax: array name = new data type [array length];

You can also directly merge

Public class HelloWorld {public static void main (String [] args) {// defines an array of 5 strings, save exam subject information String [] subjects = new String [5]; // assign values to the elements in the array respectively. subjects [0] = "Oracle "; subjects [1] = "PHP"; subjects [2] = "Linux"; subjects [3] = "Java"; subjects [4] = "HTML"; System. out. println ("4th subjects in the array:" + subjects [3]);}

3. Use the Arrays class to operate Arrays in Java

The Arrays class is a tool class provided by Java in the java. util package. This class contains some methods for directly operating arrays, such as sorting and searching arrays.

Common Methods in Arrays:

(1) sorting

Syntax: Arrays. sort (array name );

You can use the sort () method to sort the array. You only need to put the array name in the brackets of the sort () method to sort the array in ascending order ).

 

The running result is:

(2) convert an array to a string

Syntax: Arrays. toString (array name );

You can use the toString () method to convert an array into a string. This method concatenates multiple array elements in order and separates them by commas and spaces.

The running result is:

Elements in the output array nums: [,]

4. Use foreach to operate the Array

Syntax:

 

For (element type element variable: traversing object) {executed code}

 

Import java. util. arrays; public class HelloWorld {public static void main (String [] args) {// defines an integer array and saves the score information int [] scores = {89, 72, 64, 58, 93}; // sort the array in the Arrays class. sort (scores); // use foreach to traverse the elements in the output array for (int score: scores) {System. out. println (score );}}}

5. Method overloading in Java

If the same class contains two or more methods with the same name, number of method parameters, order, or type, it is called method overload, this method can also be called overloaded.

Public class HelloWorld {public static void main (String [] args) {// create the object HelloWorld hello = new HelloWorld (); // call the hello method without parameters. print (); // call the method with a string parameter hello. print ("Dragon"); // call the hello method with an integer parameter. print (2);} public void print () {System. out. println ("print method without Parameters");} public void print (String name) {System. out. println ("print method with a string parameter, parameter value:" + name);} public void print (int age) {System. out. println ("print method with an integer parameter, parameter value:" + age );}}

6. Java Constructor

(1) use the new + constructor to create a new object.

(2) constructor is a method defined in a Java class to initialize objects. constructor has the same name as the class and has no return value.

7. static variables used in Java

In some cases, we want all objects of this class to share the same member. It's time for static to show off!

Static modified members in Java are called static members or class members. It belongs to the whole class, rather than an object, that is, it is shared by all objects of the class. Static members can use class names for direct access or object names for access.

Running result:

 

8. static usage in Java

Running result:

 

Note the following three points:

(1) Static members of the same type can be called directly in static methods, but non-static members cannot be called directly.

To call non-static variables in a static method, you can create a Class Object and access non-static variables through the object. For example:

 

(2) In common member methods, you can directly access non-static and static variables of the same type, as shown below:

(3) Non-static methods cannot be called directly in static methods. You need to access non-static methods through objects. For example:

9. static initialization Block Used in Java

The class declaration can contain multiple initialization blocks. When a class instance is created, these code blocks are executed in sequence. Static initialization blocks are called static initialization blocks.

Note: The static initialization block is only executed when the class is loaded and will only be executed once. At the same time, the static initialization block can only assign values to static variables and Cannot initialize common member variables.

 

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.