Basic Java syntax
A Java program can be defined as a collection of objects, communicating through calling each other's procedures. Now we will look at the class, object, procedure and instance variable / instance variable to consider their meaning.
A Java program can be defined as a collection of objects, communicating through calling each other's procedures. Now we will look at the class, object, procedure and instance variable to consider their meaning.
- Subjects: Subjects with status and behavior. Example: A flower has states such as: name, color, scent; behavior: blooming, dying. An object is an instance of the class.
- Class: A class can be defined as a detailed template / design, describing the behavior / state that its object supports.
- Procedure (or method): A procedure is basically a behavior. It is where logic is written, data is manipulated and all actions are performed.
- Entity variables: Each object has a unique set of entity variables. The state of the object is created by the values assigned to these variables.
You can meet these concepts in familiar object-oriented programming languages like C ++, Python, PHP. And in the context of Java, I will only talk about them in the Java language. Below, is the difference between the class and the object in Java.
Basic Java syntax
Before starting to dive into Java programming, you must keep in mind the following points:
- Distinguish between upper and lower case letters: Java is a case-sensitive language, meaning that you write Quantum, quantrimang or QuanTangMang, QUANTRIMANG will have different meanings.
- Class name: For all class names, the first letter should be in capital letters. If you use multiple words to form a class name, the first word in the word should be in capital letters.
Example: class JavaFirst, class QuanTriMang
- Procedure name: All procedure names should start with lowercase letters. If multiple words combine multiple words into the procedure's name, after the first word, the first letters of the word should be in capital letters.
Example: public void myQuanTriMang ()
- Program file name : The file name must match the class name exactly. When saving the file, you name the file as the class name and the extension ".java", remember that Java is case sensitive, if the file name and the class name do not match, it cannot be compiled.
For example: Assuming JavaFirst is the class name, the file must be JavaFirst.java
- public static void main (String args []): The Java program starts with the main () procedure, which is a required part of every Java program.
Identifier in Java
All Java components must be named. The name used for class, variable, and procedure is called the -identifier identifier.
There are several points to note about identifiers:
- All identifiers should begin with a letter (A to Z or a to z), a monetary character ($) or an underscore (_).
- After the first character, you can use any character to write an identifier.
- A keyword cannot be used as an identifier.
- Identifiers are also case sensitive.
- Examples of correct identifiers: tuoi, $ luong, _giatri_, __1_giatri.
- Examples of false identifiers: 123ghi, -tien.
Modifier in Java
Like other programming languages, you can modify classes, methods, . using Modifiers. In Java, there are two types of Modifiers:
- Access Modifier: Include default, public, protected, private.
- Non-access Modifier: Including final, abstract, strictfp.
We will learn about these Modifiers in the next tutorials.
Variable in Java
Java has the following types of variables:
- Local variable
Variable of class (static variable)
Object variable (not static variable)
Array in Java
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.
Java Enum
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.
Keywords in Java
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.Comment in Java
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
Use blank lines
A line only contains spaces, possibly comments, called blank lines, and Java completely ignores it.
Inheritance in Java
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.
Interface in Java
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
- 200 common Java interview questions, with reference answers
- Basic Java exercises, with sample decoding (some)
- What is Java? Why choose Java?
You should read it
- What is Java? Why choose Java?
- Basic Java exercises, with sample decoding
- Download and install Java on the computer
- What is JAVA file? How to open, edit and convert JAVA files
- eQuiz - Multiple choice quiz about JAVA
- Which career Java programming options are waiting for you?
- How to Compile a Java Program
- How to install Java on a Raspberry Pi
- How to fix the error does not install Java
- Write and run Java code on the computer for the first time
- How to Tune a Java Virtual Machine (JVM)
- How to Fix Java
Maybe you are interested
Thousands of iOS apps could be at risk because of an open source vulnerability How to track the amount of data used on Android The strange forest area looks like a chessboard when viewed from above Impressive image: ISS International Space Station flies over the Sun. Russian spacecraft 'resupply' successfully 3 tons of cargo for the ISS in just 3 hours How clean are astronauts keeping the space station?