Variable of class (static variable)
Object variable (not static variable)
Arrays are objects that store multiple variables with the same data type. However, an array itself is also an object in memory. We will also learn how to declare, build, and initialize this object in future articles.
Enum was introduced in Java 5.0. Enum restricts a variable so that it can only receive one value among several predefined values. Easier to understand, enum is used to define a set of fixed numbers, such as day of week, season of year, .
Using enum can help reduce the number of errors in the code.
For example, we assume an application for fruit juice shops, which can limit the size of small, medium and large cup sizes. This can help ensure that others cannot add sizes other than small, medium and large.
class FreshJuice { enum FreshJuiceSize { SMALL , MEDIUM , LARGE } FreshJuiceSize size ; } public class FreshJuiceTest { public static void main ( String args []){ FreshJuice juice = new FreshJuice (); juice . size = FreshJuice . FreshJuiceSize . MEDIUM ; System . out . println ( "Size: " + juice . size ); } }
Output result:
Size: MEDIUM
Note: Enum can be declared individually or declared in a class. Procedures, variables, constructors can also be defined in Enum.
The list below is the keyword (key) in Java. They cannot be used as constants, variables or any other identifier. So when naming variables, constants, procedures, classes, . you have to avoid these keywords.
abstract assert boolean break byte case class const continue catch default by double else enum extends final finally float for goto if implements import instanceof int giao diện mới không có gói không được bảo vệ public return short static strictfp super chuyển đổi đồng thời không đúng.Java supports commenting on one line or multiple lines like C, C ++, Python. All characters in the comment will be ignored by the Java compiler.
For example:
public class MyFirstJavaCode { /* Đây là chương trình Java đầu tiên. * Nó sẽ in ra màn hình 'Hello World'. * Đây là ví dụ comment trên nhiều dòng.
* By TipsMake.com */ public static void main ( String [] args ) { // Đây là ví dụ comment trên 1 dòng. /* Đây vẫn là ví dụ comment trên một dòng by TipsMake.com. */ System . out . println ( "Hello World" ); } }
Output:
Hello World
A line only contains spaces, possibly comments, called blank lines, and Java completely ignores it.
In Java, classes can be deduced (or derived) from other classes. If you need to create a new class that already has a class containing the code you need, you can deduce the new class from that existing class.
Inheritance allows you to reuse existing class procedures and procedures without rewriting in the new class. In this case, the existing class is called a superclass, the new class is deduced called a subclass.
In the Java programming language, an interface can be defined as a contract between objects on how to communicate with each other. Interfaces play an essential role when it comes to the concept of inheritance.
An interface defines the procedures, subclasses should use. But the implementation of methods is entirely dependent on subclasses.
Next lesson: Write and run Java code on the computer for the first time
Previous article: Download and install Java on the computer