Biến (variable) là một vùng nhớ trong máy tính dùng để lưu trữ giá trị (thường lưu trữ trên RAM máy tính).
data_type variable_name = value;
Trong đó:
data_type
: kiểu dữ liệu của biếnvariable_name
: tên biếnvalue
: giá trị của biếnVí dụ:
// Khai báo 3 biến tên "a", "b", "c" // - Có kiểu dữ liệu số nguyên int // - Không có giá trị ban đầu int a, b, c; // Khai báo 2 biến tên "diem_lt", "diem_th" // - Có kiểu dữ liệu số nguyên int // - Có gán giá trị ban đầu int diem_lt = 10, diem_th = 3; // Khai báo biến tên "B" // - Có kiểu dữ liệu byte // - Có giá trị ban đầu byte B = 22; // Khai báo biến tên "kytu" // - Có kiểu dữ liệu char // - Có giá trị ban đầu char kytu = 'c';
Tên biến cần tuân theo các quy tắt sau:
Sr.No | Reserved Words & Description |
---|---|
1 | abstract
As per dictionary, abstraction is the quality of dealing with ideas rather than events. |
2 | assert
assert keyword is used in Java to define assertion. An assertion is a statement in Java which ensures the correctness of any assumptions which have been done in the program. |
3 | boolean
boolean datatype is one of the eight primitive datatype supported by Java. It provides means to create boolean type variables which can accept a boolean value as true or false. |
4 | break
The break statement in Java programming language has the following two usages −
|
5 | byte
byte datatype is one of the eight primitive datatype supported by Java. It provides means to create byte type variables which can accept a byte value. |
6 | case
case keyword is part of switch statement which allows a variable to be tested for equality against a list of values. |
7 | catch
An exception (or exceptional event) is a problem that arises during the execution of a program. |
8 | char
char datatype is one of the eight primitive datatype supported by Java. |
9 | class
Java is an Object-Oriented Language. As a language that has the Object-Oriented feature. |
10 | const
final keyword is used to define constant value or final methods/classes in Java. |
11 | continue
The continue keyword can be used in any of the loop control structures. |
12 | default
default keyword is part of switch statement which allows a variable to be tested for equality against a list of values. |
13 | do
A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time. |
14 | double
double datatype is one of the eight primitive datatype supported by Java. |
15 | if
An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. |
16 | enum
The Java Enum class is the common base class of all Java language enumeration types. |
17 | extends
extends is the keyword used to inherit the properties of a class. Following is the syntax of extends keyword. |
18 | final
final keyword is used to define constant value or final methods/classes in Java. |
19 | finally
finally keyword is used to define a finally block. The finally block follows a try block or a catch block. A finally block of code always executes, irrespective of occurrence of an Exception. |
20 | float
float datatype is one of the eight primitive datatype supported by Java. It provides means to create float type variables which can accept a float value. |
21 | for
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to be executed a specific number of times. |
22 | goto
goto statement is not supported by Java currrenly. It is kept as a reserved keyword for future. As an alternative, Java supports labels with break and continue statement. |
23 | if
An if statement consists of a Boolean expression followed by one or more statements. |
24 | implements
Generally, the implements keyword is used with classes to inherit the properties of an interface. |
25 | import
import keyboard is used in context of packages. |
26 | instanceof
instanceof keyword is an operator which is used only for object reference variables. |
27 | int
int datatype is one of the eight primitive datatype supported by Java. |
28 | interface
An interface is a reference type in Java. It is similar to class. It is a collection of abstract methods. |
29 | long
long datatype is one of the eight primitive datatype supported by Java. |
30 | native |
31 | new |
32 | package
Packages are used in Java in order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations and annotations easier, etc. |
33 | private
Methods, variables, and constructors that are declared private can only be accessed within the declared class itself. |
34 | protected
The protected access modifier cannot be applied to class and interfaces. |
35 | public
A class, method, constructor, interface, etc. declared public can be accessed from any other class. |
36 | return |
37 | short
By assigning different data types to variables, you can store integers, decimals, or characters in these variables. |
38 | static
The static keyword is used to create variables that will exist independently of any instances created for the class. |
39 | strictfp |
40 | super
The super keyword is similar to this keyword. |
41 | switch
A switch statement allows a variable to be tested for equality against a list of values. |
42 | synchronized |
43 | this
this keyword is a very important keyword to identify an object. Following are the usage of this keyword. |
44 | throw
If a method does not handle a checked exception, the method must declare it using the throws keyword. |
45 | transient
Serialization is a concept using which we can write the state of an object into a byte stream so that we can transfer it over the network (using technologies like JPA and RMI). |
46 | try
A method catches an exception using a combination of the try and catch keywords. |
47 | void |
48 | volatile |
49 | while
A while loop statement in Java programming language repeatedly executes a target statement as long as a given condition is true. |
Trong Java cung cấp các kiểu dữ liệu nguyên thủy (primitive data types) - mà có thể làm nền tảng để kết hợp thành các kiểu dữ liệu phứt hợp sau này như sau:
byte
trong Java-128 (-2^7)
127 (2^7 - 1)
0
byte a = 100; byte b = -33;
short
trong Java-32,768 (-2^15)
32,767 (2^15 - 1)
0
short a = 10000; short b = -20000;
int
trong Java-2,147,483,648 (-2^31)
2,147,483,647 (2^31 - 1)
0
int tong_so_luong = 230000000; int b = -22222222;
long
trong Java-9,223,372,036,854,775,808 (-2^63)
9,223,372,036,854,775,807 (2^63 - 1)
0L
long a = 1000000000L; long b = -20000000L;
float
trong Java1.4E^-45f
3.4028235^+38f
0
float diem_lt = 6.5f; float gia_tien = 1999.99f;
double
trong Java4.9^-324d
1.79769313486231570^+308d
0
double thanh_tien = 125000000.964535d;
bool
trong Javatrue
hoặc false
false
bool da_dang_nhap = false; bool co_hoa_don = true;
char
trong Java\uffff
\u0000
(0)\u0000
char kytu = 'c'; char letter = '\u0051'; System.out.println(letter); char letter1 = '9'; System.out.println(letter1); char letter2 = 65; System.out.println(letter2);
String
trong Javajava.lang.String
""
String hoten = "Duong Nguyen Phu Cuong"; String diachi = "123 Ly Tu Trong, Quan Ninh Kieu, TP Can Tho";
Cùng nhau học tập, khám phá các kiến thức nền tảng về Lập trình web, mobile, database nhé.
Nền tảng kiến thức - Hành trang tới tương lai hân hạnh phục vụ Quý khách!
Khám phá, trải nghiệm ngay
Vui lòng đăng nhập để gởi bình luận!
Đăng nhậpChưa có bình luận nào!