Introduction to java basics and fields

Blogger Profile: Former employee of Tencent, an Internet security giant, employee of Venustech, an Internet security giant, expert blogger of Alibaba Cloud development community, high-quality creator of basic notes on Java on WeChat public account, high-quality creator blogger of csdn, entrepreneur, knowledge sharer, welcome to follow , like, favorite.


First, the introduction of the class

  If everything is an object, what determines the appearance and behavior of a "class"? Most object-oriented languages ​​use the keyword "class" to express such a meaning, so we also use class to declare a class in java. For example, java teacher, python teacher, big data teacher, language teacher, math teacher, etc., we can all call them teachers, extract features to create a class.insert image description here


Second, define the class

  Even though we use class to represent a class, let's see how to declare a class.

class Teacher {
/*类主体置于这里
*/
}
复制代码

  In this way, a new type is introduced, and my class is successfully created. Although there is nothing in the class body, I can also use new to create a new object of this type.

Teacher  a = new Teacher ();
复制代码

  The class body consists of only one comment, so there isn't much you can do with it. In fact, it cannot be instructed to do anything at all unless some method is defined for it.


3. Fields

  When you define a class, you can set two types of elements in your class: fields and methods. Among them, the field is an object, which can be a basic data type or a handle to the object, then the handle must be initialized, as seen earlier, using the new keyword.

  Each object maintains storage for its own data members; data members are not shared between objects.   Here is an example of a class that defines a teacher:

class Teacher {
 int age;
 float money;
 boolean isBeautiful;
 Student student;
}
复制代码

  This class doesn't do anything substantial, but we can create an object:

Teacher  teacher = new Teacher ();
复制代码

  You can assign values ​​to data members, but you must first know how to refer to a member of an object. In order to achieve the purpose of referencing object members, first write the name of the object, followed by a period, followed by the name of the internal members of the object. That is "object name. member". E.g:

teacher.age= 18;
teacher.isBeautiful = true;
复制代码

  An object may also contain another object, and another object contains the data we want to modify. For this question, for example:

teacher.student.age = 15;
复制代码

  The teacher class can't do much more than hold the data, because it has no methods. If a primary data type is a class member, they are guaranteed to get a default value even without explicit initialization, and here is the default value case for our primary type.

main type Defaults
Boolean false
Char '\u0000'(null)
byte (byte)0
short (short)0
int 0
long 0L
float 0.0f
double 0.0d

  If the field is a main type, it can be directly initialized at the class definition location, that is, it is directly assigned when it is created. This is an example. When the above class is declared, the field is assigned the default value.

class Teacher {
 int age=18;
 float money=100000;
 boolean isBeautiful=true;
 Student student;
}
复制代码

4. Summary

   The above is about the introduction of java basics and related knowledge of field definitions, focusing on how to define java classes, how to create, initialize, and field related concepts, you can refer to it, if you think it is good, welcome to like, favorite, in Look, welcome to WeChat search and pay attention to java basic notes, and we will continue to update relevant knowledge later, and everyone will make progress together.

Guess you like

Origin juejin.im/post/7087828525301891080