Java basics: Several confusing questions for beginners

Source: Internet
Author: User
Java basics: Several confusing questions for beginners-Linux general technology-Linux programming and kernel information. For details, refer to the following section. 1. String and StringBuffer classes

They are all String-processing classes, but they have the biggest difference, that is, String objects are text characters that you cannot change.
String. Instead, use the StringBuffer class as the replacement if you want to change it.
Eg1:
......
// Omit some code
String s1 = "You are hired! ";
System. out. println (s1.replace ('h', 'F'); // replace h in the string with f.
System. out. println (s1 );
......
// Omit some code
Running result:
You are fired!
You are hired!
Result Analysis:
From the results, it is obvious that the s1 value is not changed, and the first line of results is only a replacement of the screen content.
Eg2:
......
// Omit some code
StringBuffer s2 = new StringBuffer ("Hello from Java! ");
S2.replace (6, 10, "");
System. out. println (s2 );
......
// Omit some code
Running result:
Hello to Java!
Result Analysis:
Obviously, the s2 value has changed.

2. Bit logic and conditional Logic

First, we declare that in order to better distinguish the logic from the bit logic, I take the commonly referred logic as an alias called conditional logic.
They all have their own operators. Bit logical operators include: & (and operation), ^ (exclusive or operation), | (or operation); conditional logical operators include: & (and)
And), | (OR ).
Bit logical operations are usually performed on two numbers, while conditional logical operations are performed on two conditional expressions.
In fact, bitwise logical operators can implement conditional operations, but there is an important difference: when using bitwise operators, regardless of
If the conditional expression is not true, it must be used for calculation and judgment. The conditional logical operators are different.
Then, it will not calculate the right operand. This is short-circuit! See the following example.
Eg1:
......
// Omit some code
Double value = 0;
If (value! = 0 & 1/value <1000 ){
System. out. println ("The value is not too small .");
}
Else {
System. out. println ("The value is too small .");
}
......
// Omit some code
Running result:
The value is too small.
Result Analysis:
It should be said that there should be an error where the divisor is 0, but as I said just now, because the conditional logical operator is a short-circuit operator, obviously, value! = 0 No
If it is true, you can immediately make the statement after else is executed, so it will no longer calculate and Judge 1/value <1000. If you do not understand it, please read another one.
Example:
Eg2:
......
// Omit some code
Double int1 = 0, int2 = 1, int3 = 1;
If (int1! = 0 & (int2 = 2) = 1 ){}
System. out. println ("int2 =" + int2 );
If (int1! = 0 & (int3 = 2) = 1 ){}
System. out. println ("int3 =" + int3 );
......
// Omit some code
Running result:
Int2 = 2.0
Int3 = 1.0
Result Analysis:
I don't want to analyze it. You should understand it.

3. instance variables and class variables

You can use two methods to store data in a class: instance variables and class variables. instance variables are specific to objects. If you have two
For example (two instances of a class), the instance variables in each object are independent of the instance variables in another object.
Class variables all point to the same data, and therefore save the same value. In other words, class variables are shared by all objects in the class.
Formal differences: class variables are declared with one more static variable than the instance variables.
Eg:
Class data
{
Public int intdata = 0; // apparently, intdata is an instance variable here
}
Public class exam
{
Public static void main (String [] args)
{
Data a, B;
A = new data ();
B = new data ();
A. intdata = 1;
System. out. println ("B. indata =" + B. intdata );
}
}
Running result:
B. intdata = 0
Result Analysis:
It can be seen that although the value of a. intdata has changed, it does not affect B. intdata. However, if intdata is declared in the data class
When static is added to the surface, it becomes a class variable (that is, public static int intdata = 0;). Then, the running result is changed:
B. intdata = 1
This change in the. intdata value affects B. intdata. In fact, the class variables of object a and object B point to the same data.
This is the role of a class variable.

4. instance method, class method, constructor Method

The method we usually call refers to the instance method, just like the function in C language. I don't need to talk about the specific method. Here I mainly
It is used to partition the classification method and constructor method. The biggest difference between the class method and the instance method is that there is one more static class method in the form. In terms of usage,
You can directly call the class method without creating an object (but the instance method must first create an object and then call it through the object ).
Eg:
Class add
{
Static int addem (int op1, int op2)
{
Return op1 + op2;
}
}
Public class xxf
{
Public static void main (String [] args)
{
System. out. println ("addem (2, 2) =" + add. addem (2, 2 ));
} // Call the class method directly using the class name as the object
}

Note: You can also create an object and call the method based on the common method. However, static does not make any sense.
Let's talk about the constructor method. It is a method used to initialize the data in the object. It is easy to create a constructor. You only need to add a constructor with this class in the class.
Methods With the same name do not need to add any access specifiers or return types before them. In addition, the constructor can also pass parameters like methods.
Eg:
Class data
{
Private String data1; // stated in advance

Data (String s)
{
Data1 = s;/* initialize the variable by receiving data. (Note: The variable cannot be in the constructor.
Declare the variable. Declare the variable beforehand .)*/
}

Public String getdata ()
{
Return data1;
}
}

Public class xxf
{
Public static void main (String [] args)
{
System. out. println (new data ("I love you"). getdata ();/* call the constructor to create a new one by passing Parameters
Objects, and then get the data through the object call Method */
}
}

5. interfaces and Classes

A class is a type description of a specific object. we can define a class to create an object and combine all components belonging to the class by creating an object.
The interface cannot be implemented in this way. The interface is essentially a set of constants and abstract methods. To use an interface, you need to implement this interface in the class.
Interface, and then as part of the class definition, write every method declared in the interface, the method in the interface is always public, abstract, in the interface
Constants are always public static and final, so you do not need to describe attributes for them.
Java does not support multi-inheritance. However, interfaces can be used to implement similar functions, which is one of the important functions of interfaces.
Eg:
Interface anyone // define an interface
{
Final double PI = 3.1416;
Void setNumber (int number );
Int getNumber ();
}
Interface anyother // define another interface
{
Void setString (String str );
String getString ();
}

Class xxf implement anyone, anyother // defines a class and uses two interfaces
{
Int number;
String str;
Public xxf (){}
Void setNumber (int number)
{
This. number = number;
}
Void setString (String str)
{
This. str = str;
}
Void int getNumber () {}// it can be an empty implement.
Void String getString (){}
}
// All methods declared in the interface must be implemented in the class. (Of course, this is not necessary, but the adapter class or abstract class must be used)
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.