CTYPE html> Lesson 4: Literal, Constants and Data I/O methods in Java 9 programming – Training professional programmers

It-Pro Training

Lesson 4: Literal, Constants and Data I/O methods in Java 9 programming

Lesson 4: Literal, Constants and Data I/O methods in Java 9 programming

Lesson4 : Literal, Constants and Data I/O methods in Java 9

1. Hang – Literal

Literal represents the specific value then attached to the variable. It is represented directly in the program code and requires no computation.

Watching: What is Literal

The following figure depicts some values ​​that have a primitive data type or a primitive data type.

Figure 1: Example literals

Just like data types, literals have several types as follows:

1.1Integer Literal

Literal has an integer type used to represent values ​​of type int. In Java programming integer values ​​are stored with a width of 32 bits. In the program, int is probably the most commonly used type.

Integer literals can be expressed as follows:

+ The stored value of the variable belongs to the decimal system (according to base 10, meaning values ​​representing numbers 0 -> 9). For example: intdecNumber = 50;

+ The stored value of the variable belongs to the hexadecimal system (Hexadecimal or Hexadecimal), the stored values ​​can contain from 0 to 9 and from A to F. For example: int hecNum = CF7;

+ The stored value of the variable belongs to the binary system, the value is based on 2 numbers 0 and 1.

For example int binNum = 0010;

A literal has an integer value, which can be assigned other values ​​such as bytes or longs. When the assigned literal is of type byte or short, no error occurs. However, if assigning to values ​​with a data type larger (long type) than int, a suffix must be used to indicate that we are using (‘l’) or (‘L’) to tell the compiler. The number is of type long (64 bit integer type).

READ MORE  Should You Use Auto

1.2Floating-point Literals – Floating point constants

Floating-point literals represent integer values ​​but contain decimals or in other words represent the value of real numbers. This literal contains the following parts:

+ Numeric component – ​​Contains integers from 0 to 9. For example 0, 1, 2…, 9

+ Decimal point – Example: 4.90, 3.141

+ The exponent is represented by E or e followed by a decimal, it can be negative or positive; For example, e+307, 5.123E6, 453E-05

+ Type of suffix D, d, F or f (D is double, F is float).

1.3Boolean Literals – Boolean constants

These literals represent the value true or false. In Java programming true and false cannot be converted to a numeric type, which means that true cannot be converted to 1 and false cannot be converted to 0.

1.4Character Literals – Character constants

These literals represent values ​​of type char, and their value is enclosed in single quotes ”. All values ​​in the ASCII encoding can be represented in this way. For example ‘g’, ‘$’, ‘z’.

1.5Null Literals – Constant null

With objects when initialized, the compiler will allocate a memory area to store the information of the variable value. The variables will then have a reference data type (a reference to a memory address). However, when we want to initialize the object but we do not want to allocate the memory area to store the value for that variable, we will initialize it with a null value. Example: Employee emp = null;

1.6String Literals – String constants

String Literal contains information about a stringcharacter, its value is set in the pair of “”; Example: String namEmp = “Nguyen Ban”;

READ MORE  What is Market Cap, Market Cap Value

2. Escape sequence – Line processing character or control character

In the usual sense, Escape sequense is a sequence of characters, it is often used with a number of specific operations such as inserting a tab into the display string or printing a double jump … in other words, the Escape sequence is line handler or control character in Java.

Control characters are used to represent special characters. Control characters can be used for character constants or string constants.

In Java programming the following table describes the Escape sequence.

See also: What is a Martingale – Trading Methods

Table1 : Description of Escape sequence

Eg :

Figure 2: Illustrated example of Escape sequence

Figure 3: Example run results

3. Hang

Constant is also a variable, but the value of the constant does not change. Variables are very powerful tools, but when working with a value that is defined as immutable, the programmer must ensure its value is not changed during the program.

3.1 Declare constants

In Java programmers can declare constants for all data types. When declaring a constant, it is imperative to set the value of the constant before using it. Using final keyword to declare constants, the following describes the syntax for declaring constants

Syntax :

final= ;

Describe :

final: Required keyword when declaring constants

data type: The data type of the constant.

· identifier name: Constant name, pay attention to the principle of naming constants like variable names.

value: Fixed value of the constant.

3.2 Example

Declare constant PI to use for calculation.

Figure 4: Declare and use constants

READ MORE  Exercise; What is Exercise/ Strike Price ? Definition, Example, Explanation

4. Data import and export format

With new programmers learn Java programming, the use of the command line window to perform data input and output of the program. To do this, it is imperative that programmers understand the basic input and output methods of Java. Basically, performing data import and export through the command window is simpler than other applications such as Web applications, Mobile …

4.1 Data export method

In Java, the System.out class in packageSystem is used to perform operations on the console. To display the screen, we use the method to display data to the command window:

Use print() and println() methods

+ System.out.print() : Display the results on the screen.

+ System.out.println(): Display the results to the screen and line breaks.

Eg :

Figure 5: Using the print and println methods

Result of running the program

Figure 6: Description of the results of running the program

Use printf() method

The printf() method is used to display numeric data to the console in different number formats such as:

+%d : display the result in integer format

+%f:show results in real number format

+%o: Display results in octal format.

+%e : display results in scientific number format

+%n: display the results in a new line.

See also: What is Take Over – Meaning of the word Take Over

Eg :

Figure 7: Example using the printf() method

The results are displayed as follows:

Figure 8: Result of running the program

4.2 Data input method

In Java, to read data from the keyboard we use Standard in input and pass the Scanner class.