In java learning, the instanceof keyword, final keyword, value transfer (small record in java learning), instanceoffinal

Source: Internet
Author: User

In java learning, the instanceof keyword, final keyword, value transfer (small record in java learning), instanceoffinal

In java learning, the transmission of instanceof keywords, final keywords, and values (small records in java learning)Author: Star)

 

Instanceof keyword

Purpose:

1. used to determine whether an object belongs to a class.

2. Prerequisites for using the instanceof Keyword: the class specified by the object has an inheritance or implementation relationship.

3. Force type conversion will be used Instanceof keyword.

For example:

Student s = (Student) new Person (); // to do so, Student must inherit Person.

Boolean res = s instanceof Person

If (res ){

Student s = (Student) new Person ();

}

The Code is as follows:

1 package study; 2 3 class Person {4 String name; 5 public Person (String name) {6 this. name = name; 7} 8 public Person () {} 9} 10 11 class Student extends Person {12 13} 14 15 public class star {16 public static void main (String [] args) {17 Person star = new Person ("star"); 18 boolean result1 = star instanceof Person; 19 System. out. println (result1); // ture20 21 Student s = new Student (); // The default value is 22 23 boolean result2 = s instanceof Person. out. println (result2); // ture25} 26 27}

Final keywords (modifier, final)

1. If you use a final keyword to modify a basic data type variable, you cannot assign a new value if you change the value. The first result is the final result.

2. If you use final to modify the referenced data type, the variables that reference the data type cannot be assigned a value.

For example, this is wrong.

Final Yuan yuan = new Yuan (10 );

Yuan = new Yuan (100); // The value cannot be re-assigned.

3. If final modifies a method, the method cannot be overwritten.

4. If final modifies a class, the class cannot be inherited.

5. The final variable must be initialized, or an error will be reported.

1 package study; 2 3 class Yuan {4 int r; 5 final double pai = 3.14; // It should be a constant volume, otherwise, the parameter below will be modified if it is not 3.14. Similar to constants 6 7 public Yuan (int r/*, double pai */) {8 this. r = r; 9/* this. pai = pai; */10} 11 12 public void area () {13 System. out. println ("the area of the circle is" + r * pai); 14} 15} 16 17 public class star {18 public static void main (String [] args) {19 Yuan yuan = new Yuan (10/*, 3.14 */); // 20 yuan will be modified if not modified. area (); 21} 22}

How to Use final to represent constants:

Format:

Public final static basic data type variable name

 

Value Transfer

Assign values between basic data types. Actually, assign the value of a variable to another variable.

If the parameter is of the reference type, the transmitted parameter is the address of the parameter reference type data.

Reference Type: Class, interface, array

 

1 package study; 2 3 public class star {4 public static void main (String [] args) {5 6 // requirement: define the number of two integers, exchange two numbers in one way: 7 int a = 10; 8 int B = 20; 9 change (a, B); 10 System. out. println ("a =" + a + ", B =" + B); // 10.2011 12 // requirement: Define an array, switch the positions of two numbers 13 int [] arr = {}; 14 changeArr (arr); // 20, System. out. println ("arr [0] =" + arr [0] + "arr [1] =" + arr [1]); 16} 17 18 public static void change (int a, int B) {19 int temp = a; 20 a = B; 21 B = temp; 22} 23 public static void changeArr (int [] arr) {24 int temp = arr [0]; 25 arr [1] = arr [0]; 26 arr [0] = temp; 27} 28}

 

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.