Java Note 1 (2015-8-30)

Source: Internet
Author: User

Focus on: The scope of the variable, the declaration and operation of the String object.

1, the method defines the formula:

    1 +  return value type   + parameter list method body; 2) Classification of methods: There are no return value types and the number of returned parameters is categorized. 3) method Invocation: Object name parameter list );

Note:1) The method name of the normal method cannot be the same as the class name (constructor method). 2) If the method does not return a value type, then the return value type should write:void. 3) in the method body, the returned data should be returned using the return keyword to return the data that needs to be returned. ( return returns the type of data to be associated with the method's return value type).

2, variables are divided into: global variables (scoped to the entire class) and local variables (the scope of the variable is declared in the method or domain). Add: In java , there is a principle of proximity.

3. use and Operation ofString objects ( Most frequently used in Java )

A) How to define

    1. String variable name = initialization data;
    2. String variable name (object name) = new string ();

b) use of common methods of String objects

    1. Equals():String Object compares the contents of the same
    2. Split(): Splits a string, returns an array of strings
    3. Substring(): String intercept, return new string

Program Example: how to explain and use the method

public class test{

--no return value type, no argument list

public void Play () {System.out.println ("--play--"); }

--no return value type, parameter list

public void Call (String name) {System.out.println (name+), you come to the office now!  "); }

--There is a return value type, no argument list: Returns a string

Public String speak () {return "My name is Xiao Hong"; }

--There is a return value type, a parameter list: int

public int getage (String name) {return 20; }

public static void Main (string[] args) {

--Instantiating an object

Test test = new test ();

--Call of method

Test.play ();

Test.call ("Xiao Ming");

String name = Test.speak ();

System.out.println ("Name:" +name);

int age = Test.getage ("Xiaoming");

System.out.println ("Age:" +age);

}

}

2, the variable scope of the explanation, use

public class test1{

String str = "I am a global variable";

public void Play () {

String str1 = "I am a local variable";

System.out.println ("play-local variable:" +STR1);

}

public void Cs () {

SYSTEM.OUT.PRINTLN ("Global variable:" +STR);

SYSTEM.OUT.PRINTLN ("local variable:" +STR1);

}

public static void Main (string[] args) {

Test1 test = new Test1 ();

Test.cs ();

Test.play ();

} }

3.

public class test2{

String str = "I am a global variable";

public void Cs () {

String str = "I am a local variable";

System.out.println ("str:" +STR);

}

public static void Main (string[] args) {

Test2 test = new Test2 ();

Test.cs ();

} }

4. Use of String objects

public class test3{

public static void Main (string[] args) {

--Comparison of string contents

String sex = "male";

if ("Female". Equals (Sex)) {

SYSTEM.OUT.PRINTLN ("The information you have entered is female");

}else if ("Male". Equals (Sex)) {

SYSTEM.OUT.PRINTLN ("The information you entered is male");

}else{

SYSTEM.OUT.PRINTLN ("The information you entered is wrong!") Think about it and then enter!!! ");

}

Use of--split

String str = "Ni hao ma?";

string[] arr = Str.split ("");

System.out.println (arr.length);

--substring: The Intercept is started by the subscript of each character of the string (space is not counted) string str1 = Str.substring (4);

System.out.println ("str1:" +STR1);

Starting from subscript 3, to the end of subscript 5, 5-3=2 output 2 characters.

String str2 = str.substring (3,5);

System.out.println ("str2:" +STR2);

} }

Java Note 1 (2015-8-30)

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.